加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在EF7中使用dbset和mappers有什么区别

发布时间:2020-12-15 23:39:59 所属栏目:百科 来源:网络整理
导读:我开始在洋葱架构中使用.net核心和Entityframework 7!我读了 this教程,我认为它是学习以下主题的最佳案例.但是本教程的一部分在我的脑子里提出了一个大问题.就像你在这个链接页面看到的那样;在数据层我们有一些类是我们的模型!! public class User : BaseEn
我开始在洋葱架构中使用.net核心和Entityframework 7!我读了 this教程,我认为它是学习以下主题的最佳案例.但是本教程的一部分在我的脑子里提出了一个大问题.就像你在这个链接页面看到的那样;在数据层我们有一些类是我们的模型!!

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

和一些类映射上面的模型这样的!!

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方法中使用此映射器类在数据库中创建以下模型作为表:

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

我的大问题是:我们可以使用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!

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

DbSet<>对于db上下文中的每个实体,使用Mapper配置DbSet是相同的.

在Entity Framework 6中,我们使用EntityTypeConfiguration并创建映射类,如this.与Data Annotation相比,它非常干净,并遵循单一责任原则.

美丽是我们只需要the following code使用反射自动配置数百个实体.

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,并从现有数据库创建实体和映射配置.只需几分钟就可以将数百个表生成到类中.这对我们来说是一个很大的节省时间.

不幸的是,EntityTypeConfiguration< T>截至今天,EF Core尚未提供.我认为我们中的很多人仍然喜欢使用旧方法和新的EntityTypeBuilder< T>保持映射配置在DbContext之外,尽管它不像我们在EF6中那样顺利.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读