Question

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
    }
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top