Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

EntityFramework Code First under .net core 1.1

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Create a new asp.net core project, and then upgrade the class library referenced by. Net core to 1.1. In this case, you need to manually add a runtimes node under project.json, as follows:

"runtimes": {

"win10-x64": {}

}

Add the following three class libraries (version 1.1) to Nuget at the same time:

Microsoft.EntityFrameworkCore.Design

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.Tools

Next, define DbContext, which is used to generate the database, as follows:

Using Microsoft.EntityFrameworkCore;using System.Collections.Generic;using System.ComponentModel.DataAnnotations Namespace EntityFrameworkDemo.Model {/ Database object / public class PermissionContext: DbContext {public PermissionContext (DbContextOptions opt): base (opt) {} protected override void OnModelCreating (ModelBuilder modelBuilder) {/ / confirm that the two fields in the UserRole table are the joint primary key modelBuilder.Entity () .HasKey (u = > new {u.UserID) U.RoleID}) } public DbSet Users {get; set;} public DbSet Roles {get; set;} public DbSet UserRoles {get; set;} / public class User {[Key] public int ID {get; set;} public string UserName {get; set } public string Password {get;set;} public List UserRoles {get;set;} / public class Role {[Key] public int ID {get;set;} public string RoleName {get;set;} public List UserRoles {get;set }} / user role relationship / public class UserRole {public int UserID {get; set;} public int RoleID {get; set;} public User User {get; set;} public Role Role {get; set;}}

At this point, you need to add data concatenated strings to the StartUp.cs to guide the server and database name when automatically generating the database.

Public void ConfigureServices (IServiceCollection services) {var connection = @ "Server=.;Database=PermissionDb;Trusted_Connection=True;"; services.AddDbContext (options = > options.UseSqlServer (connection)); services.AddMvc ();}

Now, Build the project and execute it in the package Manager console (vs menu tools-NuGet package Manager-package Manager console) with two commands:

Add-Migration MyFirstMigration

Used to generate commands, generate C # code for databases and tables

Update-Database

Execute the generated code

An error will be reported when using Add-MigrationMyFirstMigration. There is no required project configuration file (.json) in netcoreapp1.1. When you open the bin directory, you will find that there will be an extra win10-x64 folder under netcoreapp1.1. This is exactly what we manually added in project.json. Open this folder and copy the corresponding .json inside. (this should be when Add-MigrationMyFirstMigration generates code. The default search configuration file does not always result in the path where we manually add runtimes)

Add-Migration MyFirstMigration again after copying.

You will find that a folder Migrations has been added to the project and two files are generated below, which are the instructions needed to generate the database.

Now execute Update-Database again

When the execution is successful, use SQL Server's administrative tools to view the generated database, and the table relationships in the table are entered below:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report