Dynamic 实体框架代码首先不带modelBuilder.Configuration或运行时修改modelBuilder.Configuration
我是实体框架的新手,我用下面的例子创建了从DbContext派生的类 公共部分类StudentdbContext:DbContext{Dynamic 实体框架代码首先不带modelBuilder.Configuration或运行时修改modelBuilder.Configuration,dynamic,runtime,entity-framework-5,code-first,Dynamic,Runtime,Entity Framework 5,Code First,我是实体框架的新手,我用下面的例子创建了从DbContext派生的类 公共部分类StudentdbContext:DbContext{ public StudentdbContext() : base("Name=StudentdbContext"){ } public DbSet<student> students { get; set; } protected override void OnModelC
public StudentdbContext()
: base("Name=StudentdbContext"){
}
public DbSet<student> students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Configurations.Add(new studentMap());
}
publicsstudentdbcontext()
:base(“Name=StudentdbContext”){
}
公共数据库集学生{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder){
添加(newstudentmap());
}
}
现在,我正在添加另一个名为“StudentInfo”的实体,代码为studentdbContext.Set(),但如何将“StudentInfo映射”添加到modelBuilder.Configuration runtime?是否可以使用代码优先实体框架而不使用映射对象,如“EntityTypeConfiguration”和“EntityTypeConfiguration”?
谢谢您必须使用类型,或者直接在OnModelCreating
方法中定义映射:
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Configurations.Add(new studentMap());
modelBuilder.Entity<StudentInfo>()
.HasRequired(...)
etc
}
protectedoverride void OnModelCreating(DbModelBuilder modelBuilder){
添加(newstudentmap());
modelBuilder.Entity()
.hassrequired(…)
等
}
我想这是实体框架不必要的限制之一。若他们有api来设置实体运行时,那个么若他们已经为变更后的modelBuilder.configuration运行时提供了api,那个就更好了。当数据库有很多表时,情况会变得更加复杂,即使您没有全部使用它们,但最终还是会在modelBuilder.Configuration中添加所有表,如果您将“EntityTypeConfiguration”类动态加载到modelBuilder.Configuration中