This article transferred from: http://www.cnblogs.com/tdws/p/5874212.html
This article copyright belongs to the blog park and the author Wu Di Common All, welcome reprint, reprint and Reptile Please indicate the blog Park snail original address http://www.cnblogs.com/tdws/p/5874212.html.
At present, all the major forums in China, Daniel's share is to follow the official Microsoft document, the Web layer directly applied EF Core. Of course this is not a problem, because I also want to share from the document. The only thing that is more is calling the DbContext method in the DAL layer. You used the ef6.x, if in the test code you can directly new out, in the formal project development, you control the context thread within the unique, you can also new object. But! you can't do this in EF core.
In ef6.x, you have this in your context class, and there are no parameters in the constructor method.
Perhaps this is the way you create a unique context within a thread .
Reviewing the ef6.x and previous versions, enter the text of this share below. English Official document Address https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html#
First I create a new. NET Core Webapplication,consoleapp and a few. NET Core class libraries, let's start with a Bll,dal two class library.
The solution is as follows:
EF core.sqlserver and design are installed from NuGet in the class library and ConsoleApp and WEBAPI. You can also manage the NuGet console commands through NuGet Visual management. The command is as follows:
Install-package Microsoft.EntityFrameworkCore.SqlServer
Install-package Microsoft.EntityFrameworkCore.SqlServer.Design
You will also need to install the tool using the command below. and modify the Project.json of the Dal Layer Class library project to increase the tools node.
Install-package Microsoft.entityframeworkcore.tools–pre
"Tools": { "Microsoft.EntityFrameworkCore.Tools"1.0.0-preview2-final " }
Because it's DB first, you'll also create your test database, like so.
Next, in the NuGet console where you want to add the DAL layer of EF, execute the following command to connect the database information, and don't forget to modify:
"server= (localdb) \mssqllocaldb;database=appdb; Trusted_connection=true; "Microsoft.entityframeworkcore.sqlserver-outputdir Models
This is my configuration.
"server=2013-20150707dj\sql2012express;database=appdb; Trusted_connection=true; "Microsoft.entityframeworkcore.sqlserver-outputdir Models
Of course the direct execution of this paragraph, you will certainly encounter a similar error. Since your startup project is the Web, currently you have not done the references that the project should have, so it tells you that you can't find DAL.dll.
The workaround is, of course, that the UI reference bll,bll the DAL, regenerate it and execute the command again. And if you're prompted to dotnet restore, then you can do it a bit.
After success you will find that the DAL layer has more than one model folder and maps your database tables out to entity classes, plus the DbContext class.
Next look at how to deal with your dbcontext. Miscrosoft tells us that we need to modify the following code.
void onconfiguring (dbcontextoptionsbuilder optionsbuilder) { #warning to protect potentially Sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?linkid=723263 for guidance on storing connection strings. optionsbuilder.usesqlserver (@ "server=2013-20150707dj\sql2012express;database=appdb; Trusted_connection=true; "); }
This is done after the modification:
//protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) //{// #warning to protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/? Linkid=723263 for guidance on storing connection strings. // Optionsbuilder.usesqlserver (@ "server= 2013-20150707dj\sql2012express;database=appdb; Trusted_connection=true; "); // public Appdbcontext (Dbcontextoptions<appdbcontext> options): base
Make the following changes to your WEBAPI or your MVC Startup.cs. In its Configureservices method, add the following code, connection string still remember to modify:
Services. Addapplicationinsightstelemetry (Configuration); var connection = @ "SERVER=2013-20150707DJ\SQL2012EXPRESS;DATABASE=APPDB; Trusted_connection=true; "
Do the following to add code, look and ef6.x no difference. The only difference is that the DbContext object is automatically injected into the IOC by the. NET Core framework for our constructor dependencies.
Here we operate DbContext successfully in WEB/WEBAPI. So what if you want to operate on the DAL layer? This _dbcontext without the dependency injection, who will give us the object? Own new one, but our constructors are placed there, not simply new to operate.
When you really set out to start the new one, you find that you need a dbcontextoption<appdbcontext> object.
Then we're new to a Dbcontextoption<t> object. You see that the overload requires this parameter.
Overloading says: You typically initialize an instance using the Override Dbcontext.onconfiguring method, or use dbcontextoptionbulider<t> to create an instance. Because the object we want is a generic dbcontextoption<t> object, the overloaded method of the former is not a generic type. It may be that I use the wrong, if you have a good implementation, please leave a suggestion.
1PublicClassUserdal2{3StaticString connection =@"server=2013-20150707dj\sql2012express;database=appdb; Trusted_connection=true;";4static Dbcontextoptions<appdbcontext> dbcontextoption =New dbcontextoptions<appdbcontext>();5static Dbcontextoptionsbuilder<appdbcontext> Dbcontextoptionbuilder =New dbcontextoptionsbuilder<appdbcontext>(dbcontextoption);6 Appdbcontext _dbcontext =New Appdbcontext (Dbcontextoptionbuilder.usesqlserver (connection). Options);7 public int AddUser () 8 { 9 _dbcontext.users.add (new Users {Name = "ws4", email = "[email protected]" }); _dbcontext.savechanges return (); One }
Called in ConsoleApp, adding success
If I share a bit, you have a bit of help, welcome to praise, but also for your own harvest praise.
Long-term sharing, welcome to click below attention. Wish you a happy Mid-Autumn Festival!
Drink a Red Bull and have some tea at night, not sleepy until five o'clock in the morning ... Waist pain. , a graduate student friend often told me, have you ever seen XI ' an in the three o'clock in the morning? Now get some sleep and wake up and ask him if he's seen the five o'clock in the morning Suzhou.
Finally, I have a question, does EF core currently support code first? No documentation is visible.
How is EF core guaranteed to be unique within a context thread? There is no callcontext ..., there is no way to use HttpContext.Current.Item, has anyone manipulated it?
[Turn].net core entity Framework Core How do you create DbContext