简体   繁体   English

首先将sql视图映射到Entity Framework Code中已定义的多个实体吗?

[英]Map a sql view to already defined multiple entities in Entity Framework Code first?

I am using code first approach fluent API to map the sql tables to entities as follows: 我正在使用代码优先方法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());
    }
}

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. 现在公司决定使用sql视图来获取完整的用户信息,以便您将所有用户信息集中在一个地方。 Now i dont want to redo/create another mapper which maps all the fields form both tables, which are coming from sql view. 现在,我不想重做/创建另一个映射器,该映射器映射来自两个表的所有字段,这些表均来自sql视图。

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 谢谢

You should define a new entity for your view: 您应该为视图定义一个新实体:

public class YourViewName
{ 
   // properties from UserEntity
   // properties from AddressEntity
}

And use following mapping: 并使用以下映射:

public View_mapping:EntityTypeConfiguration<YourViewName>
{
   public View_mapping()
   {
      ToTable("vw_user");
      // other mappings
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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