实体框架4.1树形映射
我在将经典数据库树映射到实体框架实体时遇到问题。
这是我的数据库:
CREATE TABLE intranet_organization_units
(
[OrganizationUnitId] INT IDENTITY PRIMARY KEY,
[ParentOrganizationUnitId] INT NULL,
[Name] NVARCHAR(50) NOT NULL
)
实体:
public class OrganizationUnit
{
public int OrganizationUnitId { get; set; }
public string Name { get; set; }
public int? ParentOrganizationUnitId { get; set; }
public virtual OrganizationUnit ParentOrganizationUnit { get; set; }
public virtual ICollection<OrganizationUnit> ChildrenUnits { get; set; }
}
最后是映射:
public class OrganizationUnitMapper : EntityTypeConfiguration<OrganizationUnit>
{
public OrganizationUnitMapper()
{
ToTable("intranet_organization_units");
Property(x => x.Name).HasColumnName("Name").IsRequired();
HasMany(x => x.ChildrenUnits).WithOptional().HasForeignKey(x => x.OrganizationUnitId);
HasOptional(x => x.ParentOrganizationUnit).WithMany().HasForeignKey(x => x.ParentOrganizationUnitId);
}
}
当我运行应用程序时,我收到此异常:
在模型生成过程中检测到一个或多个验证错误:
System.Data.Edm.EdmAssociationEnd:: 多重性在角色中无效 'OrganizationUnit_ChildrenUnits_Target' 在关系中 'OrganizationUnit_ChildrenUnits'。 因为从属角色指的是 关键属性,上限 从属角色的多重性 必须为“1”。
那么问题出在哪里呢?谢谢
I have problem with mapping classic db tree to entity framework entity.
Here is my db :
CREATE TABLE intranet_organization_units
(
[OrganizationUnitId] INT IDENTITY PRIMARY KEY,
[ParentOrganizationUnitId] INT NULL,
[Name] NVARCHAR(50) NOT NULL
)
entity :
public class OrganizationUnit
{
public int OrganizationUnitId { get; set; }
public string Name { get; set; }
public int? ParentOrganizationUnitId { get; set; }
public virtual OrganizationUnit ParentOrganizationUnit { get; set; }
public virtual ICollection<OrganizationUnit> ChildrenUnits { get; set; }
}
and finally mapping :
public class OrganizationUnitMapper : EntityTypeConfiguration<OrganizationUnit>
{
public OrganizationUnitMapper()
{
ToTable("intranet_organization_units");
Property(x => x.Name).HasColumnName("Name").IsRequired();
HasMany(x => x.ChildrenUnits).WithOptional().HasForeignKey(x => x.OrganizationUnitId);
HasOptional(x => x.ParentOrganizationUnit).WithMany().HasForeignKey(x => x.ParentOrganizationUnitId);
}
}
When I run application, I get this exception :
One or more validation errors were detected during model generation:
System.Data.Edm.EdmAssociationEnd: :
Multiplicity is not valid in Role
'OrganizationUnit_ChildrenUnits_Target'
in relationship
'OrganizationUnit_ChildrenUnits'.
Because the Dependent Role refers to
the key properties, the upper bound of
the multiplicity of the Dependent Role
must be �1�.
So where is problem? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这个:
定义此关联映射时,您说:
Use this:
When defining this association mapping you say: