C# 实体框架多对多对单个对象
我试图映射一个用户的属性,该用户在数据库中具有多对多关系,但每个用户只有一个属性。但我无法在entityframework中找到所需的映射。我拥有以下实体:C# 实体框架多对多对单个对象,c#,sql,entity-framework,C#,Sql,Entity Framework,我试图映射一个用户的属性,该用户在数据库中具有多对多关系,但每个用户只有一个属性。但我无法在entityframework中找到所需的映射。我拥有以下实体: public class User { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } //Need to map this property
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//Need to map this property
public virtual SecurityRole SecurityRole { get; set; }
}
public class SecurityRole
{
public int Id { get; set; }
public string Name { get; set; }
}
a见下表:
User:
Id
FirstName
LastName
SecurityRole:
Id
Name
UserSecurityRole:
UserId
SecurityRoleId
如果有人有任何想法或可以为我指出正确的方向,那将是非常好的即使数据库中只有一条记录,如果您在
用户
和SecurityRole
之间有多对多关系,它应该是这样工作的:
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<SecurityRole> SecurityRoles { get; set; }
}
public class SecurityRole
{
public int Id { get; set; }
public string Name { get; set; }
public List<User> Users { get; set; }
}
公共类用户
{
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共列表SecurityRoles{get;set;}
}
公营证券
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表用户{get;set;}
}
所以一个用户只能有一个securityrole?为什么不删除这个多对多表,只将安全角色添加为用户的属性?我很想这样做,但我无法更改数据库结构,因为对于新数据库来说,它非常旧,并且有很多依赖的传统sql脚本时间?很可能,但有很多工作您必须对数据库结构建模,不是特定实例中的实际数据。实际上,每个用户只能有一个SecurityRole,这一事实并不能改变这样一个事实,即您可以有多个SecurityRole,否则,如果数据库中存在错误的数据,您的模型就会崩溃。我更希望用户只能有一个SecurityRole,因为应该只有一个SecurityRole,但我无法更改数据库结构