Unique key applying on asp.net MVC 5 model property

心已入冬 提交于 2020-01-03 02:23:33

问题


I have User(name,password) model and Roles model(id,role).Now what I want is mapping model(id,Uid,Rid) between these two models and here Uid,Rid foreign keys also Uid should be unique. How can acheive this. I have done with foreign key and with mapping table but I stuck at unique key applying on Uid.

Can anyone help me in this? Thanks in advance.

 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; }
}

回答1:


Unique Key in Model Code First

Follow the above link to create Unique Key prior to EF 6.1 and

EF 6.1 onwards you can easily have unique constrains ,

[Index("TitleIndex", IsUnique = true)]

public string Title { get; set; }



来源:https://stackoverflow.com/questions/24670109/unique-key-applying-on-asp-net-mvc-5-model-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!