類庫一級項(xiàng)目使用.net core 3.1 框架
其中EFCore是和數(shù)據(jù)庫交互的
MultiCore 注入EFCore中的DBContext與數(shù)據(jù)庫交互
主要為了解決多項(xiàng)目中數(shù)據(jù)庫遷移失敗問題
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
這是關(guān)鍵,但是這僅僅在開發(fā)環(huán)境下使用,用戶數(shù)據(jù)庫遷移,生產(chǎn)不需要
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;
}
}
}
此時(shí)將efcore設(shè)置為啟動(dòng)項(xiàng)就可以完成數(shù)據(jù)庫遷移了(add-migration update-database)文章來源:http://www.zghlxwxcb.cn/news/detail-468979.html
在主工程中注冊EFDbcontext即可文章來源地址http://www.zghlxwxcb.cn/news/detail-468979.html
// 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");
});
}
到了這里,關(guān)于.net core 多項(xiàng)目中使用EFCore的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!