国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

.net core 多項(xiàng)目中使用EFCore

這篇具有很好參考價(jià)值的文章主要介紹了.net core 多項(xiàng)目中使用EFCore。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

類庫一級項(xiàng)目使用.net core 3.1 框架
.net core 多項(xiàng)目中使用EFCore
.net core 多項(xiàng)目中使用EFCore

其中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)

在主工程中注冊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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ASP.NET Core 3.1系列(15)——EFCore之DB First

    ASP.NET Core 3.1系列(15)——EFCore之DB First

    本文開始介紹一些關(guān)于 Entity Framework Core 的內(nèi)容。在 EFCore 中,常用的為 DB First 模式和 Code First 模式,下面就來介紹一下如何在 EFCore 中使用 DB First 模式生成實(shí)體類和數(shù)據(jù)庫上下文。 在 SQL Server 中新建一個(gè)數(shù)據(jù)庫 Dao ,執(zhí)行如下語句,創(chuàng)建 Country 和 Province 數(shù)據(jù)表。 運(yùn)行結(jié)果如

    2024年02月15日
    瀏覽(29)
  • asp.net core 6.0 efcore +sqlserver增刪改查的demo

    下面是一個(gè)使用ASP.NET Core 5.0和Entity Framework Core進(jìn)行增刪改查操作的示例。 首先,創(chuàng)建一個(gè)空的ASP.NET Core 6.0 Web應(yīng)用程序項(xiàng)目。 然后,安裝以下NuGet包: Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools 接下來,創(chuàng)建一個(gè)數(shù)據(jù)庫上下文類,用于定義實(shí)體類和數(shù)據(jù)庫連接

    2024年02月13日
    瀏覽(27)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——模塊管理升級(六十)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——模塊管理升級(六十)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——定義倉儲(chǔ)并實(shí)現(xiàn) (四) abp(net core)+easyui+efcore實(shí)

    2023年04月09日
    瀏覽(92)
  • ASP.Net Core Web Api+EFCore+MySql實(shí)現(xiàn)動(dòng)態(tài)查詢(保姆教學(xué))

    ASP.Net Core Web Api+EFCore+MySql實(shí)現(xiàn)動(dòng)態(tài)查詢(保姆教學(xué))

    本文會(huì)詳細(xì)講解如何從打開文件到第一個(gè)API開發(fā)完成,過程十分詳細(xì),是基于學(xué)習(xí)入門。 現(xiàn)在讓我們開始吧! 打開VS(演示用的Visual Studio2022) 第一步我們選擇創(chuàng)建新項(xiàng)目? ?第二步 選擇開發(fā)語言以及應(yīng)用程序 我們選擇C# -所有平臺-Web?API.找到 ASP.NET Core Web API 應(yīng)用 ? 這里應(yīng)用

    2024年02月12日
    瀏覽(18)
  • .NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫、EFCore 數(shù)據(jù)遷移

    .NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫、EFCore 數(shù)據(jù)遷移

    接上期文章《.NET6項(xiàng)目連接數(shù)據(jù)庫方式方法》,有人問了我?guī)讉€(gè)問題,現(xiàn)在就這幾個(gè)問題,拓展延申一下創(chuàng)建實(shí)體類、數(shù)據(jù)庫。把ORM框架和數(shù)據(jù)遷移都寫進(jìn)去。 我的項(xiàng)目是在Linux上創(chuàng)建的,使用的是vscode開發(fā)工具遠(yuǎn)程開發(fā)。為了方便大家閱讀和操作,我將項(xiàng)目down到我的本地電

    2024年02月05日
    瀏覽(23)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——模塊管理升級之上(六十一)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——模塊管理升級之上(六十一)

    ? A bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)—

    2023年04月16日
    瀏覽(57)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——組織管理升級之下(六十二)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——組織管理升級之下(六十二)

    A bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——

    2023年04月23日
    瀏覽(49)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——供應(yīng)商管理升級之上(六十三)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——供應(yīng)商管理升級之上(六十三)

    a bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——

    2024年02月02日
    瀏覽(47)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——供應(yīng)商管理升級之下(六十四)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——供應(yīng)商管理升級之下(六十四)

    a bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——

    2024年02月03日
    瀏覽(39)
  • .NET使用一行命令輕松生成EF Core項(xiàng)目框架

    dotnet ef是Entity Framework Core(EF Core)的一個(gè)命令行工具,用于管理EF Core應(yīng)用程序的數(shù)據(jù)庫和代碼。除了提供管理數(shù)據(jù)庫的命令之外,dotnet ef還可以生成和管理實(shí)體和上下文代碼。本文將介紹如何使用dotnet ef動(dòng)態(tài)生成代碼。 一、環(huán)境準(zhǔn)備 1、項(xiàng)目準(zhǔn)備 用vs2022新建一個(gè).NET6的asp.

    2023年04月27日
    瀏覽(32)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包