0

我有一个项目实体和一个 Rfi 实体。项目实体包含一个 TeamMembers 列表。Project 是 Rfi 实体中的导航属性。在 Rfi 实体中有一个 RecipientId。此 Id 代表 TeamMembers 集合中的一个人。所以想象一下,在一个网页上,我们有一个名为 Recipient 的下拉框。该列表包括项目的所有团队成员。用户将从该列表中选择一个联系人。该联系人的 ID 将保存在 RecipientsId 属性中。当页面重新加载时,我们将根据 RecipeintsId 属性中的值在下拉列表中选择该用户的 Id。使用流利的 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
    }
4

1 回答 1

0

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

您需要Recipient导航属性 inRfiRfis导航属性 in Contact。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);
于 2011-05-20T14:57:38.877 回答