.net 三方关系:用户、LOB和角色
假设我们有用户、业务线LOB和角色。用户有1个LOB。但是,用户也可以在不同的LOB中拥有多个角色 下面的代码优先模型创建了3个表用户、角色和LOB。如果留给他自己的设备,它也会创建用户角色。这不是我想要的 到目前为止,我已经尝试创建UserLOBRole{RoleId,UserId,LOBCode}对象,并将所有字段标记为复合主字段,但是EF对外键发出吠声 我有没有办法先在EF4.1代码中描述这种关系 非常感谢您的帮助.net 三方关系:用户、LOB和角色,.net,c#-4.0,entity-framework-4.1,ef-code-first,.net,C# 4.0,Entity Framework 4.1,Ef Code First,假设我们有用户、业务线LOB和角色。用户有1个LOB。但是,用户也可以在不同的LOB中拥有多个角色 下面的代码优先模型创建了3个表用户、角色和LOB。如果留给他自己的设备,它也会创建用户角色。这不是我想要的 到目前为止,我已经尝试创建UserLOBRole{RoleId,UserId,LOBCode}对象,并将所有字段标记为复合主字段,但是EF对外键发出吠声 我有没有办法先在EF4.1代码中描述这种关系 非常感谢您的帮助 public class User { public int Us
public class User
{
public int UserId {get;set;}
public string UserName {get;set;}
public virtual LOB UserLOB {get;set;}
// HOW DO I DEFINE RELATIONSHIP HERE?
public virtual ICollection<Role> UserRoles {get;set;}
}
public class Role
{
public int RoleId {get;set;}
public string RoleName {get;set;}
}
public class LOB
{
public string LOBCode {get;set;}
public string LobName {get;set;}
}
如果不将UserLOBRole作为单独的实体公开,并通过这个新实体互连用户、角色和LOB,这是不可能的。隐藏连接表只适用于多对多关系,不需要任何附加数据,但情况并非如此
public class UserLOBRole
{
[Key, Column(Order = 0)]
public int UserId { get; set; }
[Key, Column(Order = 1)]
public int RoleId { get; set; }
[Key, Column(Order = 2)]
public string LOBCode { get; set; }
public virtual User User { get; set; }
public virtual Role Role { get; set; }
[ForeignKey("LOBCode")]
public virtual LOB LOB { get; set; }
}
您的代码可能会抱怨FKs,因为您的LOB和角色当前没有FKs,所以UserLOBRole必须有FKs
您的用户现在将使用:
public class User
{
public int UserId {get;set;}
public string UserName {get;set;}
public virtual LOB UserLOB {get;set;}
public virtual ICollection<UserLOBRole> UserRoles {get;set;}
}
谢谢,看来这是唯一的办法。将需要完善API以回答诸如此用户具有哪些角色、此用户是否在lob中具有角色等问题。