MVC5引用Guid

时间:2015-12-13 18:04:41

标签: c# asp.net entity-framework asp.net-mvc-5

我正在创建一个这样的表:

public class Team
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string CaptainId { get; set; }
    public string CoCaptainId { get; set; }
    public string ContactDetails { get; set; }
}

然后我有一张这样的表:

public class TeamMember
{
    public string Id { get; set; }
    public string Description { get; set; }
    public string GameDisplayName { get; set; }
    public DateTime Joined { get; set; }
    public string TeamId { get; set; }
}

我有2个问题,我不了解EF 6和MVC5。

1)如何在Team表中引用我的Id字段。我是在TeamMember中将TeamId设置为字符串还是Guid?我知道我需要设置属性[ForeignKey("Team")]但是我仍然不明白如何在代码中正确引用它,因为每当我需要进行任何类型的比较时,我总是要输入.ToString()在Guid上能够获得与另一个字符串值进行比较的值。

2)我的TeamMember也有一个Id,这个Id在Identity框架中引用了User。我应该如何引用那个,Id是一个引用User表中Id的外键,但我不知道如何正确引用那个,是字符串就像我做的那样,还是Guid或者什么的别的?

编辑:

public class Team
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public Guid CaptainId { get; set; }
    public Guid CoCaptainId { get; set; }
}

public class TeamMember
{
    public Guid MemberId { get; set; }
    public string TeamId { get; set; }
    public virtual Team Team { get; set; }
    public virtual Member Member { get; set; }
}

public class Member : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string About { get; set; }
    public string Alias { get; set; }
    public string CustomUrl { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Member> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<Member>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Team> Teams { get; set; }
    public DbSet<TeamMember> TeamMembers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUserClaim>().ToTable("MemberClaim");
        modelBuilder.Entity<IdentityUserRole>().ToTable("MemberRole");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("MemberLogin");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<Member>().ToTable("Member");
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

我得到的错误:

var user = new Member { UserName = model.Email, Email = model.Email, CustomUrl = model.CustomUrl, Alias = model.Alias};
var team = new Team {CaptainId = user.Id, Created = currentTime, IsSingleMember = true};

CaptainId = user.Id gives an error:

Cannot convert source type 'string' to target type 'System.Guid'

1 个答案:

答案 0 :(得分:3)

您应该使用相同的类型,即<label id="lbl_ftjenester" runat="server" style="color: black; background-color: #f2f2f2; font-size: 18px; font-weight: 600; float:left; border: 2px; padding: 5px; border-width: 4px; border-style: outset; border-color: #00e6ab;"> Flyttetjenester<input id="ftjenester" type="radio" name="skjematype" runat="server"/></label> <label id="lbl_hleie" runat="server" style="color: black; background-color: #f2f2f2; font-size: 18px; font-weight: 600; float:right; border: 2px; padding: 5px; border-width: 4px; border-style: outset; border-color: #e6b800;"> Hengerleie<input id="hleie" type="radio" onserverchange="change_input_form" name="skjematype" runat="server"/></label>

public partial class _Default : System.Web.UI.Page
{
    HtmlInputRadioButton v_ftjenester = new HtmlInputRadioButton();
    HtmlInputRadioButton v_hleie = new HtmlInputRadioButton();

    HtmlGenericControl v_lbl_ftjenester = new HtmlGenericControl();
    HtmlGenericControl v_lbl_hleie = new HtmlGenericControl();

    protected void Page_Load(object sender, EventArgs e)
    {
        //sets the status of the ordering forms in terms of looks and on/off
        v_ftjenester = ftjenester;

        ftjenester.Checked = true;

        v_lbl_hleie = lbl_hleie;

        v_lbl_hleie.Style.Add("opacity", "0.5");
    }

    protected void change_input_form(object sender, EventArgs e)
    {
        //some css code to show change in status
        v_hleie = hleie;

        if (v_hleie.Checked == true)
        {
            v_lbl_hleie = lbl_hleie;

            v_lbl_hleie.Style.Remove("opacity");

            v_lbl_ftjenester = lbl_ftjenester;

            v_lbl_ftjenester.Style.Add("opacity", "0.5");
        }
        else
        {
            v_lbl_ftjenester = lbl_ftjenester;

            v_lbl_ftjenester.Style.Remove("opacity");
            
            v_lbl_hleie = lbl_hleie;

            v_lbl_hleie.Style.Add("opacity", "0.5");
        }
    }
  }

此外,您需要在Guid模型中保留public Guid TeamId { get; set; } 类型的virtual Team属性。通常,TeamTeamMemberint是用于表的主键的类型。 long可能不是一个好主意,因为您需要执行自定义代码来生成表中不存在的字符串。

这将生成具有适当外键关系的2个表。

Guid

您现在无需进行string转换即可进行比较。你可以做public class Team { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string CaptainId { get; set; } public string CoCaptainId { get; set; } public string ContactDetails { get; set; } } public class TeamMember { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } public string Description { get; set; } public string GameDisplayName { get; set; } public DateTime Joined { get; set; } public Guid TeamId { get; set; } public virtual Team Team { set; get; } } 表达