当我调试我的应用程序时,此方法似乎不在DbContext的构造函数上下文中执行.那么什么时候被召唤呢?
我从数据库中创建了一个Entity Framework模型.我有多对多的关系:User- UserRole- Role.
EF创建UserRole实体与UserRoles导航属性在User实体和Role实体,但我宁愿Roles在User和Users中Role.这是否可以通过模型设计师设计?如何手动配置中间表的多对多关系?
我想每次都触发 OnModelCreating new DataContext(new Entity())......但是当我创建一个表的连接时,它可以工作。当为另一个表创建连接时,OnModelCreating 不再工作,所以因为我出错了,
the entity type <tableName> is not part of the model for the current context.
public class DataContext : DbContext
{
private BaseEntity _entity;
public DataContext(BaseEntity entity)
{
Database.Connection.ConnectionString = Parameters.ConnectionString;
_entity = entity;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
_entity.Map(modelBuilder); // this is dynamic fluent api here
}
}
Run Code Online (Sandbox Code Playgroud)