我很熟悉如何将流畅的API配置组织到EF6上的单独类中,但是如何通过EF7实现这一点?
以下是如何使用EF6执行此操作的示例:
ModelConfigurations.cs
public class ModelConfigurations : EntityTypeConfiguration<Blog> { ToTable("tbl_Blog"); HasKey(c => c.Id); // etc.. }
并从OnModelCreating()调用它
protected override void OnModelCreating(DbModelbuilder modelBuilder) { modelBuilder.Configurations.Add(new ModelConfigurations()); // etc... }
解决方法
试试这个:
public class BlogConfig { public BlogConfig(EntityTypeBuilder<Blog> entityBuilder) { entityBuilder.HasKey(x => x.Id); // etc.. } } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); new BlogConfig(modelBuilder.Entity<Blog>()); }