问题
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