博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EntityFramework Core笔记:入门(1)
阅读量:7282 次
发布时间:2019-06-30

本文共 1832 字,大约阅读时间需要 6 分钟。

1. 安装运行环境

  EntityFramework Core运行环境,安装NuGget包:

//Sql Server Database ProviderPM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
//提供Add-Migration,Update-Database等Powershell命令PM> Install-Package Microsoft.EntityFrameworkCore.Tools

2. 控制台程序

2.1 基础代码

  实体类:Role.cs

using System;using System.Collections.Generic;using System.Text;namespace Libing.App.Models.Entities{    public class Role    {        public int RoleID { get; set; }        public string RoleName { get; set; }    }}
Role.cs

  DbContext.cs

using System;using System.Collections.Generic;using System.Text;using Microsoft.EntityFrameworkCore;using Libing.App.Models.Entities;namespace Libing.App.Models{    public class LibingContext : DbContext    {        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        {            // 数据库连接            optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=Libing;Integrated Security=True;");        }        protected override void OnModelCreating(ModelBuilder modelBuilder)        {        }        public DbSet
Roles { get; set; } }}

2.2 生成表结构

PM> Add-Migration InitialCreate
PM> Update-Database

2.3 运行代码

using System;using Libing.App.Models;using Libing.App.Models.Entities;namespace Libing.App{    class Program    {        static void Main(string[] args)        {            using (var context = new LibingContext())            {                var role = new Role                {                    RoleName = "管理员"                };                context.Roles.Add(role);                context.SaveChanges();            }        }    }}

  执行的SQL语句:

exec sp_executesql N'SET NOCOUNT ON;INSERT INTO [dbo].[Role] ([RoleName])VALUES (@p0);SELECT [RoleID]FROM [dbo].[Role]WHERE @@ROWCOUNT = 1 AND [RoleID] = scope_identity();',N'@p0 nvarchar(100)',@p0=N'管理员'

转载地址:http://pykjm.baihongyu.com/

你可能感兴趣的文章
ClassLoader
查看>>
将战略性的内容策略融入进网页设计中
查看>>
C/C++内存泄漏及检测
查看>>
转载 Spring Boot中启动HTTPS
查看>>
iOS地图 mapView.region.span.latitudedelta 属性
查看>>
Mysql 内部结构 / Replication | Binlog格式
查看>>
蚁群优化算法
查看>>
phpstorm 乱码问题解决
查看>>
王甲佳_移动互联条件下O2O已经迭代到哪里了?
查看>>
2015年中国数字营销趋势报告
查看>>
CKCalendar
查看>>
聊一聊Go中channel的行为
查看>>
Keyboard and Input view
查看>>
30个最棒的JavaScript库和工具(二)
查看>>
sublime for Go
查看>>
mac node 安装mysql-libmysqlclient 问题
查看>>
npm 命令整理
查看>>
php打开系统应用程序
查看>>
OpenCart 之 CSV 格式商品导入 – 如何导入商品主图片和附加图片?
查看>>
避免常见的六种HTML5错误用法
查看>>