2

當我們使用Asp.net Boiler plate時,你能告訴我如何設置與下面提到的模型的1:1關係嗎?提前致謝。實體框架與Asp.net鍋爐板的1對1關係

注:我看到this nice answer關於EF 1至1 relationship.But不幸的是我不知道如何與鍋爐plate.B'cos PK自動從ABP.On我的情況採取設置其中兩個表都有int PK。

注2:這裏的屬性和地址模型具有1:1的關係。

地產模式

[Table("IpProperties")] 
    public class Property : FullAuditedEntity 
    { 

     public virtual bool Vacant { get; set; } 

     public virtual Address Address { get; set; } 

} 

地址模式

[Table("IpAddresses")] 
    public class Address : FullAuditedEntity 
    { 

     [Required] 
     [MaxLength(MaxLength)] 
     public virtual string StreetNumber { get; set; } 

     public virtual Property Property { get; set; } 
    } 

回答

1

關係映射應該在你的DbContext的OnModelCreating方法來實現。您的DbContext類將位於EntityFramework文件夾下的EntityFramework項目中。

您可以使用類似以下內容:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    base.OnModelCreating(modelBuilder); 
    modelBuilder.Entity<Property>().HasRequired(e => e.Address).WithOptional(e => e.Property); 
} 

如果AddressProperty財產不能爲空,則.WithOptional()方法可以用WithRequiredDependent()WithRequiredPrincipal()根據使用情況更換。

另一種解決方案:

ABP forum - REFERENTIAL INTEGRITY ISSUE - ONE TO ONE RELATIONSHIP

+1

感謝您的支持:) – Sampath