簡體   English   中英

實體框架6在第3級子表中編碼第一個一對多關系

[英]Entity Framework 6 Code first One-to-Many relation in 3rd level child tables

我有ASP.NET身份框架創建的ApplicationUser表,並希望使用另一個表Customer與one-to-one relation ApplicationUser表的PK是string類型的Id Customer表的字符串類型為FK 我不允許更改ApplicationUser表的Id類型。 但是customer表必須具有int類型的標識(自動編號)列。 這可以。

然后,此Customer表與Contact表具有one-to-many關系。 在此輸入圖像描述

Customer表和Contact表都具有唯一的標識列Id Contact表有一個CustomerId列。 如果沒有AspNetUser表,代碼首先使用這些idCustomerId列生成one-to-many關系。 由於AspNetuser表與Customer具有one-to-one關系,因此不使用CustomerId 但是代碼首先生成自己的字符串鍵(Customer_UserId)來構建這種one-to-many關系。

如何使用Customer表列來關聯ContactCustomerId列以使用EF6代碼第一種方法映射關系?

這是完整的代碼:

ApplicationUser

public class ApplicationUser : IdentityUser
{
    //Navigation   properties
    public Customer Customer { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

顧客

public class Customer
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Index(IsUnique = true)]
    public int Id { get; set; }

    [Key, ForeignKey("ApplicationUser")]
    public string UserId { get; set; }

    public string BusinessName { get; set; }

    //Navigation properties
    [ForeignKey("UserId")]
    public ApplicationUser ApplicationUser { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}

聯系

public class Contact
{
    public int Id { get; set; }
    public int CustomerId { get; set; }

    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string OfficeLocation { get; set; }
    //Navigation properties
    [Required]
    public Customer Customer { get; set; }

}

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(): base("DefaultConnection", throwIfV1Schema: false){}

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Customer>()
            .HasRequired(u => u.ApplicationUser).WithRequiredDependent(c => c.Customer);
    }

    public System.Data.Entity.DbSet<WebServer.Models.Customer> Customers { get; set; }

    public System.Data.Entity.DbSet<WebServer.Models.Contact> Contacts { get; set; }
}

我發現刪除顯式外鍵並刪除聯系人的“客戶”屬性會顯着改變架構。

這將刪除Customer_UserId

看起來這就是你所追求的行為。

我想到了。 ApplicationUserCustomer表是one-to one因此兩個表具有相同的PKApplicationUser String IdCustomer String UserId )。 從邏輯上講,我們可以將這兩個表視為一個PK類型為string表。 Contact表是Customer表的子表。 因此, CustomerContact之間one-to-many關系。 因此, Contact表必須具有string類型的Foreign Key 因此模型中的CustomerId應該是string類型而不是int 另外,為了指示代碼優先使用此字段,我需要將[ForeignKey("CustomerId")]屬性添加到public Customer Customer { get; set; } public Customer Customer { get; set; } public Customer Customer { get; set; }屬性。

所以新的Contact模型應該是這樣的

public class Contact
{
   public int Id { get; set; }
   public string CustomerId { get; set; }
   public string LastName { get; set; }
   public string FirstName { get; set; }
   public string OfficeLocation { get; set; }

   //Navigation properties

   [ForeignKey("CustomerId")]
   [Required]
   public Customer Customer { get; set; }
}

PS:在我的原始應用程序中,我在此表層次結構中有另一個級別。 聯系人可以有多個地址,因此Address表可以使用int類型的FKContact s PK形成另一個one-to-many關系。

暫無
暫無

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

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