我应该双向关系的双方在EF代码首先映射? [英] Should I map both sides of bidirectional relations in EF code first?

查看:137
本文介绍了我应该双向关系的双方在EF代码首先映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下实体类:

public class Customer {
  public int Id { get; set; }
  public virtual ICollection<Order> Orders { get; set; }
}

public class Order {
  public int Id { get; set; }
  public virtual Customer Customer { get; set; }
}



应该如何的那些实体框架6流利代码优先映射映射?我要更明确一些的映射,而不是依赖于自动映射的约定。

How should those be mapped in Entity Framework 6 fluent code-first mapping? I want to be explicit about the mapping and not rely on automatic mapping conventions.

只是地图这两个类的本地属性。 。这是我会怎么做它在功能NHibernate

Just map the local properties of both classes. That's how I would do it in Fluent NHibernate.

public class CustomerMap : EntityTypeConfiguration<Customer> {
  public CustomerMap() {
    HasMany(x => x.Orders);
  }
}

public class OrderMap : EntityTypeConfiguration<Order> {
  public OrderMap() {
    HasRequired(x => x.Customer);
  }
}



选项2


$ ; b $ b

地图在这两个班的关系的双方

Option 2

Map both sides of the relationship in both classes.

public class CustomerMap : EntityTypeConfiguration<Customer> {
  public CustomerMap() {
    HasMany(x => x.Orders).WithRequired(x => x.Customer);
  }
}

public class OrderMap : EntityTypeConfiguration<Order> {
  public OrderMap() {
    HasRequired(x => x.Customer).WithMany(x => x.Orders);
  }
}



选项3



映射关系的两侧,但只在类之一。该代码将类似的选项2,刚才的那两个构造函数之一将是空的。

Option 3

Map both sides of the relation, but only in one of the classes. The code would be similar to option 2, just one of the two constructors would be empty.

是否有任何区别这些选项?如果是的话,还请解释为什么我应该或不使用特定的选项。

Is there any difference between those options? If yes, please also explain why I should or shouldn't use a specific option.

推荐答案

我会去的选项3

在选项1你可以忘记到关联的反向端映射。在这个简单的例子很明显, Order.Customer Customer.Orders 是同一协会的两端。当事情变得更加复杂,这并不总是显而易见的。此外,它是冗余的代码。

In option 1 you can forget to map the inverse end of an association. In this simple example it's clear that Order.Customer and Customer.Orders are two ends of the same association. When things get more complex, this isn't always obvious. Also, it is redundant code.

在选项2你可以有冲突的映射。例如,当你有...

In option 2 you could have conflicting mappings. For instance when you have...

HasOptional(x => x.Customer).WithMany(x => x.Orders);



...在 OrderMap ,你会得到一个运行时异常,告诉你这两个映射不匹配。再次,它是多余的代码。

...in OrderMap, you will get a runtime exception telling you that both mappings don't match. And again, it is redundant code.

所以,选项3是干燥和安全。唯一的问题是,这是一个有点乱在何处配置的映射。我倾向于坚持测绘孩子们在他们父母的映射。

So option 3 is DRY and safe. The only issue is that it's a bit arbitrary where to configure the mappings. I tend to adhere to mapping children in their parent's mapping.

还有一个评论。您可能希望在订单添加一个基本属性客户ID 。该映射是这样的:

One more comment. You may want to add a primitive property CustomerId in Order. The mapping would look like:

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        HasMany(x => x.Orders).WithRequired(x => x.Customer)
                              .HasForeignKey(o => o.CustomerId);
    }
}

现在你有过关联的两端完全控制及要使用的外键的名称。除此之外,也有一些,这些优势的外键关联的而不是独立协会的(没有原始外键属性)。例如,能够建立关联而不必从数据库获取的父对象。您可以只用设置ID值。

Now you have full control over both ends of the association and the foreign key name to be used. Besides that, there are some advantages of these foreign key associations as opposed to independent associations (without a primitive foreign key property). For instance, the ability to establish an association without having to fetch the parent object from the database. You can just by set an Id value.

这篇关于我应该双向关系的双方在EF代码首先映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆