.NET经销商实战(一)——项目搭建与EFCore的使用
作者:互联网
一.项目框架搭建
1.创建四个项目,分别是DealerPlatform.Core,DealerPlatform.Domain,DealerPlatform.Service,DealerPlatform.Web
除了web都是类库,web是api项目
2.在DealerPlatform.Core项目中,新建一个Repository文件夹,分别新增Repository类与IRepository接口
3.在Core项目中引入包:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
4.在Core项目中新增一个文件夹,Core文件夹,创建数据库连接上下文DearlerPlatformDbContext继承于DbContext
DearlerPlatformDbContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace DealerPlatform.Core.Core
{
public class DearlerPlatformDbContext : DbContext
{
public DearlerPlatformDbContext()
{
}
/// <summary>
///
/// </summary>
/// <param name="options">设置数据库连接字符串及配置</param>
/// <returns></returns>
public DearlerPlatformDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}
}
5.appsetting.json代码如下
点击查看代码
{
"ConnectionStrings": {
"Default": "Server=101.43.0.76,1433;database=DealerPlatform;uid=sa;pwd=12345678wpz."
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
6.引用层级关系:
Web->Service->Core->Domain
7.在web项目也引入那三个efcore的包
8.在program中引入数据库上下文服务,并配置数据库连接字符串
builder.Services.AddDbContext
9.打开vscode,打开powershell控制台,
当前是用dbfirst,输入:dotnet ef dbcontext scaffold "Server=101.43.0.76,1433;database=DealerPlatform;uid=sa;pwd=12345678wpz." Microsoft.EntityFrameworkCore.SqlServer --project DealerPlatform.Core --output-dir Models --context-dir Core
dotnet cli命令参考:https://docs.microsoft.com/zh-cn/ef/core/cli/dotnet
标签:Core,EntityFrameworkCore,EFCore,using,NET,DearlerPlatformDbContext,DealerPlatfor 来源: https://www.cnblogs.com/humblexwang/p/16251361.html