EF4代码首先与外部实体建立多对多关系

发布于 2024-12-18 16:18:41 字数 1321 浏览 3 评论 0原文

问题

我有两个类:

  • OfficeProfile:存在于我的 EF 模型中
  • BusinessUnit:来自外部源,例如 Web 服务 办公室

配置文件和业务单位之间存在多对多关系,我想用一个链接表OfficeProfilesBusinessUnits。我不想最终得到一个包含业务单位列表的 BusinessUnits 表,因为这些业务单位存储在我无法控制的外部域中。

有可能实现这一目标吗?

public class OfficeProfile
{
    public OfficeProfile()
    {
        ContactNumbers = new HashSet<ContactNumber>();
        BusinessUnits = new HashSet<BusinessUnit>();
    }

    public int OfficeProfileId { get; set; }
    public Address Address { get; set; }
    public ICollection<ContactNumber> ContactNumbers { get; private set; }
    public ICollection<BusinessUnit> BusinessUnits { get; private set; }
}

public class BusinessUnit
{
    public BusinessUnit(string code, string name, BusinessUnit parent)
    {
        Code = code;
        Name = name;
        Parent = parent;
    }

    public string Code { get; set; }
    public string Name { get; set; }
    public BusinessUnit Parent { get; set; }
}

我尝试

过忽略 BusinessUnit 实体,但是忽略了链接表。

我尝试在映射多对多关系时忽略 BusinessUnit 实体,但这会引发 InvalidOperationException 说明

导航属性“BusinessUnits”不是 上声明的属性 输入“Office配置文件”。验证是否未明确排除 来自模型并且它是有效的导航属性

Problem

I've got two classes:

  • OfficeProfile: exists within my EF model
  • BusinessUnit: comes from an external source, e.g. a web service

There is a many to many relationship between office profiles and business units, which I'd like to represent in a link table OfficeProfilesBusinessUnits. I don't want to end up with a BusinessUnits table however that contains a list of business units as these are stored in an external domain outside of my control.

Is it possible to achieve this?

public class OfficeProfile
{
    public OfficeProfile()
    {
        ContactNumbers = new HashSet<ContactNumber>();
        BusinessUnits = new HashSet<BusinessUnit>();
    }

    public int OfficeProfileId { get; set; }
    public Address Address { get; set; }
    public ICollection<ContactNumber> ContactNumbers { get; private set; }
    public ICollection<BusinessUnit> BusinessUnits { get; private set; }
}

public class BusinessUnit
{
    public BusinessUnit(string code, string name, BusinessUnit parent)
    {
        Code = code;
        Name = name;
        Parent = parent;
    }

    public string Code { get; set; }
    public string Name { get; set; }
    public BusinessUnit Parent { get; set; }
}

What I've tried

I've tried ignoring the BusinessUnit entity, however that omits the link table.

I've tried ignoring the BusinessUnit entity whilst mapping the many-to-many relationship, however that throws an InvalidOperationException stating

The navigation property 'BusinessUnits' is not a declared property on
type 'OfficeProfile'. Verify that it has not been explicitly excluded
from the model and that it is a valid navigation property

.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

抹茶夏天i‖ 2024-12-25 16:18:41

您可以添加一个额外的实体,该实体仅具有 ID 和指向业务部门的链接。在实体模型中,您将忽略 BusinessUnit 但添加链接实体,以便获得多对多关系链接表。

You could add an extra entity which would only have an ID and a link to the BusinessUnit. In your Entity Model you would then ignore the BusinessUnit but add the link entity so you get the many to many relation link table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文