使用默认约定的可选依赖关系的一对一关系 [英] One-to-one relationsip with optional dependent end using default conventions

查看:154
本文介绍了使用默认约定的可选依赖关系的一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个主体实体( Person )与可选的依赖实体( Car )映射使用实体框架代码优先模型中的默认约定。

I'd like to have a principal entity (Person) with optional dependent entity (Car) mapped using the default conventions in Entity Framework code-first model.

一个解决方案在这个答案,它在 modelBuilder 上使用流畅的API映射到密钥。

One solution is described in this answer, which uses the fluent API on modelBuilder to map to the key.

是有可能只使用默认的EF约定吗?以下代码将抛出无法确定Person和Car类型之间关联的主体结束。无效操作异常。

Is it possible to do this using only the default EF conventions? The following code will throw a Unable to determine the principal end of an association between the types Person and Car. invalid operation exception.

public DatabaseContext : DbContext
  public DbSet<Person> Persons { get; set; }
  public DbSet<Car> Cars { get; set; }
  // No OnModelCreating - possible?

public class Person
  public Int32 PersonId { get; set; }
  public Int32? CarId { get; set; } // Optional.
  public virtual Car Car { get; set; }

public class Car
  public Int32 CarId { get; set; }
  public Int32 PersonId { get; set; }
  public virtual Person Person { get; set; }

此示例假设一个人只能与无或一个车相关联。我无法插入没有相关人员的汽车,但我应该能够插入没有相关车辆的人。为什么EF不会弄清楚 CarId 在人身上是否是 Car 的外键,反之亦然?

This example assumes a person can only be associated with none or one cars. I should not be able to insert a car without an associated person, but I should be able to insert a person without an associated car. Why will EF not figure out that CarId on person is a foreign key for Car and vice versa?

我在.NET Framework 4.5上使用Entity Framework 6.1.2。

I am using Entity Framework 6.1.2 on .NET Framework 4.5.

当我尝试使用流畅的API定义关系时,它有效,但是我无法将键映射到现有的列,这就是为什么我要首先使用约定。我得到的例外是类型中的每个属性名称必须是唯一的。

When I attempt to define the relationship using fluent API, it works, but I am unable to map the key to an existing column, which is why I want to use conventions in the first place. The exception I get with this is Each property name in a type must be unique..

modelBuilder.Entity<Person>().HasOptional(p => p.Car).WithRequired(c => c.Person).Map(a => a.MapKey("PersonId"));

如果我省略了 MapKey SaveChanges 成功,但数据库检查显示 Car 行的 PersonId 外键列包含0,而不是1,它是 Person 行中的 PersonId 主键列。 / p>

If I leave out the MapKey call, SaveChanges succeeds, but database inspection reveals that the Car row's PersonId foreign key column contains 0, instead of 1, which is the PersonId primary key column in the Person row.

推荐答案

Entity Framework不支持一对一外键关联。您必须删除外键属性并使用navegation属性。以这种方式映射你的关系:

One-to-one foreign key associations are not supported by Entity Framework. You must remove the foreign key properties and use the navegation properties. Map you relationship this way:

modelBuilder.Entity<Person>().HasOptional(p => p.Car).WithRequired(c => c.Person);

EF将为您创建FK:

EF will create the FK for you:

这是因为实体框架要求将从属(Person)的主键用作外键。

This is because of Entity Framework’s requirement that the primary key of the dependent (Person) be used as the foreign key.

这篇关于使用默认约定的可选依赖关系的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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