簡體   English   中英

如何將ASP.Net Identity User Email屬性作為外鍵引用

[英]How to reference ASP.Net Identity User Email property as Foreign Key

嘗試將ASP.Net Identity User的Email屬性作為外鍵引用,但不斷收到錯誤消息

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

}

'Peeking'到IdentityUser的定義告訴我Email屬性是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; }
...
}

我已將AppAccount Model的PK設置為string並將ApplicationUserEmail屬性設置為Alternate鍵,然后使用流暢的API設置One-To-One關系...

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

}

道歉所有,它的業余時間

首先,正如@TetsuyaYamamoto(Thankyou)指出的那樣,我正在編寫HasPrincipleKey而不是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點編碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM