如何参考ASP.网络身份用户电子邮件属性作为外键



试图引用ASP的Email属性。Net Identity User作为外键,但一直得到错误消息

使用MVC6, EF7

我有一个AppAccount,它是主模型,ApplicationUser: IdentityUser是从属模型。

我试图将ApplicationUserEmail属性设置为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的定义告诉我电子邮件属性的类型是字符串…

public class IdentityUser<TKey> where TKey : IEquatable<TKey>
{
    ...
    //
    // Summary:
    //     Gets or sets the email address for this user.
    public virtual string Email { get; set; }
...
}

我将AppAccount模型的PK设置为字符串,并将ApplicationUserEmail属性设置为Alternate键,然后使用fluent API设置一对一关系…

builder.Entity<ApplicationUser>(au =>
            {
                au.HasAlternateKey(u => u.Email);
                au.HasAlternateKey(u => u.UserName);    
            });
        builder.Entity<AppAccount>(aa =>
        {
            aa.HasKey(a => a.AppAccountID);
            aa.HasOne(a => a.AppUser)
            .WithOne(u => u.AppAccount)
            .HasPrincipalKey<ApplicationUser>(u => u.Email);  // PK of AppAccount is FK of AppUser
        });

当我运行迁移时,它工作正常,但当我尝试更新数据库时,我得到以下错误

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.

我已经尝试手动设置AppAccountIDEmail属性的最大长度为相同的限制

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

我已经尝试在服务器上将两个属性设置为相同类型…

builder.Entity<ApplicationUser>(au =>
            {
                ...
                au.Property(u => u.Email).ForSqlServerHasColumnType("nvarchar(100)");    
            });
builder.Entity<AppAccount>(aa =>
        {
            ...
            aa.Property(a => a.AppAccountID).ForSqlServerHasColumnType("nvarchar(100)");
            ...
        });

尝试将ApplicationUser类中的Email属性重写为

public override string Email {get ; set ;}

,我尝试将AppAccount模型的AppAccountID属性设置为virtual

`public virtual string AppAccountID {get ; set ;}

我认为这可能是一个服务器问题,但检查数据库的Email列类型是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; }
}

抱歉,这是业余时间

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

其次,在无数次检查数据库后,ApplicationUserEmail属性为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);
        });

谢谢所有…这就是为什么你不在凌晨1点编写代码

相关内容

  • 没有找到相关文章

最新更新