. NET Cross-platform: Build a database with EF7 on Linux based on ASP. 5

Source: Internet
Author: User

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

Related Article

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.