当前位置:网站首页>.net core 多项目中使用EFCore
.net core 多项目中使用EFCore
2022-04-22 18:16:00 【小兜全糖(Cx)】
类库一级项目使用.net core 3.1 框架


其中EFCore是和数据库交互的
MultiCore 注入EFCore中的DBContext与数据库交互
主要为了解决多项目中数据库迁移失败问题
EFCore 工程安装如下包
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.10" />
</ItemGroup>
</Project>
MultiCore 安装如下
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EFCore\EFCore.csproj" />
</ItemGroup>
</Project>
EFCore
person.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace EFCore.Entity
{
public class Person
{
public int id {
get; set; }
public int age {
get; set; }
public string name {
get; set; }
}
}
personconfig.cs
using System;
using System.Collections.Generic;
using System.Text;
using EFCore.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace EFCore.EntityConfig
{
internal class PersonConfig : IEntityTypeConfiguration<Person>
{
public void Configure(EntityTypeBuilder<Person> builder)
{
builder.ToTable("person");
}
}
}
EFDbcontext.cs
using EFCore.Entity;
using Microsoft.EntityFrameworkCore;
using System;
namespace EFCore
{
public class EFDbContext:DbContext
{
public DbSet<Person> people {
get; set; }
public EFDbContext(DbContextOptions<EFDbContext> options)
:base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
}
EFDbContextFac .cs
这是关键,但是这仅仅在开发环境下使用,用户数据库迁移,生产不需要
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
namespace EFCore
{
internal class EFDbContextFac : IDesignTimeDbContextFactory<EFDbContext>
{
public EFDbContext CreateDbContext(string[] args)
{
DbContextOptionsBuilder<EFDbContext> options = new DbContextOptionsBuilder<EFDbContext>();
options.UseNpgsql(@"Host=localhost;Database=postgres;Username=postgres;Password=postgres");
EFDbContext eFDbContext = new EFDbContext(options.Options);
return eFDbContext;
}
}
}
此时将efcore设置为启动项就可以完成数据库迁移了(add-migration update-database)
在主工程中注册EFDbcontext即可
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<EFDbContext>(options =>
{
options.UseNpgsql(@"Host=localhost;Database=postgres;Username=postgres;Password=postgres");
});
}
版权声明
本文为[小兜全糖(Cx)]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43632687/article/details/122625646
边栏推荐
猜你喜欢

Guiyitong appointment registration

国产芯片DP9637-K总线收发器替代L9637D芯片和SI9241

MFC原理与方法(二)
![[fundamentals of interface testing] Part 8 common assertions of postman and the working principle of assertions](/img/7c/eaf5802b7ff801cf1f6b06da986537.png)
[fundamentals of interface testing] Part 8 common assertions of postman and the working principle of assertions

【Lane】Ultra-Fast-Lane-Detection(1)自定义数据集训练

Leetcode 108: convert an ordered array into a binary search tree

es6 Generator函数的使用

208. 实现 Trie (前缀树)

Configuring test libraries for JUnit

【接口测试基础】第十篇 | 详解Postman请求前置脚本及其工作原理
随机推荐
秒雲助力中電科32所發布“基於擬態應用集成框架的SaaS雲管理平臺解决方案”
mysql 之 MHA
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
Zhihu hot discussion: Zhejiang University has studied Bo for eight years and now makes money by delivering takeout
ZeroMQ:事件监控
Interpretation of the seven key tasks of the guiding opinions on comprehensively improving the development level of digital economy in Jiangsu
The USB flash disk cannot be recognized when it is inserted into the computer. How to solve it?
Must brush the simulated question bank and answers of the latest eight members of Jiangxi in 2022
Zeromq: Event Monitoring
Learning documents.
Build China's cloud ecology | Huayun data and Baixin complete product compatibility and mutual certification and jointly promote the high-quality development of information innovation industry
filter执行原理深度剖析(bitset机制与caching机制)
leetcode - 234. 回文链表
国产芯片DP9637-K总线收发器替代L9637D芯片和SI9241
【接口测试基础】第十一篇 | 详解Postman关联接口及批量执行用例集
B树[概念]
Domestic chip dp9637-k bus transceiver replaces l9637d chip and si9241
In 2022, it is said on the Internet that Apple's upcoming new models iPhone 14 pro and iPhone 14 Pro Max will be a new screen shape, not a banged screen. Do you expect the iPhone 14 with a new screen
常见报错记录
[Lane] ultra fast lane detection (1) custom dataset training