0

I google up hell lot but could not get it completely, here I found little useful info but not to success. Below is my question,

I have below classes and need a way to specify the relation in entity model.

public class User
{
    [Key]
    public int UserId { get; set; }
    public int? HomeAddressId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual Address HomeAddress{ get; set; }
}
public class Address
{
    [Key]
    public int AddressId { get; set; }
    public int UserId { get; set; }
    public string AddressLine1 { get; set; } 

    // other properties
    public virtual User User { get; set; }
}

Requirement is each User has many addresses and each address is associated with one User(one-to-many). One of the address is Home Address and hence the User has HomeAddres(optional). HomeAddressId is FK (nullable) to Address table. How do I define this relation in entity model code first preferably in Fluent API method?

Community
  • 1
  • 1
Ravi D
  • 3
  • 1
  • if you made copy your code and paste here.y you have wrong definition User member of Adress. I changed it. and this code should work.additional you could change your model. Addresses collect all address. you can seperate adress by type as AddresType member of Address. – Nuri YILMAZ Sep 08 '16 at 11:45

1 Answers1

0

I prefer to show code my easy way.

   public class User
    {
        [Key]
        public int UserId { get; set; }
        public int? HomeAddressId { get; set; }

        public virtual ICollection<Address> Addresses { get; set; }
        public Address HomeAddress { 
           get {
                if(Addresses != null)
                   return Addresses.FirstOrDefault(p=> p.AddressType.Home);
                return null;
           }
        }
    }
    public enum AddressType
    {
        Unknown,
        Home,
        Office
    }
public class Address
{
    [Key]
    public int AddressId { get; set; }
    public int UserId { get; set; }
    public string AddressLine1 { get; set; } 
    public AddressType AddressType {get; set; }

    // other properties
    public virtual User User { get; set; }
}

Second fluent.

public class User
{
     public int UserId { get; set; }
     public virtual Address HomeAddress { get; set; }
     public virtual int? HomeAddressId { get; set; }
     public virtual ICollection<Address> OfficeAddresses { get; set; }
}

public class Address
{
     public int AddressId { get; set; }
     public string AddressLine1 { get; set; } 
}

public class UserMap : MapEntityTypeConfiguration<User>
{
     public UserMap ()
     {
        //Other definitions PK etc...

        this.HasOptional(o => o.HomeAddress)
                .WithMany()
                .HasForeignKey(a => a.HomeAddressId);

        this.HasMany(t => t.OfficeAddresses)
          .WithMany()
          .Map(m =>
          {
              m.ToTable("UserOfficeAddresses");
              m.MapLeftKey("AddressId");
              m.MapRightKey("UserId");
          });
     }
}
//and define Address Configurations.. MapEntityTypeConfiguration<Address>
Nuri YILMAZ
  • 4,291
  • 5
  • 37
  • 43
  • possibly this will resolve the question but not as per my approach, I don't want to add an extra AddressType field on Address here. The question is to have HomeAddressId on User object and use it. – Ravi D Sep 08 '16 at 13:19
  • i see. it just only simple and easy solution. other akternatives you should use fluent api for define relations. I will try to awswer it. ;) – Nuri YILMAZ Sep 08 '16 at 13:24
  • second what you ask. – Nuri YILMAZ Sep 08 '16 at 13:43