如何引用ASP.Net身份用户电子邮件属性作为外键

如何引用ASP.Net身份用户电子邮件属性作为外键,asp.net,sql-server,asp.net-mvc,entity-framework,asp.net-identity,Asp.net,Sql Server,Asp.net Mvc,Entity Framework,Asp.net Identity,试图将ASP.Net标识用户的电子邮件属性作为外键引用,但不断收到错误消息 使用MVC6、EF7 我有一个AppAccount,它是主要型号,而ApplicationUser:IdentityUser是从属型号 我正在尝试将ApplicationUser的Email属性设置为AppAccount模型的外键 public class AppAccount { public string AppAccountID { get; set; } public string Accoun

试图将ASP.Net标识用户的电子邮件属性作为外键引用,但不断收到错误消息

使用MVC6、EF7

我有一个
AppAccount
,它是主要型号,而
ApplicationUser:IdentityUser
是从属型号

我正在尝试将
ApplicationUser
Email
属性设置为
AppAccount
模型的外键

public class AppAccount
{
    public string AppAccountID { get; set; }

    public string AccountType { get; set; }

    public DateTime DateCreated { get; set; }

    public virtual ApplicationUser AppUser { get; set; }

}


public class ApplicationUser : IdentityUser
{

    public string FirstName { get; set; }

    public string Surname { get; set; }

    public DateTime DOB { get; set; }

    public virtual AppAccount AppAccount { get; set; }

}
“偷看”到IdentityUser的定义会告诉我电子邮件属性的类型为string

public class IdentityUser<TKey> where TKey : IEquatable<TKey>
{
    ...
    //
    // Summary:
    //     Gets or sets the email address for this user.
    public virtual string Email { get; set; }
...
}
当我运行迁移时,它工作正常,但是当我尝试更新数据库时,我得到以下错误

Error Number:1753,State:0,Class:16
Column 'AspNetUsers.Email' is not the same length or scale as 
referencing column 'AppAccount.AppAccountID' 
in foreign key 'FK_AppAccount_ApplicationUser_AppAccountID'. 
Columns participating in a foreign key relationship must 
be defined with the same length and scale.
Could not create constraint or index. See previous errors.
我已尝试手动将
AppAccountID
电子邮件
属性的最大长度设置为相同的限制

builder.Entity<ApplicationUser>(au =>
            {
                ...
                au.Property(u => u.Email).HasMaxLength(100);    
            });

builder.Entity<AppAccount>(aa =>
        {
            ...
            aa.Property(a => a.AppAccountID).HasMaxLength(100);
            ...
        });
我尝试将
AppAccount
模型的
AppAccount
属性设置为
virtual

`public virtual string AppAccountID {get ; set ;}
我认为这可能是服务器问题,但检查数据库时,
电子邮件
列类型是nvarchar,因此我不明白它为什么不编译?

试试这种模式

public class AppAccount
{
public string AppAccountID { get; set; }

public string AccountType { get; set; }

public DateTime DateCreated { get; set; }
[ForeignKey("UserId")
public virtual ApplicationUser AppUser { get; set; }

}

抱歉,这是业余时间

首先,我写的是
haspricplekey
,而不是@TetsuyaYamamoto(谢谢)指出的
HasForeignKey

其次,在第无数次检查数据库之后,
ApplicationUser
Email
属性的类型为
NVARCHAR(256)
,因此按照以下方式更新定制允许EF成功编译模型

builder.Entity<AppAccount>(aa =>
        {
            aa.HasKey(a => a.AppAccountID);
            aa.Property(a => a.AppAccountID).ForSqlServerHasColumnType("NVARCHAR(256)");
            aa.HasOne(a => a.AppUser)
            .WithOne(u => u.AppAccount)
            .HasForeignKey<ApplicationUser>(u => u.Email);
        });
builder.Entity(aa=>
{
aa.HasKey(a=>a.appaccounted);
aa.Property(a=>a.AppAccountID).ForSqlServerHasColumnType(“NVARCHAR(256)”);
aa.HasOne(a=>a.AppUser)
.带一个(u=>u.AppAccount)
.HasForeignKey(u=>u.Email);
});

谢谢大家…这就是为什么您不在早上1点编码的原因,正如错误所提示的那样,检查电子邮件是否具有在参与外键关系的model
列中定义的长度属性,必须使用相同的长度和比例来定义
意味着您需要检查外键关系,数据类型和链接列的长度。您也可以尝试
HasForeignKey
内对modelcreating
方法进行操作。@LibinJoseph-在我可以看到的模型中没有定义长度属性,在以前解决问题的尝试中,试图覆盖length属性,但没有效果。@TetsuyaYamamoto-按照建议在
OnModelCreating
中使用
HasForeignKey
进行了尝试,但仍然收到相同的错误。仍然在寻找FK和数据类型的差异…@Scheeeve我无法重现这一点-如果我设置相同的长度(
HasMaxLength(x)
),它对我有效。在调用
base.OnModelCreating(builder)
内部
OnModelCreating
方法后是否添加自定义项?添加
[ForeignKey(“UserId”)]
注释会将FK关系指向错误的列。我确实尝试将
ForeignKey(“Email”)]
添加到模型中,但在尝试更新数据库时仍然收到相同的错误
`public virtual string AppAccountID {get ; set ;}
public class AppAccount
{
public string AppAccountID { get; set; }

public string AccountType { get; set; }

public DateTime DateCreated { get; set; }
[ForeignKey("UserId")
public virtual ApplicationUser AppUser { get; set; }

}
builder.Entity<AppAccount>(aa =>
        {
            aa.HasKey(a => a.AppAccountID);
            aa.Property(a => a.AppAccountID).ForSqlServerHasColumnType("NVARCHAR(256)");
            aa.HasOne(a => a.AppUser)
            .WithOne(u => u.AppAccount)
            .HasForeignKey<ApplicationUser>(u => u.Email);
        });