I am using code first approach fluent API to map the sql tables to entities as follows:
public class ProjectEntities : DbContext
{
public ProjectEntities ()
: base("name=ProjectEntities ")
{
}
public DbSet<UserEntity> UserEntity { get; set; }
public DbSet<AddressEntity> AddressEntity { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserEntityMapper());
modelBuilder.Configurations.Add(new AddressEntityMapper());
}
}
And the mapper are delacred as :
public class AddressEntityMapper : EntityTypeConfiguration<AddressEntity>
{
public AddressEntityMapper()
{
ToTable("Address");
HasKey(m => m.AddressID);
HasRequired(m => m.UserEntity).WithMany(b => b.AddressEntity);
Property(p => p.AddressKey).HasColumnName("ADDRESSID");
Property(p => p.UserID).HasColumnName("User_id");
Property(p => p.Address1).HasColumnName("ADDRESS1");
Property(p => p.Address2).HasColumnName("ADDRESS2");
Property(p => p.Address3).HasColumnName("ADDRESS3");
Property(p => p.City).HasColumnName("CITY");
Property(p => p.State).HasColumnName("STATE");
Property(p => p.ZipCode).HasColumnName("ZIPCODE");
}
}
And User mapper :
public class UserEntityMapper : EntityTypeConfiguration<UserEntity>
{
public UserEntityMapper()
{
ToTable("User");
HasKey(m => m.UserID);
Property(p => p.UserID).HasColumnName("UserID");
Property(p => p.FirstName).HasColumnName("FIRSTNAME");
Property(p => p.LastName).HasColumnName("LAST_NAME");
Property(p => p.MiddleInitial).HasColumnName("MIDDLEINITIAL");
}
}
This stuff works fine.Now we have to change the logic to get the information. Now company decided to use sql view for complete user information, so that you will get all the information of user at one place. Now i dont want to redo/create another mapper which maps all the fields form both tables, which are coming from sql view.
So my question is can we declare another mapper which maps both of these mappers as:
public class UserFullEntityMapper : EntityTypeConfiguration<UserEntity, AddressEntity>
{
public UserFullEntityMapper()
{
ToTable("vw_user");
///Declare the auto mapping here to the fields from above entities to columns retuned from sql view.
}
}
Please suggest. Thanks