What is the difference between EntityTypeConfiguration and DbMigration for a new EF project

柔情痞子 提交于 2019-12-12 17:06:33

问题


Should I use EntityTypeConfiguration to build my data model, or should I directly enable migrations an use DbMigration.

That is use :

public class BlogEFCFConfiguration : EntityTypeConfiguration<Blog> {
    public BlogEFCFConfiguration()
        : base() {
        HasKey(x => x.BlogId);
        Property(x => x.BlogId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.Name).HasColumnType("varchar").HasMaxLength(128);
    }
}

or

public partial class InitialCreate : DbMigration {
    public override void Up() {
        CreateTable(
            "dbo.Blogs",
            c => new
                {
                    BlogId = c.Int(nullable: false, identity: true),
                    Name = c.String(maxLength: 128, unicode: false),
                })
            .PrimaryKey(t => t.BlogId);

    }

    public override void Down() {
        DropTable("dbo.Blogs");
    }
}

Indeed if I want to change my model I will have to finally use DbMigration. So why use EntityTypeConfiguration ?

It is may be a too open question.


回答1:


They are doing different jobs - EF migrations ensure that the application and database changes are aligned. The configurations inform EF how to map from your object model to your relational model.

Configurations are required when the default conventions don't suit your model.

EF migrations are required if you wish to model the database changes in code between application versions. This has the advantage of being able to automatically have the application update the database to the latest version on startup for example.



来源:https://stackoverflow.com/questions/17805394/what-is-the-difference-between-entitytypeconfiguration-and-dbmigration-for-a-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!