栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.net core 多项目中使用EFCore

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net core 多项目中使用EFCore

类库一级项目使用.net core 3.1 框架

其中EFCore是和数据库交互的
MultiCore 注入EFCore中的DBContext与数据库交互
主要为了解决多项目中数据库迁移失败问题

EFCore 工程安装如下包



  
    netcoreapp3.1
    enable
  

  
    
    
      all
      runtime; build; native; contentfiles; analyzers; buildtransitive
    
    
      all
      runtime; build; native; contentfiles; analyzers; buildtransitive
    
    
  



MultiCore 安装如下



  
    netcoreapp3.1
  

  
    
  

  
    
  




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
    {
        public void Configure(EntityTypeBuilder builder)
        {
            builder.ToTable("person");
        }
    }
}

EFDbcontext.cs

using EFCore.Entity;
using Microsoft.EntityFrameworkCore;
using System;

namespace EFCore
{
    public class EFDbContext:DbContext
    {
        public DbSet people { get; set; }
        public EFDbContext(DbContextOptions 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
    {
        public EFDbContext CreateDbContext(string[] args)
        {
            DbContextOptionsBuilder options = new DbContextOptionsBuilder();
            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(options =>
            {
                options.UseNpgsql(@"Host=localhost;Database=postgres;Username=postgres;Password=postgres");
            });
        }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号