如何在類庫項目中正確定義DBContext?


我正在使用實體框架7並創建了兩個項目。一個項目作為ASP.NET 5 Web API項目,另一個是類庫項目(包),我希望將所有數據訪問層邏輯存儲在其中。這樣我就可以將此包用於另一個報告項目,我可以提供的其他服務。

基本上我在web api項目的控制器中有一個簡單的帖子,它在我的數據庫項目中調用一個函數。當函數啟動數據庫時,它表示數據庫未定義,即使它在兩個項目中都已定義。


錯誤

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

數據庫項目:InsertPerson

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

配置文件

Web API項目:Startup.cs

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

Web API項目:appsettings.json

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

Web API項目:project.json

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

數據庫項目:Startup.cs

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

數據庫項目:appsettings.json

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

數據庫項目:AppContext.cs

{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}

一般承認的答案

看起來您在示例代碼中的問題是您正在新建AppContext實例而不是從ServiceProvider解析它。以這種方式初始化DbContext時,將為上下文隱式創建一個新的ServiceProvider,並且該上下文不會滿足Startup.cs中的任何配置。如果您在OnConfiguring中配置上下文並且對應用程序的其餘部分中的依賴項注入不感興趣,那麼這是您希望使用的模式。

在這種情況下,我希望您通過構造函數注入獲取AppContext。對於這種解決方案,您還需要在ServiceCollection中註冊數據訪問層類。結果應該看起來更像這樣:

數據庫項目:AppContext.cs

namespace DataLibrary
{
    public class AppContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<tbl_Person> tbl_Person { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            new tbl_PersonMap(builder.Entity<tbl_Person>());
            base.OnModelCreating(builder);
        }
    }
}

數據庫項目:PersonH​​elper.cs

namespace DataLibrary
{
    public class AppContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<tbl_Person> tbl_Person { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            new tbl_PersonMap(builder.Entity<tbl_Person>());
            base.OnModelCreating(builder);
        }
    }
}

Web API項目:Startup.cs

namespace DataLibrary
{
    public class AppContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<tbl_Person> tbl_Person { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            new tbl_PersonMap(builder.Entity<tbl_Person>());
            base.OnModelCreating(builder);
        }
    }
}

Web API項目:MyController.cs

namespace DataLibrary
{
    public class AppContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<tbl_Person> tbl_Person { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            new tbl_PersonMap(builder.Entity<tbl_Person>());
            base.OnModelCreating(builder);
        }
    }
}

您甚至可以考慮在IServiceCollection上為數據訪問層類添加擴展方法,以幫助減少配置中的重複,尤其是在添加更多常用服務時。


熱門答案

我找到了一種方法來做到這一點,但我仍然不相信這是最好的方法。在包含我的插入函數的類文件中,我添加了這個,以便當從web api項目調用它時,來自web api的appcontext被傳遞給數據庫項目。話雖如此,我不確定是否有更優雅的方法

public class fnCommon
{
    private readonly AppContext db;

    public fnCommon(AppContext context)
    {
        this.db = context;
    }

    public int InsertPerson(tbl_Person person)
    {

        try
        {
            db.tbl_Person.Add(person);
            db.SaveChanges();
            return person.PersonID;
        }

        catch
        {
            return 0;
        }
    }
}




許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因