实体框架1到1关系使用代码第一。怎么样? [英] Entity Framework 1 to 1 relationship using code first. how?
问题描述
{
public int RegionId {get;组; }
public string Name {get;组; }
public virtual Factory _factory {get;组; }
}
public class Factory
{
public int FactoryId {get;组; }
public string Name {get;组; }
public virtual Region _region {get;组;
}
当我尝试这个,我得到这个错误:
多重在关系Region_Factory中的角色Region_Factory_Source中无效。因为从属角色属性不是关键属性,所以依赖角色的多重性的上限必须是'*'。
p>由于是virtual关键字,在CodeFirst中出现这种情况。实际上,您正在创建一个关系,其中创建一个项目需要创建另一个项目。然而,virtual关键字允许延迟实例化,这意味着创建一个类型的对象不会自动创建其他类型,允许外部项目上的Id为null。这意味着0..1关系,但是由于每一边都是虚拟的,所以你得到的是0..0,这是不允许的。
有两种方法您可以使用它来补救这种情况。
- 从导航属性的一侧或两侧删除虚拟选项,允许0..1或1..1地图。
- 从每个对象上的其他实体显式添加外键的属性。即在
区域
添加一个属性FactoryId
和工厂
添加一个属性RegionId
还有其他方法可以帮助实体框架确定哪个对象是从属对象,即使用实体框架流畅api。
从 MSDN
配置两端需要的关系(一对一)
在大多数情况下,实体框架可以推断哪个类型是依赖关系,哪个是关系中的主体。但是,当需要关系的两端或双方都是可选的时,实体框架无法识别依赖和主体。当需要关系的两端时,在HasRequired方法之后使用WithRequiredPrincipal或WithRequiredDependent。当关系的两端都是可选的时,在HasOptional方法之后使用WithOptionalPrincipal或WithOptionalDependent。
以下代码将创建一个Principal 工厂
与依赖区域
//配置区域的主键
modelBuilder.Entity< Region>()
.HasKey(t => t.RegionId);
modelBuilder.Entity< Factory>()
.HasRequired(t => t.Region)
.WithRequiredPrincipal(t => t.Factory);
I have two classes. How can I turn these two classes into a one to one relationship using the entity framework code first method?
public class Region
{
public int RegionId { get; set; }
public string Name { get; set; }
public virtual Factory _factory { get; set; }
}
public class Factory
{
public int FactoryId { get; set; }
public string Name { get; set; }
public virtual Region _region { get; set; }
}
When I try this, I get this error: Multiplicity is not valid in Role 'Region_Factory_Source' in relationship 'Region_Factory'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
This occurs in CodeFirst because of the virtual keyword. In effect, you are creating a relationship where creating one item requires the creation of the other. however, the virtual keyword allows lazy instantiation, which means that creating an object of one type doesn't automatically create the other type, allowing the Id on the foreign item to be null. This implies a 0..1 relationship, but since each side is virtual, what you get is a 0..0 which isn't allowed.
There are 2 methods which you can use to remedy the situation.
- remove the virtual option from either one side or both sides of the navigation properties, allowing for a 0..1 or a 1..1 map.
- explicitly add a property for the Foreign key from the other entity on each object. i.e. on class
Region
add a property forFactoryId
and onFactory
add a property forRegionId
There are other ways to help Entity Framework determine which object is the Dependent Object, i.e. using Entity Framework Fluent api.
from MSDN
Configuring a Relationship Where Both Ends Are Required (One-to-One)
In most cases the Entity Framework can infer which type is the dependent and which is the principal in a relationship. However, when both ends of the relationship are required or both sides are optional the Entity Framework cannot identify the dependent and principal. When both ends of the relationship are required, use WithRequiredPrincipal or WithRequiredDependent after the HasRequired method. When both ends of the relationship are optional, use WithOptionalPrincipal or WithOptionalDependent after the HasOptional method.
the following code would create a Principal Factory
with a Dependent Region
// Configure the primary key for the Region
modelBuilder.Entity<Region>()
.HasKey(t => t.RegionId);
modelBuilder.Entity<Factory>()
.HasRequired(t => t.Region)
.WithRequiredPrincipal(t => t.Factory);
这篇关于实体框架1到1关系使用代码第一。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!