what is the difference between use dbset and mappers in EF7

╄→гoц情女王★ 提交于 2019-12-04 10:47:32

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>()); basically is EF Core way of configuring and mapping Entity to DbSet using Fluent API.

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

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.

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.

I'll address only a part of your "big question", that probably will change your initial assumption:

why this tutorial didnt use dbset? why we have to create mappers?

You've missed a huge point. Check the tutorial once more and look for this piece of code:

public class Repository<T> : IRepository<T> where T : BaseEntity
{
    private readonly ApplicationContext context;
    private DbSet<T> entities;   <---- HHHEEEERRREEEE

I've marked it for you. Yes. The tutorial indeed uses DbSet<>. The fact that they wrap the dbsets into "repository" is just a convenience and formalism of onion architecture: they expose "IRepository" from the layer instead of exposing DbSets directly - just to keep the reference to EntityFramework at bay and not let any other layers know that EF is used internally. But that does not mean that they are not useing DbSets. Clearly the Repository uses DbSets.

This is because DbSet and Mappings are totally different things. The Mappings define how to map classes/properties to tables/columns and also add some metadata like which column should be not-null (= properites should be 'required'), which/what indexes to create, etc.

But all of that mappings gives you NO way to download/insert/update the records. It only defines how the database and classes look like.

For executing operations, here comes DbSet. It defines methods that operate on the data defined by mappers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!