在 EF7 中使用 dbset 和映射器有什么区别 [英] what is the difference between use dbset and mappers in EF7

查看:20
本文介绍了在 EF7 中使用 dbset 和映射器有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在 Onion Architecture 中使用 .net coreEntityframework 7 !我读了 这个教程,我认为它是学习以下主题的最佳案例.但是本教程的一部分在我的脑海中提出了一个大问题.就像您在此链接页面上看到的一样;在数据层,我们有一些类是我们的模型!!

I started to work with .net core and Entityframework 7 in Onion Architecture! i readed this tutorial and i think its best case for learning following subject. but one part of this tutorial made a big question inside my brain. like what you see at this linked page; at Data Layer we have some classes which are our model!!

public class User : BaseEntity
{
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

public class UserProfile : BaseEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public virtual User User { get; set; }
}

和一些像这样映射到模型之上的类!!

and some class which are mapping above models like this !!

public class UserProfileMap
{
    public UserProfileMap(EntityTypeBuilder<UserProfile> entityBuilder)
    {
        entityBuilder.HasKey(t => t.Id);
        entityBuilder.Property(t => t.FirstName).IsRequired();
        entityBuilder.Property(t => t.LastName).IsRequired();
        entityBuilder.Property(t => t.Address);
    }
}

public class UserMap
{
    public UserMap(EntityTypeBuilder<User> entityBuilder)
    {
        entityBuilder.HasKey(t => t.Id);
        entityBuilder.Property(t => t.Email).IsRequired();
        entityBuilder.Property(t => t.Password).IsRequired();
        entityBuilder.Property(t => t.Email).IsRequired();
        entityBuilder.HasOne(t => t.UserProfile).WithOne(u => u.User).HasForeignKey<UserProfile>(x => x.Id);
    }
}

并且它在DbContext的OnModelCreating方法中使用这个映射器类来在数据库中创建以下模型作为表:

and it use this mapper classes in OnModelCreating method of DbContext for creating following models as table, in database:

public class ApplicationContext : DbContext
{
    public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        new UserMap(modelBuilder.Entity<User>());
        new UserProfileMap(modelBuilder.Entity<UserProfile>());
    }
}

我的大问题是:我们可以对 db 上下文中的每个实体使用 DbSet<> 并避免编写映射器并在 OnModelCreating 方法中实例化它们>dbcontext.为什么本教程没有使用 dbset ?为什么我们必须创建映射器!

My big question is this: we can use DbSet<> for each entities inside the db context and avoiding writing mapper and instantiating them in OnModelCreating method of dbcontext. why this tutorial didnt use dbset ?. why we have to create mappers!

推荐答案

我的大问题是:我们可以对内部的每个实体使用 DbSet<>db 上下文并避免编写映射器并在其中实例化它们dbcontext 的 OnModelCreating 方法.为什么本教程没有使用 dbset?.为什么我们必须创建映射器!

My big question is this: we can use DbSet<> for each entities inside the db context and avoiding writing mapper and instantiating them in OnModelCreating method of dbcontext. why this tutorial didnt use dbset ?. why we have to create mappers!

new UserMap(modelBuilder.Entity()); 基本上是 EF Core 使用 Fluent API 配置和映射实体到 DbSet 的方式.

new UserMap(modelBuilder.Entity<User>()); basically is EF Core way of configuring and mapping Entity to DbSet using Fluent API.

DbSet<>对于db context内的每个实体使用Mapper配置DbSet是一样的.

DbSet<> for each entities inside the db context and using Mapper to configuring DbSet are the same.

在实体框架 6 中,我们使用 EntityTypeConfiguration 并创建映射类,如 this.与数据标注相比,它非常干净,并且遵循单一职责原则.

In Entity Framework 6, we use EntityTypeConfiguration and create mapping classes like this. It is very clean compare to Data Annotation, and follows Single Responsibility Principle.

美妙的是我们只需要 以下代码使用反射自动配置数百个实体.

The beauty is we just need the following code to automatically configure the hundreds of Entities using reflection.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   ...
   var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
        .Where(type => !string.IsNullOrEmpty(type.Namespace) &&
             type.BaseType != null &&
             type.BaseType.IsGenericType &&
             type.BaseType.GetGenericTypeDefinition() == typeof (EntityTypeConfiguration<>));

   foreach (var type in typesToRegister)
   {
      dynamic configurationInstance = Activator.CreateInstance(type);
      modelBuilder.Configurations.Add(configurationInstance);
   }

   base.OnModelCreating(modelBuilder);
}

此外,我们可以使用Entity Framework Power Tools,并创建来自现有数据库的实体和映射配置.将数百个表生成到类中只需要大约几分钟的时间.这为我们节省了大量时间.

In addition, we can use Entity Framework Power Tools, and create Entities and Mapping configuration from existing database. It only takes about a couple of minutes to generate the hundreds of tables into classes. It was a big time saver for us.

遗憾的是,截至今天,EntityTypeConfigurationEF Core 中尚不可用.我认为我们中的很多人仍然喜欢使用带有新 EntityTypeBuilder 的旧方法来将 Mapping 配置保留在 DbContext 之外,尽管它不像我们那样顺利已经在 EF6 中完成.

Unfortunately, EntityTypeConfiguration<T> is not available in EF Core yet as of today. I think a lot of us still like to use the old approach with new EntityTypeBuilder<T> to keep the Mapping configuration outside of DbContext, although it is not as smooth what we have done in EF6.

这篇关于在 EF7 中使用 dbset 和映射器有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆