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