我有用户(名称,密码)模型和角色模型(id,角色)。现在我想要的是在这两个模型之间映射模型(id,Uid,Rid),这里 Uid,Rid 外键也 Uid 应该是唯一的。怎么能做到这一点。我已经完成了外键和映射表,但我坚持在 Uid 上应用唯一键。
任何人都可以帮助我吗? 提前致谢。
public class Image
{
public int id { get; set; }
public string Name{ get; set; }
public String Password { get; set; }
}
//User roles class
public class Role
{
public int id { get; set; }
public string Roles { get; set; }
}
//User mapping with roles
public class Rolemapping
{
public int id { get; set; }
public int Uid { get; set; }
[ForeignKey("Uid")]
public Image User { get; set; }
public int Rid { get; set; }
[ForeignKey("Rid")]
public Role Role { get; set; }
}
在 EF 6.1 和
之前,请按照上面的链接创建唯一键EF 6.1 之后你可以很容易地拥有独特的约束,
[Index("TitleIndex", IsUnique = true)]
public string Title { get; set; }
如果您使用的是 MYSQL,您可能会在没有密钥长度的情况下在密钥规范中使用 BLOB/TEXT 列“值”,因此请添加密钥长度
[Index("TitleIndex", IsUnique = true)]
[MaxLength(15)]
public string Title { get; set; }
这也会将字段类型更改为 varchar