简体   繁体   English

.Net核心实体框架抽象

[英].Net Core Entity Framework Abstraction

I have an .Net Core Class Library project which has a context file that uses Entity Framework . 我有一个.Net Core Class Library项目,该项目具有使用Entity Framework的上下文文件。 The context file looks like this; 上下文文件如下所示;

public partial class Qcrr : DbContext
{
    public virtual DbSet<AirWorthyNess> AirWorthyNess { get; set; }
    public virtual DbSet<AwLog> AwLog { get; set; }
    public virtual DbSet<AwserialNumbers> AwserialNumbers { get; set; }
    public virtual DbSet<EmployeeInformation> EmployeeInformation { get; set; }
    public virtual DbSet<Password256> Password256 { get; set; }
    public virtual DbSet<Persistant> Persistant { get; set; }
    public virtual DbSet<TblApplicationRights> TblApplicationRights { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"my connection string");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        /* my tables
        .
        .
        .

        */
    }
}

Now, I also have a simple .Net Core Console Application that references the class library dll for testing purposes. 现在,我还有一个简单的.Net Core Console应用程序 ,该类引用了类库dll以进行测试。 I want to be able to use the EF DbSet functionality as it is , within my console app without having to reference EF again. 我希望能够在控制台应用程序中原样使用EF DbSet功能,而不必再次引用EF。 Of course if I try to use the context file directly, I have the following error; 当然,如果我尝试直接使用上下文文件,则会出现以下错误;

在此处输入图片说明

Its telling me to reference Entity Framework again. 它告诉我再次引用实体框架。

So my question is, what would be the best practice approach to abstract my dbcontext which lies within my Class Library ? 所以我的问题是,什么是提取类库中dbcontext的最佳实践方法?

Would I need to implement the DbContext functionalities (scuh as Add, AddRange, Delete, IQueryable implementations (Select, Where, First...))? 我是否需要实现DbContext功能(如Add,AddRange,Delete,IQueryable实现(Select,Where,First ...))?

Entity framework is already a good abstraction for DB. 实体框架已经是数据库的良好抽象。 if You add another layer, you always will be forced to add more and more functionality to your abstraction. 如果添加另一层,则总是被迫向您的抽象添加越来越多的功能。

Good example is AsNoTracking function for EF. EF的AsNoTracking函数就是一个很好的例子。 It is good for performance, but adding it's implementation to your abstraction without re-implementing whole EF logic is a big deal. 它对性能有好处,但是在不重新实现整个EF逻辑的情况下将其实现添加到您的抽象中很重要。 Another example is Spatial data types, raw SQL queries and much more interesting functions that your business logic might require, but you can't use them, because of your abstractions. 另一个示例是空间数据类型,原始SQL查询以及您的业务逻辑可能需要的更多有趣功能,但是由于抽象,您不能使用它们。 So don't add another layer. 因此,请勿添加其他层。

In my opinion the solution is to use Repository pattern. 我认为解决方案是使用存储库模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM