本文介绍了唯一键应用上asp.net MVC 5模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户(姓名,密码)模型和角色模型(ID,角色)。现在我想的是这两款车型在这里的Uid之间的映射模型(ID,UID,RID),摆脱外键也应该的Uid是独特。怎样才能达致这。我曾与外键和映射表,这样做,但我还是坚持在唯一键上应用的UID。

谁能帮我在这?
先谢谢了。

 公共类图片
 {
    公众诠释ID {搞定;组; }    公共字符串名称{;组; }    公共字符串密码{搞定;组; }
}//用户角色类
公共类角色
{
    公众诠释ID {搞定;组; }    公共字符串角色{搞定;组; }
}//用户映射与角色
公共类Rolemapping
{
    公众诠释ID {搞定;组; }    公众诠释的Uid {搞定;组; }    [ForeignKey的(UID)]
    公众形象用户{搞定;组; }    公众诠释摆脱{搞定;组; }    [ForeignKey的(摆脱)]
    公共角色角色{搞定;组; }
}


解决方案

请按照上面的链接创建到EF 6.1唯一键之前以及

EF 6.1起,可以轻松拥有独特的约束,

[指数(标题索引= isUnique设置真)]

公共字符串名称{搞定;组; }

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; }
}
解决方案

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

这篇关于唯一键应用上asp.net MVC 5模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:23