How to configure One-to-One or ZeroOrOne-to-One relationships in Entity Framework 7 Code First using Data Annotations or Fluent Api?
相关问题
- Entity Framework Code First Migration
- Entity Framework throws exception - Network Relate
- Do I need to close the DbConnection manually when
- Slow loading first page - ASP.NET MVC
- query and create objects with a one to many relati
相关文章
-
EF6 DbSet
returns null in Moq - Entity Framework 4.3.1 failing to create (/open) a
- EF6 code first: How to load DbCompiledModel from E
- Why do I need a ToList() to avoid disposed context
- Code-First Add-Migration keeps adding the same col
- EF Codefirst validate unique property?
- EF Core 'another instance is already being tra
- Add XML documentation / comments to properties/fie
You can define OneToOne relationship using Fluent API in Entity Framework 7 as below
The above answer is absolutely correct.
Just for readers information: This has been explained nicely in official documentation
One-to-one
One to one relationships have a reference navigation property on both sides. They follow the same conventions as one-to-many relationships, but a unique index is introduced on the foreign key property to ensure only one dependent is related to each principal.
Note
EF will choose one of the entities to be the dependent based on its ability to detect a foreign key property. If the wrong entity is chosen as the dependent, you can use the Fluent API to correct this.