[英]Entity Framework throws invalid column name User_Id error
我试图在我的项目中创建和关联实体。 用户可以具有许多角色,声明和登录名。 另一方面,声明或登录只能有一个用户,而一个角色也可以有多个用户。 我有使用流畅的API定义的关系:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Entity<User>().HasMany(u => u.Claims).WithRequired(cl => cl.User).Map(m => m.MapKey("User"));
modelBuilder.Entity<Claim>().HasRequired(cl => cl.User).WithMany(u => u.Claims).Map(m => m.MapKey("User"));
modelBuilder.Entity<User>().HasMany(u => u.Logins).WithRequired(lg => lg.User).Map(m => m.MapKey("User"));
modelBuilder.Entity<Login>().HasRequired(lg => lg.User).WithMany(u => u.Logins).Map(m => m.MapKey("User"));
modelBuilder.Entity<User>().HasMany(u => u.Roles).WithMany(ro => ro.Users).Map(userRoles =>
{
userRoles.ToTable("Users_Roles");
userRoles.MapLeftKey("User");
userRoles.MapRightKey("Role");
});
modelBuilder.Entity<Role>().HasMany(ro => ro.Users).WithMany(u => u.Roles).Map(userRoles =>
{
userRoles.ToTable("Users_Roles");
userRoles.MapLeftKey("User");
userRoles.MapRightKey("Role");
});
}
如您所见,我的数据库表未遵循Entity Framework的约定,而不是将“ User_Id”用作外键,而是将外键简称为“ User”。 但是,我不断收到此“无效的列名称:User_Id”异常消息。
我试图通过调用方法Map()和MapKey()用上述代码定义外键列名,但没有成功。 为什么会这样呢? 我写的映射代码错了吗? 有人可以帮忙吗?
PS:异常错误消息非常有用,我不知道此列名称错误与哪个表相关联。 有谁知道如何使异常消息显示表名,而不仅仅是列名? 谢谢。
另外,我还添加了用于Claim,Login和Role实体的代码(删除了不相关的方法)。
public class Claim
{
public string Id { get; protected set; }
public string Type { get; protected set; }
public string Value { get; protected set; }
public virtual User User { get; protected set; }
public Claim() { }
public Claim(string id, User user, string type, string value)
{
Id = id;
User = user;
Type = type;
Value = value;
}
}
public class Login
{
public string Id { get; protected set; }
public string Provider { get; protected set; }
public string Key { get; protected set; }
public DateTime? DateLoggedin { get; protected set; }
public virtual User User { get; protected set; }
public Login() { }
public Login(string id, string provider, string key, DateTime? dateLoggedIn = null)
{
Id = id;
Provider = provider;
Key = key;
DateLoggedin = dateLoggedIn;
}
}
public class Role
{
public string Id { get; protected set; }
public string Title { get; protected set; }
public string Description { get; protected set; }
public bool IsAdmin { get; protected set; }
public bool IsBanned { get; protected set; }
public IList<User> Users { get; protected set; }
public Role() { }
public Role(string id, string title, string description, bool isAdmin, bool isBanned)
{
Id = id;
Title = title;
Description = description;
IsAdmin = isAdmin;
IsBanned = isBanned;
}
}
EF约定正在创建User_Id列,因为您将密钥映射到与导航属性同名的属性。
您可以删除.Map()部分,让EF处理密钥,也可以在Claim和Login类上定义密钥属性,然后在流畅的api中进行映射。 我会亲自去做。
例如:
public class Claim
{
public string Id { get; protected set; }
public string Type { get; protected set; }
public string Value { get; protected set; }
public virtual User User { get; protected set; }
public int UserId{get;set;}
public Claim() { }
}
在您的模型创建中
modelBuilder.Entity<User>().HasMany(a=>a.Claims).WithRequired(a=>a.User).HasForeignKey(a=>a.UserId);
如果您创建“ EntityNameId”(用户ID,ClaimId等)的键字段,则EF约定将自动将其映射为您的外键,而不会进行流畅的映射。 但是,如果您使用其他名称(UsernameId或其他名称),则必须显式提供映射。 一些阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.