实体框架 4.1 中可能共享的外键(实体中的值对象)的映射建议?

发布于 2024-11-09 01:29:29 字数 1443 浏览 4 评论 0原文

我有一个 Project 实体和一个 Rfi 实体。项目实体包含团队成员列表。项目是 Rfi 实体中的导航属性。在 Rfi 实体中,有一个 RecipientId。此 ID 代表 TeamMembers 集合中的一个人。想象一下,在网页上,我们有一个名为“收件人”的下拉框。该列表包括该项目的所有团队成员。用户将从该列表中选择联系人。该联系人的 ID 将保存在 RecipientsId 属性中。重新加载页面时,我们将根据 RecipeintsId 属性中的值在下拉列表中选择该用户的 Id。使用 Fluent API 在 EF 4.1 中映射此内容的最佳方法是什么?

    public class Project : BaseEntity
    {
        public string ProjectNumber { get; set; }
        public string Description { get; set; }

        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public string Currency { get; set; }


        #region Navigation Properties
        public Guid AddressId { get; set; }
        public virtual Address Address { get; set; }
        public Guid CompanyCodeId { get; set; }
        public virtual CompanyCode CompanyCode { get; set; }
        public virtual ICollection<Contact> TeamMembers { get; set; }
        #endregion

    }


    public class Rfi : Document
    {
        public string Number { get; set; }
        public string Subject { get; set; }
        public string SubcontractorRfiReference { get; set; }
        public string SpecificationSection { get; set; }

        public RfiStatus RfiStatus { get; set; }

        public Guid RecipientId { get; set; }


        #region Navigation Properties
        public Guid ProjectId { get; set; }
        public Project Project { get; set; }
        #endregion
    }

I have a Project entity and an Rfi entity. The project entity contains a list of TeamMembers. Project is a navigation property in the Rfi entity. In the Rfi entity there is a RecipientId. This Id represents a person from the TeamMembers collection. So imagine, on a web page, we have a drop down box named Recipient. The list includes all team members of the Project. The user will select a Contact from that list. The Id of that contact will be saved in the RecipientsId property. When the page is reloaded we will select the Id of that user in the drop down based off the value in the RecipeintsId property. What is the best way to map this in EF 4.1 using the fluent API?

    public class Project : BaseEntity
    {
        public string ProjectNumber { get; set; }
        public string Description { get; set; }

        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public string Currency { get; set; }


        #region Navigation Properties
        public Guid AddressId { get; set; }
        public virtual Address Address { get; set; }
        public Guid CompanyCodeId { get; set; }
        public virtual CompanyCode CompanyCode { get; set; }
        public virtual ICollection<Contact> TeamMembers { get; set; }
        #endregion

    }


    public class Rfi : Document
    {
        public string Number { get; set; }
        public string Subject { get; set; }
        public string SubcontractorRfiReference { get; set; }
        public string SpecificationSection { get; set; }

        public RfiStatus RfiStatus { get; set; }

        public Guid RecipientId { get; set; }


        #region Navigation Properties
        public Guid ProjectId { get; set; }
        public Project Project { get; set; }
        #endregion
    }

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

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

发布评论

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

评论(1

风向决定发型 2024-11-16 01:29:29

据我了解,您的问题是 RfiContect 之间的映射 - 从数据库角度来看,Project 在您的收件人功能中没有任何作用。

您需要 Rfi 中的 Recipient 导航属性或 Contact 中的 Rfis 导航属性。 EF 代码优先 至少在关系的一侧需要导航属性

所以你可以使用类似的东西:

public class Rfi : Document
{
    public string Number { get; set; }
    public string Subject { get; set; }
    public string SubcontractorRfiReference { get; set; }
    public string SpecificationSection { get; set; }

    public RfiStatus RfiStatus { get; set; }

    #region Navigation Properties
    public Guid RecipientId { get; set; }
    public Contact Recipient { get; set; }
    public Guid ProjectId { get; set; }
    public Project Project { get; set; }
    #endregion
}

和地图:

modelBuilder.Entity<Rfi>()
            .HasRequired(r => r.Recipient)
            .WithMany()
            .HasForeignKey(r => r.RecipientId);

As I understand it your problem is mapping between Rfi and Contect - Project doesn't have any role in your Recipient functionality from the database perspective.

You need either Recipient navigation property in Rfi or Rfis navigation property in Contact. EF code first needs navigation property on at least one side of the relation.

So you can use something like:

public class Rfi : Document
{
    public string Number { get; set; }
    public string Subject { get; set; }
    public string SubcontractorRfiReference { get; set; }
    public string SpecificationSection { get; set; }

    public RfiStatus RfiStatus { get; set; }

    #region Navigation Properties
    public Guid RecipientId { get; set; }
    public Contact Recipient { get; set; }
    public Guid ProjectId { get; set; }
    public Project Project { get; set; }
    #endregion
}

And map:

modelBuilder.Entity<Rfi>()
            .HasRequired(r => r.Recipient)
            .WithMany()
            .HasForeignKey(r => r.RecipientId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文