Linux is using the UBUNTU,DNX version is the 1.0.0-BETA6-12120,EF version is 7.0.0-beta5.
The following are the steps to build a SQL Server database using the entity Framework 7.
Add a reference to the entity Framework 7 in Project.json:
{" dependencies": { "entityframework.sqlserver": "7.0.0-beta5", "Entityframework.commands": "7.0.0- Beta5 " }}
Define entity classes, such as:
namespacecnblogs.aboutus.models{ Public classTabnav { Public intId {Get;Set; } Public stringTitle {Get;Set; } Public stringURL {Get;Set;} Public BOOLIsActive {Get;Set; } }}
Define DbContext, such as:
using Microsoft.Data.Entity; using CNBlogs.AboutUs.Models; namespace cnblogs.aboutus.data{ publicclass efdbcontext:dbcontext { publicgetset;}} }
To add a database connection string in Config.json:
{" Data": { "ConnectionString": "[Database connection string]"} }
Load the configuration in the Config.json in Startup.cs:
Public Startup (iapplicationenvironment appenv) { new Configuration (Appenv.applicationbasepath) . Addjsonfile ("config.json");} Public Get set; }
Note:
1) need to add command space Microsoft.Framework.ConfigurationModel and Microsoft.Framework.Runtime;
2) The problem of no data stores is configured was encountered because the Config.json was not loaded correctly.
To configure EF in Startup.cs:
Public void configureservices (iservicecollection services) { services. Addmvc (); Services. Addentityframework () . Addsqlserver () . Adddbcontext<EfDbContext> (options = options. Usesqlserver (Configuration.get ("data:connectionstring"));} );
Note: You need to reference the namespace Microsoft.Data.Entity.
Add EF command in Project.json to build the database using the migration feature of EF.
{" commands": { "EF": "Entityframework.commands"}
Package Required for installation:
DNU Restore
To build the database with the EF command:
Dnx. EF Migration Add Firstmigrationdnx. EF Migration Apply
Build succeeded!
"Legacy Issues"
The above actions are done using mono-based DNX, and the following problem occurs with CORELCR-based DNX:
System.PlatformNotSupportedException:This platform does not support getting the current color. At System.ConsolePal.get_ForegroundColor () at Microsoft.Data.Entity.Commands.Utilities.ConsoleCommandLogger.WriteVerbose (String message)
This is because the get operation of the Foregroundcolor property is not implemented in Corefx's ConsolePal.Unix.cs.
. NET Cross-platform: Build a database with EF7 on Linux based on ASP. 5