C# 首先将sql视图映射到实体框架代码中已定义的多个实体?

C# 首先将sql视图映射到实体框架代码中已定义的多个实体?,c#,sql,sql-server,entity-framework,ef-fluent-api,C#,Sql,Sql Server,Entity Framework,Ef Fluent Api,我使用代码优先方法fluent API将sql表映射到实体,如下所示: public class ProjectEntities : DbContext { public ProjectEntities () : base("name=ProjectEntities ") { } public DbSet<UserEntity> UserEntity { get; set; }

我使用代码优先方法fluent API将sql表映射到实体,如下所示:

 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());
    }
}
公共类项目实体:DbContext
{
公共项目实体()
:base(“名称=项目实体”)
{
}
公共数据库集用户实体{get;set;}
公共数据库集地址实体{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
添加(新的UserEntityMapper());
添加(新的AddressEntityMapper());
}
}
地图绘制者被定义为:

 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");
}
}
公共类地址EntityMapper:EntityTypeConfiguration
{
公共地址
{
ToTable(“地址”);
HasKey(m=>m.AddressID);
HasRequired(m=>m.UserEntity)。带有多个(b=>b.AddressEntity);
属性(p=>p.AddressKey).HasColumnName(“ADDRESSID”);
属性(p=>p.UserID);
属性(p=>p.Address1).HasColumnName(“Address1”);
属性(p=>p.Address2).HasColumnName(“Address2”);
属性(p=>p.Address3);
房地产(p=>p.City).HasColumnName(“城市”);
属性(p=>p.State);
属性(p=>p.ZipCode);
}
}
和用户映射器:

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");
}
}
公共类UserEntityMapper:EntityTypeConfiguration { 公共用户EntityMapper() { ToTable(“用户”); HasKey(m=>m.UserID); 属性(p=>p.UserID); 属性(p=>p.FirstName).HasColumnName(“FirstName”); 属性(p=>p.LastName); 属性(p=>p.MiddleInitial).HasColumnName(“MiddleInitial”); } } 这东西很好用。现在我们必须改变逻辑来获取信息。 现在,公司决定使用sql视图来获取完整的用户信息,这样您就可以在一个地方获得用户的所有信息。现在我不想重做/创建另一个映射器,它映射来自sql视图的两个表的所有字段

因此,我的问题是,我们是否可以声明另一个映射器,将这两个映射器映射为:

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.
}
}
公共类UserFullEntityMapper:EntityTypeConfiguration { 公共用户FullEntityMapper() { ToTable(“vw_用户”); ///在这里声明自动映射到从上述实体到从sql视图返回的列的字段。 } } 请建议。
谢谢

您应该为视图定义一个新实体:

public class YourViewName
{ 
   // properties from UserEntity
   // properties from AddressEntity
}
并使用以下映射:

public View_mapping:EntityTypeConfiguration<YourViewName>
{
   public View_mapping()
   {
      ToTable("vw_user");
      // other mappings
   }
}
公共视图映射:EntityTypeConfiguration { 公共视图映射() { ToTable(“vw_用户”); //其他映射 } }