实体框架4.1树形映射

发布于 2024-11-09 10:11:17 字数 1450 浏览 0 评论 0原文

我在将经典数据库树映射到实体框架实体时遇到问题。

这是我的数据库:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-11-16 10:11:17

使用这个:

public class OrganizationUnitMapper : EntityTypeConfiguration<OrganizationUnit>
{
    public OrganizationUnitMapper()
    {
        ToTable("intranet_organization_units");
        Property(x => x.Name).HasColumnName("Name").IsRequired();

        this.HasOptional(x => x.ParentOrganizationUnit)
            .WithMany(y => y.ChildrenUnits)
            .HasForeignKey(x => x.ParentOrganizationUnitId);
    }
}

定义此关联映射时,您说:

  • 组织单位有可选的父级
  • 父级可以有多个子级
  • 并且子级使用外键属性

Use this:

public class OrganizationUnitMapper : EntityTypeConfiguration<OrganizationUnit>
{
    public OrganizationUnitMapper()
    {
        ToTable("intranet_organization_units");
        Property(x => x.Name).HasColumnName("Name").IsRequired();

        this.HasOptional(x => x.ParentOrganizationUnit)
            .WithMany(y => y.ChildrenUnits)
            .HasForeignKey(x => x.ParentOrganizationUnitId);
    }
}

When defining this association mapping you say:

  • Organization unit has optional parent
  • Parent can have multiple children
  • And child use property for foreign key
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文