![](/img/trans.png)
[英]Entity framework 6 Code First one-to-many optional relationship remove child issue
[英]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
表,代碼首先使用這些id
和CustomerId
列生成one-to-many
關系。 由於AspNetuser
表與Customer
具有one-to-one
關系,因此不使用CustomerId
。 但是代碼首先生成自己的字符串鍵(Customer_UserId)
來構建這種one-to-many
關系。
如何使用Customer
表列來關聯Contact
表CustomerId
列以使用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
。
看起來這就是你所追求的行為。
我想到了。 ApplicationUser
和Customer
表是one-to one
因此兩個表具有相同的PK
( ApplicationUser
String Id
和Customer
String UserId
)。 從邏輯上講,我們可以將這兩個表視為一個PK
類型為string
表。 Contact
表是Customer
表的子表。 因此, Customer
和Contact
之間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
類型的FK
和Contact
s PK
形成另一個one-to-many
關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.