Putting the database model into a class library in. Net Core

Source: Internet
Author: User
Tags dotnet

I. Summary of Prerequisites

One day this year suddenly bored, decided to learn. NET core, the version is now. NET Core 1.1. has been using the. NET Framework to do projects, has been on the html.editfor () and other HTML Helper, feeling the flexibility is too poor, this time after looking at Microsoft Docs document, feel the power of. NET core, especially tag Helper, decided to plunge into the platform, but found a problem in the new project: the previous project was to put the database model in a separate class library, but now found that the. NET Core Class library configuration is not the same, there is always a problem, so began to use the international version of Bing (by the way despise the next degree) search information, Searching for answers in the issues and Stackoverflowk of the ASPNET project group at GitHub has finally found a solution.

Ii. references
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet# targeting-class-library-projects-is-not-supportedhttps://docs.microsoft.com/en-us/ef/core/miscellaneous/ Configuring-dbcontext
http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects/
https://github.com/aspnet/EntityFramework/issues/5320
Iii. Concrete Solutions 1. The new. NET core Web application is named TestMigrationInClassLibrary2, a new. NET Core class library is called NetDomain3, testmigrationinclasslibrary Reference NEtDomain4, in NThe following steps are performed in the Etdomain Class Library: (1) Add Microsoft.EntityFrameworkCore.Tools.DotNet in *.csproj
< ItemGroup >    <  Include= "Microsoft.EntityFrameworkCore.Tools.DotNet"  Version= "1.0.0"  />  </ItemGroup>
(2) Add package Microsoft.EntityFrameworkCore.Design

Using the CMD controller, enter the class Library folder directory using the following command

dotnet Add Package Microsoft.EntityFrameworkCore.Design

The final style of *.csproj after adding these two prerequisites is as follows:

<ProjectSDK= "MICROSOFT.NET.SDK">  <PropertyGroup>    <OutputType>Exe</OutputType>    <targetframework>netcoreapp1.1</targetframework>  </PropertyGroup>  <ItemGroup>    <packagereferenceInclude= "Microsoft.EntityFrameworkCore.Design"Version= "1.1.1"privateassets= "All" />  </ItemGroup>  <ItemGroup>    <dotnetclitoolreferenceInclude= "Microsoft.EntityFrameworkCore.Tools.DotNet"Version= "1.0.0" />  </ItemGroup></Project>
(3) Installing SQL Server with NuGet
Install-package Microsoft.EntityFrameworkCore.SqlServer
(4) Add model
 Public class Course    {        publicintgetset;}          Public string Get Set ; }          Public string Get Set ; }    }
(5) Add DbContext
 Public class Coursedbcontext:dbcontext    {        publicbase(options)        {        }        publicget Set ; }    }
(6) Add Contextfactory
 Public classCoursedbcontextfactory:idbcontextfactory<coursedbcontext>    {         Publiccoursedbcontext Create (dbcontextfactoryoptions options) {varOptionsbuilder =NewDbcontextoptionsbuilder<coursedbcontext>(); Optionsbuilder.usesqlserver ("server= (LocalDb) \\mssqllocaldb;database=testclasslibrary; Trusted_connection=true; Multipleactiveresultsets=true"); return NewCoursedbcontext (optionsbuilder.options); }    }
5. Use the following command to open migration in the directory where the class library is located
Dotnet EF--startup-project. /testmigrationinclasslibrary migrations Add initial-c coursedbcontext

Be aware of the ".. /testmigrationinclasslibrary "is a class library of the entry project, without this entrance, is unable to start migration.

6. Update the database
dotnet EF Database Update--startup-project. /testmigrationinclasslibrary

Also need to add entry engineering Testmigrationinclasslibrary

Iv. Summary

Because the. NET Core class library does not have the same startup functionality as the. NET Standard class library, you must designate other bootable projects as portals, so each time the migration is added "--startup-project XXX".

In order to be able to separate the database model, it still takes a lot of effort, but the other advantages of. NET core are far better than traditional. Net. To get started. NET core, it is recommended to look at Microsoft's official docs and go to GitHub to fork the entire source code.

Putting the database model into a class library in. Net Core

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.