鉴于以下情况,Entity Framework Code-first 未命名/重命名连接或连接表。
public class User () {
//all fields from Users table
//...
//Role collection from Roles table through UserRoleLinks table
public List<Role> Roles { get; set; }
}
public class AppUser() {
//subset of fields from Users through AppUsers View
//...
//Role collection from Roles table through UserRoleLinks table
public List<Role> Roles { get; set; }
}
User 和 AppUser 都有以下配置条目:
this.HasMany(e => e.Roles)
.WithMany().Map(m => {
m.MapLeftKey(a => a.Id, "Users_Id");
m.MapRightKey(b => b.Id, "Roles_Id");
m.ToTable("UserRoleLinks");
});
在运行时尝试使用 AppUsers (DbSet<AppUser>) 时出现以下错误。
“无效的对象名称 'dbo.UserRoleLinks1'。” “无效的对象名称 'dbo.UserRoleLinks1'。”
似乎代码优先配置正在增加连接表名称,因为一个已经存在。
我怎样才能得到这个工作?有没有更好的方法来获取表列的子集?