MapToStoredProcedures 现有程序被忽略

MapToStoredProcedures existing procedures ignored

我正在使用 EF 6 和 SQL 服务器 (LocalDB) 开发 ASP.Net MVC 网站。 我为 table :

的插入、更新、删除方法创建了自定义存储过程
CREATE PROCEDURE dbo.Project_Insert
   @LeftBound INT,
   @RightBound INT,
   @Level INT,
   @Name NVARCHAR(MAX),
   @SuggestedAt DATE,
   @SuggestedById INT
AS
BEGIN
...

而且我希望 EF 映射这些过程,以便在代码中保存项目实体时使用它们。所以我使用 MapToStoredProcedures :

modelBuilder.Entity<Project>().MapToStoredProcedures(p => 
            p.Insert(u => u.HasName("dbo.Project_Insert"))
             .Update(u => u.HasName("dbo.Project_Update"))
             .Delete(u => u.HasName("dbo.Project_Delete")));

问题是,即使我仔细检查了存储过程的名称和模型 class,更新数据库方法仍然告诉我有未决的更改:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.

当我 运行 Add-Migration Test 时,EF 创建了一个带有基本存储过程的迁移文件,因为它认为它从数据库中丢失了:

CreateStoredProcedure(
"dbo.Project_Insert",
 p => new
 {
     LeftBound = p.Int(),
     RightBound = p.Int(),
     Level = p.Int(),
     Name = p.String(),
     SuggestedAt = p.DateTime(storeType: "date"),
     SuggestedById = p.Int(),
 },
 body:
     @"INSERT [dbo].[Projects]([LeftBound], [RightBound], [Level], [Name], [SuggestedAt], [SuggestedById]) ...

我的问题是:为什么 EF 不检测现有的存储过程并将它们映射到项目实体?你们以前有过这个问题吗?

非常感谢

代码优先工作流希望您先编写代码,然后让它更新数据库。解决方法是添加一个空迁移,它将您的手动数据库更改与代码优先模型同步:

Add-Migration AddProjectProcs –IgnoreChanges
Update-Database

https://msdn.microsoft.com/en-us/data/dn579398.aspx?f=255&MSPPError=-2147217396#option1