How do you load an existing data project in EF 6.2 with database first methodology into a Web API Core2 Controller?

asp.net-core-2.0 asp.net-web-api2 c# entity-framework entity-framework-core

Question

So I have an EF application that is working great but it is thick client and I wanted to go service based and use Azure and MS's Core 2 framework. I go to build the Web API out of the box. Test it works, it does great. Now let's add a reference to my DataAccess Library that uses EF, bombs out. Oops forgot to add EF to new project through NuGet, do that. It proceeds to tell me it cannot find my reference to the connection string. Okay so I wire up it in the appsettings.json to a local instance database like so (copying from what works in a WPF client project already):

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "ConnectionStrings": {
    "ExpensesEntities": "metadata=res://*/Expenses.csdl|res://*/Expenses.ssdl|res://*/Expenses.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Expenses;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
  }
}

And I change my startup like so:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddScoped(_ => new MoneyEntry.DataAccess.ExpensesEntities());
}

I can see it attempt to get to my context and break saying

 'FileNotFoundException: Could not load file or assembly
 'System.Configuration.ConfigurationManager, 
  Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. 
  The system cannot find the file specified.'

Yet it's breaking on this section of the EF Context

public ExpensesEntities()
        : base("name=ExpensesEntities")
    {
    }

So I am pretty sure I just need to have the reference for the connection string in the right order and I goofed in where I put the connection string or some type of loading of the config. I was following this thread: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

My other concern is can I mix EF Frameworks? I wrote my Data Access Layer in EF 6.1.3 I just upgraded to 6.2 and yet I know .NET Core has it's own libraries. Can I mix EF.NET Core with EF.NET Framework? Or should I try to rewrite my DAL in Core too? Whatever it is, it appears to not like it and I may just try to see if I can add a model self contained from scratch to see how it works. But thought I would ask to see if people had experience blending frameworks on the new .NET Core 2.

EDIT 11-15-17 Okay so following this link https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6#handle-connection-strings is a little helpful. Yet they are doing an MVC site and not an API alone, assuming code first methodology, and also that you want dependency injection. To simplify can you do this with Database first and just get the appsettings to recognize the EF connection string? In the past you just make another project, copy and paste your EF section into the new app config, poof it works. Since ASP.NET Core 2 uses an appsettings I have tried to do a simpler connection string like:

"Expenses": "Server=.;Database=Expenses;Trusted_Connection=True;MultipleActiveResultSets=true"

I use one of my simplest tables that only has an Id int property, and a string Description property. It then bombs on loading the controller when I hit a breakpoint just trying to get the data like so:

 var data = await _context.tdType.ToListAsync();
 return View(data);

and assumes I need to define a key and am using code first. Am I assuming that EF Core cannot handle Model first behavior then and I need to adorn POCOs generated with Code first methodology? If so at this point I may need to recreate my entire EF with code first and consider how to handle procedures and views with it too.

I think the key to understanding this is this model I am using has views, functions, and procedures. From the little I know of code first you can update the mapping options of a DbSet object to override the Insert, Update, and Delete options. But the views, functions and procedures that are not directly related to tables but multiple operations are really kind of a mystery. It may be that EF Core simply is not full featured enough. But for that reason I thought piggy backing an existing EF 6.2 would fix this.

1
3
11/15/2017 6:06:04 PM

Popular Answer

You cannot use an EF 6 context in .Net Core directly, from the docs:

You can't put an EF6 context in an ASP.NET Core project because .NET Core projects don't support all of the functionality that EF6 commands such as Enable-Migrations require.

You can however use EF 6 using another assembly:

The recommended way to use Entity Framework 6 in an ASP.NET Core application is to put the EF6 context and model classes in a class library project that targets the full framework

but do note that it will in effect make you're .Net Core project dependant of the full .Net framework and so it won't be cross platform anymore.

If you're not planning on using .Net core due to the cross platform capabilities and you have invested a lot in your EF 6 code then you might think about creating aps.net full framework web api services so you can rewrite your app from a thick client to a thin client / web api architecture.

3
11/10/2017 6:49:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow