【问题标题】:Can't make EF Code First work with manually changed data base无法使 EF Code First 与手动更改的数据库一起使用
【发布时间】:2012-08-17 13:42:58
【问题描述】:

我已向我的域模型类添加了两个新属性,并相应地向数据表添加了两个属性。然后我尝试启动我的 mvc web 应用程序并得到了

The model backing the 'EFDbContext' context has changed since the database was created.  
Consider using Code First Migrations to update the database  
(http://go.microsoft.com/fwlink/?LinkId=238269).

已阅读以下帖子:

MVC3 and Code First Migrations

EF 4.3 Automatic Migrations Walkthrough

我尝试通过包管理器控制台Update-Database,但出现错误

Get-Package : Не удается найти параметр, соответствующий имени параметра "ProjectName".
C:\Work\MVC\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:611 знак:40
+     $package = Get-Package -ProjectName <<<<  $project.FullName | ?{ $_.Id -eq 'EntityFramework' }
+ CategoryInfo          : InvalidArgument: (:) [Get-Package], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,NuGet.PowerShell.Commands.GetPackageCommand

The EntityFramework package is not installed on project 'Domain'.

但 Entityframework 安装在项目 Domain 上。我从引用中删除了它,删除了 package.config 并成功地重新安装了 EF。但是Update-Database 仍然返回相同的错误。 Update-Database -Config 也可以

我做错了什么?

编辑

非常感谢 Ladislav Mrnka,我会尝试重新表述我的问题。就我手动更改数据表而言,我预计不会使用迁移。但是我现在如何使 EF 与手动编辑的域模型类和数据表一起工作?

【问题讨论】:

  • 您想使用迁移,还是只想让 EF 使用您手动更改的数据库?迁移的重点是让 EF 定义和更新您的数据库。
  • @LadislavMrnka 我“只希望 EF 使用您手动更改的数据库”...因为我是 EF 的新手,所以我想到迁移是提示对于我的问题....如果我错了,请以正确的方式指导我!
  • 有多种使用 EF 的方法。您可以使用代码优先映射并手动维护数据库,或者您可以使用代码优先并让 EF 维护数据库 - 这就是迁移所做的。
  • 我都是手动完成的,但我现在如何让 EF 工作?

标签: c# asp.net-mvc-3 entity-framework ef-code-first


【解决方案1】:

尝试将此添加到您的应用程序的启动中(您可以将其添加到App_Start):

Database.SetInitializer<EFDbContext>(null);

它应该关闭所有与从 EF 处理数据库相关的逻辑。您现在将完全负责使您的数据库与您的模型保持同步。

【讨论】:

  • 现在两个新列都显示 列名无效
  • 智能感知在 Select TOP 1000 rows 命令中的 SSMS 中显示相同的错误...尽管它可以工作并返回行
  • 那么您是否将这些列添加到数据库中?
  • 当然,我做到了!我在 SSMS 的帮助下添加了它们...然后只想检查它,然后打开 Select TOP 1000 rows..它返回了行..但是在查询编辑器中的 select 语句中智能感知带下划线的名称添加的列中带有红线,表示列名无效
  • 我尝试将它们替换为其他列(具有不同的名称),但 SSMS 智能感知仍然显示 Invalid column name...
【解决方案2】:

我遇到了同样的问题,这就是我解决问题的方法。

我使用 sql 命令删除了表 __MigrationHistory 并再次运行 update-database -verbose。

这个自动创建的表显然有问题。

【讨论】:

    【解决方案3】:

    答案 2 正是我们所需要的。虽然当我到达 App_Start 时,我意识到有 4 个配置文件并且没有看到它适合任何一个。相反,我将其添加到我的 EF 数据库上下文中

    namespace JobTrack.Concrete
    {
        public class EFDbContext : DbContext 
        {
            //Set the entity framework database context to the connection name
            //in the Webconfig file for our SQL Server data source QSJTDB1
            public EFDbContext() : base("name=EFDbConnection")
            {            
            }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Remove the tight dependency on the entity framework that 
            //wants to take control of the database. EF by nature wants
            //to drive the database so that the database changes conform
            //to the model changes in the application. This will remove the
            //control from the EF and leave the changes to the database admin
            //side so that it continues to be in sync with the model.
            Database.SetInitializer<EFDbContext>(null);
    
            //Remove the default pluaralization of model names
            //This will allow us to work with database table names that are singular
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();            
        }
    
        //Allows for multiple entries of the class State to be used with 
        //interface objects such as IQueryTables for the State database table  
        public DbSet<State> State { get; set; } 
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多