简体   繁体   English

实体框架 - 代码优先方法

[英]Entity Framework - Code First Approach

I am just following the entity framework code first approach in my project, and I fell into some problems. 我只是在我的项目中遵循实体框架代码的第一种方法,我遇到了一些问题。 Always I am forced to regenerate the db using the commands. 总是我被迫使用命令重新生成数据库。 however, now I am managed to get that stable. 然而,现在我成功地保持稳定。 I have a scenario and in this, the framework is creating a migration script is not logical, may be I am missing something and need your help in that 我有一个场景,在这个,框架创建一个迁移脚本是不合逻辑的,可能是我错过了一些东西,需要你的帮助

The scenario is - I have enabled the migration, and It created an InitialCreate class with all my table setup, I am pretty impressed with that. 场景是 - 我启用了迁移,并且它创建了一个包含所有表格设置的InitialCreate类,我对此印象非常深刻。 but when i make change in the entity, for eg, I have Deal Class 但是当我在实体中进行更改时,例如,我有Deal Class

I added new property called LenderName, BorrowerName, and run teh add-migration script, it created a migration script for me , 我添加了名为LenderName,BorrowerName的新属性,并运行了添加迁移脚本,它为我创建了一个迁移脚本,

 public partial class LenderBorrowerName : DbMigration
    {
        public override void Up()
        {
            DropColumn("dbo.Deals", "LenderName");
            DropColumn("dbo.Deals", "BorrowerName");
            DropColumn("dbo.Deals", "Discriminator");
        }

        public override void Down()
        {
            AddColumn("dbo.Deals", "Discriminator", c => c.String(nullable: false, maxLength: 128));
            AddColumn("dbo.Deals", "BorrowerName", c => c.String());
            AddColumn("dbo.Deals", "LenderName", c => c.String());
        }
    }

The problem is, the original database doesn't have this field and through this migration script it suppose to add to the database. 问题是,原始数据库没有此字段,并且通过此迁移脚本,它假定要添加到数据库。 To update these fields, if I run the update-migration -force, it fails because of the up() function above, as it try to remove the column from the table. 要更新这些字段,如果我运行update-migration -force,由于上面的up()函数失败,因为它尝试从表中删除列。 Why Microsoft is adding that drop script, is that the developer need to remove manually ? 为什么Microsoft要添加该删除脚本,开发人员是否需要手动删除? please help 请帮忙

The hint here is the Discriminator column. 这里的提示是Discriminator列。 EF applies a default strategy to implement inheritance which is called TPH, Table-Per-Hierarchy. EF应用默认策略来实现继承,称为TPH,Table-Per-Hierarchy。 The Discriminator field is automatically generated by EF when you have inheritance between your entities. 当您在实体之间具有继承时,EF将自动生成Discriminator字段。

It seems that you did more changes in your model than adding those two properties to an existing entity: you probably moved them from a derived class to a base class, or the opposite way around. 您似乎在模型中进行了更多更改,而不是将这两个属性添加到现有实体:您可能将它们从派生类移动到基类,或者相反的方式。 Also it seems that your DbContext only includes a DbSet for the base entity or the derived entity, but not for both. 此外,您的DbContext似乎只包含基本实体或派生实体的DbSet ,但两者都不包含。 Be aware that migrations by default only include the entities that are included in your DbContext as DbSets (you can also include or exclude them explicitly in your mappings). 请注意,默认情况下,迁移仅包括DbContext中包含的实体作为DbSets (您还可以在映射中显式包含或排除它们)。 So your migration is generating only the fields for the entity in your DbContext , and not for the other one, and EF is interpreting that you removed those properties, that is why you get DropColumn sentences. 因此,您的迁移仅在DbContext生成实体的字段,而不是为另一个生成,EF正在解释您删除了这些属性,这就是您获得DropColumn句子的原因。

So the solution here would be to add the missing DbSet to the DbContext and generate the migration again. 所以这里的解决方案是将缺少的DbSet添加到DbContext并再次生成迁移。

Here you can have more info about TPH. 在这里您可以获得有关TPH的更多信息。

A different explanation could be that after running Update-Database and Add-Migration several times and using the -force parameter your database has been left in an state that is inconsistent with the contents of your __MigrationHistory table. 一个不同的解释可能是,在多次运行Update-DatabaseAdd-Migration并使用-force参数后,您的数据库处于与__MigrationHistory表的内容不一致的状态。 My advice there would be to manually delete the database, if possible, generate a clean Initial migration and run it. 我的建议是手动删除数据库,如果可能的话,生成一个干净的Initial迁移并运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM