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

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

问题描述

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

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 用于在数据库中创建表的以下模型:

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上下文中每个实体的c $ c> DbSet<> ,避免编写映射器并在<$ c $的 OnModelCreating 方法中实例化它们c> 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!

推荐答案


我的大问题是:我们可以对$ b中的每个实体使用DbSet<> $ b的数据库上下文,避免编写映射器,并在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< User>()); 基本上是使用Fluent API将实体配置和映射到DbSet的 EF Core 方法。

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

db上下文中每个实体的DbSet<> 使用Mapper来配置DbSet 都是相同的。

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

在Entity Framework 6中,我们使用 EntityTypeConfiguration 并创建诸如。与数据注释相比,它很干净,并且遵循单一责任原则

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);
}

此外,我们可以使用实体框架电动工具,然后从现有数据库创建实体和映射配置。只需花费几分钟,便可以将数百个表生成类。

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.

不幸的是,中没有 EntityTypeConfiguration< T> 截至今天为止, EF Core 。我认为我们很多人仍然喜欢将旧方法与新的 EntityTypeBuilder< T> 结合使用,以将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和mappers有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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