我在 Oracle 数据库中使用数据库优先的方法。在数据库中,我有 3 个表:USERS、ROLES 和它们之间的连接表:USERS_ROLES_ASSOCIATION。我的模型包含两个类:
[Table("USERS", Schema = "SIGNUM")]
public class User
{
public User()
{
Roles = new HashSet<Role>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Column("USR_ID")]
public decimal UserId { get; set; }
[Column("USR_LOGIN")]
public string Login { get; set; }
[Column("USR_PASS")]
public string Password { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
[Table("ROLES", Schema = "SIGNUM")]
public class Role
{
public Role()
{
Users = new HashSet<User>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Column("ROL_ID")]
public decimal RoleId { get; set; }
[Column("ROL_NAME")]
public string RoleName { get; set; }
public virtual ICollection<User> Users { get; set; }
}
当我尝试解析用户的角色时:
using (var dbContext = new AppDbContext())
{
User user = dbContext.Users.FirstOrDefault(u => string.Equals(u.Login, "someusername"));
var roles = user.Roles.Select(r => r.RoleName); // exception
}
...我收到以下错误:“ORA-00942:表或视图不存在”。
问题是 EF 正在寻找表“dbo”。“RoleUsers”而不是“SIGNUM”。“USERS_ROLES_ASSOCIATION”。如何在数据库优先方法中为多对多关系和模式名称指定连接表名称?