Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在EF7中使用dbset和mappers有什么区别_C#_Asp.net Core Mvc_.net Core_Entity Framework Core_Onion Architecture - Fatal编程技术网

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

C# 在EF7中使用dbset和mappers有什么区别,c#,asp.net-core-mvc,.net-core,entity-framework-core,onion-architecture,C#,Asp.net Core Mvc,.net Core,Entity Framework Core,Onion Architecture,我开始在洋葱架构中使用.netcore和Entityframework 7!我读了教程,我认为它是学习以下主题的最佳案例。但本教程的一部分在我的脑海里提出了一个大问题。就像你在这个链接页面看到的一样;在数据层,我们有一些类是我们的模型 public class User : BaseEntity { public string UserName { get; set; } public string Email { get; set; } public string Pa

我开始在
洋葱架构
中使用
.netcore
Entityframework 7
!我读了教程,我认为它是学习以下主题的最佳案例。但本教程的一部分在我的脑海里提出了一个大问题。就像你在这个链接页面看到的一样;在数据层,我们有一些类是我们的模型

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);
    }
}
我的大问题是:我们可以对数据库上下文中的每个实体使用
DbSet
,避免在
dbcontext
OnModelCreating
方法中编写映射器并实例化它们。为什么本教程没有使用dbset?。为什么我们要创建映射器

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

newusermap(modelBuilder.Entity())基本上是使用Fluent API配置实体并将其映射到数据库集的核心方法

数据库上下文中每个实体的数据库集与使用映射器配置数据库集的数据库集是相同的

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

美妙之处在于我们只需要使用反射自动配置数百个实体

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);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
...
var typesToRegister=Assembly.getExecutionGassembly().GetTypes()
.Where(type=>!string.IsNullOrEmpty(type.Namespace)&&
type.BaseType!=null&&
type.BaseType.IsGenericType&&
type.BaseType.GetGenericTypeDefinition()==typeof(EntityTypeConfiguration));
foreach(TypeStoreRegister中的变量类型)
{
dynamic configurationInstance=Activator.CreateInstance(类型);
modelBuilder.Configurations.Add(configurationInstance);
}
基于模型创建(modelBuilder);
}
此外,我们还可以使用现有数据库创建实体和映射配置。将数百个表生成到类中只需要几分钟。这对我们来说是一个很大的时间节省


不幸的是,
entitypeconfiguration
到目前为止在EF-Core中还不可用。我认为我们中的许多人仍然喜欢使用新的
EntityTypeBuilder
的旧方法来将映射配置保持在
DbContext
之外,尽管它不像我们在EF6中所做的那样平滑。

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

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

你错过了一个重要的点。再次查看教程并查找以下代码:

public class Repository<T> : IRepository<T> where T : BaseEntity
{
    private readonly ApplicationContext context;
    private DbSet<T> entities;   <---- HHHEEEERRREEEE
公共类存储库:IRepository其中T:BaseEntity
{
私有只读应用程序上下文上下文;

私有数据库集实体;请注意,已经没有EF7了,它被重命名为EF Core 1.0,因为它是一个完全重写,而不是一个新的迭代。这个问题太广泛,而且基于观点,不利于堆栈溢出。这在很多方面都是有用的,但数据库集和映射在任何方面都不相似。一个提供查询/命令执行,另一个提供元数据。当然,DbSet使用元数据(这些元数据可以由您展示的内容提供,也可以先通过手动代码提供,或者从XML加载,或者(…),但是,由于其中一个使用另一个,所以完全不同。
public class Repository<T> : IRepository<T> where T : BaseEntity
{
    private readonly ApplicationContext context;
    private DbSet<T> entities;   <---- HHHEEEERRREEEE