简体   繁体   English

代码第一个循环0..1映射?

[英]Code First circular 0..1 mapping?

I have this model and based on Code First with existing DB: 我有这个模型,并且基于带有现有数据库的Code First:

public class Customer
{
    public int Id {get; set;}
    public int Parent1Id {get; set;}
    public int Parent2Id {get; set;}
    public Customer Parent1 {get; set;}
    public Customer Parent2 {get; set;}
}

Is this the correct way to set the mapping? 这是设置映射的正确方法吗?

this.HasOptional(t => t.Parent1)
    .WithOptionalPrincipal(d => d.Parent1);

this.HasOptional(t => t.Parent2)
    .WithOptionalPrincipal(d => d.Parent2);

And how do I map so that Parent1 navigation property maps to Parent1Id and Parent2 to Parent2Id? 以及如何映射,以便Parent1导航属性映射到Parent1Id,而Parent2映射到Parent2Id?

No, that is not the correct way to set the mapping. 不,那不是设置映射的正确方法。 To set a 0..1<->n relation, use WithMany() . 要设置0..1 <-> n关系,请使用WithMany() HasOptional(...).WithOptional*(...) is for 0..1<->0..1 mappings. HasOptional(...).WithOptional*(...)用于0..1 <-> 0..1映射。

this.HasOptional(t => t.Parent1).WithMany(); // no inverse navigation property
this.HasOptional(t => t.Parent2).WithMany();

If you add a navigation property to get the parents' children, you would specify it like so: 如果添加导航属性以获取父母的孩子,则可以这样指定:

this.HasOptional(t => t.Parent1).WithMany(t => t.Children1); // Children1 is the inverse navigation property of Parent1
this.HasOptional(t => t.Parent2).WithMany(t => t.Children2);

You can also omit WithMany to let Entity Framework figure it out itself, but see below. 您也可以省略WithMany ,让Entity Framework自行解决,但请参见下文。

And how do I map so that Parent1 navigation property maps to Parent1Id and Parent2 to Parent2Id? 以及如何映射,以便Parent1导航属性映射到Parent1Id,而Parent2映射到Parent2Id?

If Entity Framework does not automatically detect this based on the names you've used for your properties (I'm not sure when it can and when it cannot figure it out), you can use the HasForeignKey function: 如果Entity Framework不能根据您用于属性的名称自动检测到此情况(我不确定何时以及何时无法弄清楚),则可以使用HasForeignKey函数:

this.HasOptional(t => t.Parent1).WithMany().HasForeignKey(t => t.Parent1Id);
this.HasOptional(t => t.Parent2).WithMany().HasForeignKey(t => t.Parent2Id);

HasForeignKey() can only be called on the result of WithMany() , so if you need to specify the foreign key properties, you cannot omit WithMany() . HasForeignKey()只能在HasForeignKey()的结果上WithMany() ,因此,如果需要指定外键属性,则不能省略WithMany()

There are two ways to do mapping with Fluent DataAnotacion and Api. 有两种使用Fluent DataAnotacion和Api进行映射的方法。 look sample of DataAnotacion 看一下DataAnotacion的样本

public class Order
{
   [Key]
   public int Id { get; set; }
   public virtual ICollection<OrderOrganisation> OrderOrganisations { get; set; }
}

public class Organisation
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<OrderOrganisation> OrderOrganisations { get; set; }
}

public class OrderOrganisation
{
    [ForeignKey("Order"), Column(Order = 0)]
    public int? OrderId { get; set; }

    [ForeignKey("Organisation"), Column(Order = 1)]
    public int? OrganisationId { get; set; }

   [ForeignKey("OrderId")]
   public virtual Order Order { get; set; }
   [ForeignKey("OrganisationId")]
   public virtual Organisation Organisation { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM