简体   繁体   中英

Entity Framework mapping one class across two tables with a condition

I'm trying to map one class to two database tables. I have a Person Class that maps to a Person table and an Address table. My problem is I only want to select the Primary Address Line (IsPrimary = 1) of the Address table and I can't find or figure out how to do this.

I'm using Entity Framework v6 CodeFirst and I trying to create the EntityTypeConfiguration<T> class

The domain class is...

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // Primary Address
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string AddressPostCode { get; set; }
}

and the database tables look like this...

Table: Person
Int PersonId (PK)
Varchar(20) FirstName Not Null
Varchar(20) LastName Not Null

Table: Address
Int         AddressId     (PK)
Int         PersonId      (FK)
Varchar(25) AddressLine1  Null
Varchar(25) AddressLine2  Null
Varchar(25) AddressLine3  Null
Varchar(10) PostCode      Not Null
Bit         IsPrimary     Not Null

Basically one Person can have multiple addresses but only One Primary Address.

If you are using Entity Framework, probably you have these following classes with a one to many relation:

public class PersonEntity
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<AddressEntity> Addresses { get; set; }
}

public class AddressEntity
{
    public int AddressId  { get; set; }  
    public int PersonId { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string AddressPostCode { get; set; }
    public bool   IsPrimary{ get; set; }
}

And as you mentioned in you'r question, this is the person viewmodel:

public class PersonViewModel
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // Primary Address
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string AddressPostCode { get; set; }
}

Finally you can use the following linq code:

var result = ctx.PersonEntity
.Where(c => c.Addresses.FirstOrDefault(X = > X.IsPrimary == true)).ToList();

var personViewModels = result
.Select(c => new PersonViewModel{
PersonId = c.PersonId ,
FirstName = c.FirstName , 
LastName = c.LastName,  
AddressLine1 = c.Addresses != null ? c.Addresses.FirstOrDefault().AddressLine1 : String.Empty , 
AddressLine2 = c.Addresses != null ? c.Addresses.FirstOrDefault().AddressLine2 : String.Empty ,  
AddressLine3 = c.Addresses != null ? c.Addresses.FirstOrDefault().AddressLine3 : String.Empty  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM