如何为ASP.net Core配置Entity Framework 6

时间:2021-08-14 08:09:01

I'm trying to configure a new project to use Entity Framework 6 with ASP.net Core, I'm using the full .net framework in order to be able to use Entity Framework 6. This is a project that was in MVC before and I need to migrate it to Core. This is what I've done(I have two projects, one Asp.net Core and a class library that contains some classes and the DBContext class):

我正在尝试配置一个新项目以使用带有ASP.net Core的Entity Framework 6,我正在使用完整的.net框架以便能够使用Entity Framework 6.这是一个在MVC之前的项目我需要将它迁移到Core。这就是我所做的(我有两个项目,一个Asp.net Core和一个包含一些类和DBContext类的类库):

appsettings.json:

appsettings.json:

  "Data": {
    "ConnectionStrings": {
      "MyConnection": "Server=namedatabase.database.secure.windows.net,1433;Database=database_Name;User ID=blahblah;Password=blahblah;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
    }
  }

Startup.cs

Startup.cs

 public void ConfigureServices(IServiceCollection services)
        {
         services.AddScoped<MyDBContext>(_ => new LIGDataContext(Configuration.GetConnectionString("MyConnection")));
        }

I have the DBContext class separated in a class library(.dll):

我在类库(.dll)中分隔了DBContext类:

  public class MyDBContext: DbContext
    {
 public MyDBContext(string connString): base(connString)
        {
        }

But I have this code inside some controllers of my asp.net Core project and I'm not sure how to refer now to the DBContext instance... Clearly is asking for the connString parameter, this is the first time I'm doing this and I'm not sure which is the best approach to do this, ConfigurationManager is not available anymore in Core what do I need to do? This is Entity Framework 6 no Entity Framework Core..

但我在我的asp.net Core项目的某些控制器中有这个代码,我不知道如何现在引用DBContext实例......显然是要求connString参数,这是我第一次这样做我不确定哪种方法最好,在Core中不再提供ConfigurationManager我需要做什么?这是Entity Framework 6没有实体框架核心..

     public class MyController : Controller
        {
           public ActionResult MyAction()
              {
                  var _ctx = new MyDBContext();
                  return PartialView(_ctx.FormsiteApplications.Where(f => f.Application).OrderBy(f => f.Title).ToList());
              }

1 个解决方案

#1


0  

dime2lo is right, you can inject the DbContext to the controller.

dime2lo是对的,你可以将DbContext注入控制器。

codepublic class StoreController : Controller
{
    private DefaultDbContext _dbContext = null;
    public StoreController(DefaultDbContext dbContext)
    {
        this._dbContext = dbContext;
        //Do something with this._dbContext ...
    }
}

or create a context factory

或创建一个上下文工厂

public class DefaultDbContextFactory : IDbContextFactory<DefaultDbContext>{
public DefaultDbContext Create()
{
    return new DefaultDbContext("Server=XXXX ;Database=XXXXX;Trusted_Connection=True;");
}

Reference : Getting Started with ASP.NET Core and Entity Framework 6

参考:ASP.NET核心和实体框架入门6

#1


0  

dime2lo is right, you can inject the DbContext to the controller.

dime2lo是对的,你可以将DbContext注入控制器。

codepublic class StoreController : Controller
{
    private DefaultDbContext _dbContext = null;
    public StoreController(DefaultDbContext dbContext)
    {
        this._dbContext = dbContext;
        //Do something with this._dbContext ...
    }
}

or create a context factory

或创建一个上下文工厂

public class DefaultDbContextFactory : IDbContextFactory<DefaultDbContext>{
public DefaultDbContext Create()
{
    return new DefaultDbContext("Server=XXXX ;Database=XXXXX;Trusted_Connection=True;");
}

Reference : Getting Started with ASP.NET Core and Entity Framework 6

参考:ASP.NET核心和实体框架入门6