【问题标题】:what is the difference between use dbset and mappers in EF7在 EF7 中使用 dbset 和 mappers 有什么区别
【发布时间】:2017-12-23 06:55:25
【问题描述】:

我开始在Onion Architecture 中使用.net coreEntityframework 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>());
    }
}

我的大问题是:我们可以为 db 上下文中的每个实体使用DbSet&lt;&gt;,并避免在dbcontextOnModelCreating 方法中编写映射器和实例化它们。为什么本教程没有使用 dbset ?为什么我们必须创建映射器!

【问题讨论】:

  • 请注意,不再有 EF7,它被重命名为 EF Core 1.0,因为它是完全重写,而不是新的迭代。
  • 这个问题对于 Stack Overflow 来说过于宽泛且基于意见。

标签: c# asp.net-core-mvc .net-core entity-framework-core onion-architecture


【解决方案1】:

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

new UserMap(modelBuilder.Entity&lt;User&gt;()); 基本上是 EF Core 使用 Fluent API 配置实体并将其映射到 DbSet 的方式。

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

在 Entity Framework 6 中,我们使用 EntityTypeConfiguration 并创建像 this 这样的映射类。与数据注释相比,它非常干净,并且遵循单一责任原则

美妙之处在于我们只需要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&lt;T&gt; 目前在 EF Core 中不可用。我认为我们中的很多人仍然喜欢使用旧方法和新的EntityTypeBuilder&lt;T&gt; 来将映射配置保留在DbContext 之外,尽管它不像我们在 EF6 中所做的那样顺利。

【讨论】:

  • 这在很多方面都提供了丰富的信息,但是 DbSet 和 Mappings 一点也不相似。一个提供查询/命令执行,另一个提供元数据。当然,DbSet 使用元数据(这些元数据可以由您提供的内容提供,或者通过手动代码优先提供,或者从 XML 加载,或者(...)),但是,由于一个使用另一个,它完全不一样.
【解决方案2】:

我将只解决您“大问题”的一部分,这可能会改变您最初的假设:

为什么本教程没有使用 dbset?为什么我们必须创建映射器?

你错过了一个巨大的点。再次查看教程并寻找这段代码:

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

我已经给你做了标记。是的。本教程确实使用了 DbSet。他们将 dbset 包装到“存储库”中的事实只是洋葱架构的一种方便和形式:它们从层公开“IRepository”而不是直接公开 DbSet——只是为了保持对 EntityFramework 的引用而不让任何其他层知道EF是内部使用的。但这并不意味着他们没有使用 DbSet。显然 Repository 使用 DbSet。

这是因为 DbSet 和 Mappings 是完全不同的东西。映射定义了如何将类/属性映射到表/列,还添加了一些元数据,例如哪些列不应该为空(= 属性应该是“必需的”)、要创建哪些/哪些索引等。

但是所有这些映射让您无法下载/插入/更新记录。它只定义了数据库和类的外观。

对于执行操作,这里有 DbSet。它定义了对映射器定义的数据进行操作的方法。

【讨论】:

    猜你喜欢
    • 2014-07-27
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2014-07-25
    • 2010-11-26
    相关资源
    最近更新 更多