首页 > 数据库 >dotnet 多数据库 sqlite efcore model和entity区别 一对多 多对一 多对多

dotnet 多数据库 sqlite efcore model和entity区别 一对多 多对一 多对多

时间:2024-01-20 12:23:30浏览次数:53  
标签:sqlite efcore CPU System entity MultiDb Any using public

efcore-multi-db/MultiDb.sln


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2024
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultiDb.App", "MultiDb.App\MultiDb.App.csproj", "{94622556-3ECC-44F3-977A-8BC9D1555F5F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultiDb.One", "MultiDb.One\MultiDb.One.csproj", "{19B80317-1494-41F3-B139-BA4E1CB83D82}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultiDb.Two", "MultiDb.Two\MultiDb.Two.csproj", "{687DEA51-74E0-4628-8979-4FDCE9FFF2BE}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{94622556-3ECC-44F3-977A-8BC9D1555F5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{94622556-3ECC-44F3-977A-8BC9D1555F5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{94622556-3ECC-44F3-977A-8BC9D1555F5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{94622556-3ECC-44F3-977A-8BC9D1555F5F}.Release|Any CPU.Build.0 = Release|Any CPU
		{19B80317-1494-41F3-B139-BA4E1CB83D82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{19B80317-1494-41F3-B139-BA4E1CB83D82}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{19B80317-1494-41F3-B139-BA4E1CB83D82}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{19B80317-1494-41F3-B139-BA4E1CB83D82}.Release|Any CPU.Build.0 = Release|Any CPU
		{687DEA51-74E0-4628-8979-4FDCE9FFF2BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{687DEA51-74E0-4628-8979-4FDCE9FFF2BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{687DEA51-74E0-4628-8979-4FDCE9FFF2BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{687DEA51-74E0-4628-8979-4FDCE9FFF2BE}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {3774E136-D135-4DE9-AED8-C7A132717A00}
	EndGlobalSection
EndGlobal

efcore-multi-db/global.json

{
  "sdk": {
    "version": "7.0.306"
  }
}

efcore-multi-db/MultiDb.Two/DbTwoContext.cs

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.Two
{
    public class DbTwoContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<UserLecture> UserLectures { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                // optionsBuilder.UseSqlServer(@"Server=(localdb)\ProjectsV13;Database=DbTwo;Integrated Security=True;MultipleActiveResultSets=True;");
                optionsBuilder.UseSqlite(@"Data Source=DbTwo.db");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().ToTable("User");
            modelBuilder.Entity<UserLecture>().ToTable("UserLecture");
        }
    }
}

efcore-multi-db/MultiDb.Two/MultiDb.Two.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
    <PackageReference Include="microsoft.entityframeworkcore.sqlserver" Version="7.0.15" />
    <PackageReference Include="microsoft.entityframeworkcore.tools" Version="7.0.15" />
  </ItemGroup>
</Project>

efcore-multi-db/MultiDb.Two/User.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.Two
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

efcore-multi-db/MultiDb.Two/UserLecture.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.Two
{
    public class UserLecture
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public int LectureId { get; set; }

        public User User { get; set; }
    }
}

efcore-multi-db/MultiDb.One/MultiDb.One.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
    <PackageReference Include="microsoft.entityframeworkcore.sqlserver" Version="7.0.15" />
    <PackageReference Include="microsoft.entityframeworkcore.tools" Version="7.0.15" />
  </ItemGroup>
</Project>

efcore-multi-db/MultiDb.One/Lecture.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.One
{
    public class Lecture
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public string Name { get; set; }
    }
}


efcore-multi-db/MultiDb.One/DbOneContext.cs

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.One
{
    public class DbOneContext : DbContext
    {
        public DbSet<Lecture> Lectures { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                // optionsBuilder.UseSqlServer(@"Server=(localdb)\ProjectsV13;Database=DbOne;Integrated Security=True;MultipleActiveResultSets=True;");
                optionsBuilder.UseSqlite(@"Data Source=DbOne.db");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Lecture>().ToTable("Lecture");
        }
    }
}

efcore-multi-db/MultiDb.App/MultiDb.App.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\MultiDb.One\MultiDb.One.csproj" />
    <ProjectReference Include="..\MultiDb.Two\MultiDb.Two.csproj" />
  </ItemGroup>
</Project>

efcore-multi-db/MultiDb.App/UserModel.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.App
{
    public class UserModel
    {
        public int id { get; set; }
        public string name { get; set; }

        public List<LectureModel> lectures { get; set; }
    }
}

efcore-multi-db/MultiDb.App/DbInitializer.cs

using MultiDb.One;
using MultiDb.Two;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MultiDb.App
{
    public static class DbInitializer
    {
        public static void Initialize(DbOneContext db1, DbTwoContext db2)
        {            
            db1.Database.EnsureCreated();
            db2.Database.EnsureCreated();

            if (!db2.Users.Any() && !db1.Lectures.Any())
            {
                var users = new User[]
                {
                    new User { Name = "User A" },
                    new User { Name = "User B" },
                    new User { Name = "User C" }
                };

                db2.Users.AddRange(users);
                db2.SaveChanges();

                var lectures = new Lecture[]
                {
                    new Lecture { Name = "Lecture A", UserId = 1 },
                    new Lecture { Name = "Lecture B", UserId = 2 },
                    new Lecture { Name = "Lecture C", UserId = 3 }
                };

                db1.Lectures.AddRange(lectures);
                db1.SaveChanges();

                var userLectures = new UserLecture[]
                {
                    new UserLecture { LectureId = 1, UserId = 2 },
                    new UserLecture { LectureId = 1, UserId = 3 },
                    new UserLecture { LectureId = 2, UserId = 1 },
                    new UserLecture { LectureId = 2, UserId = 3 },
                    new UserLecture { LectureId = 3, UserId = 1 },
                    new UserLecture { LectureId = 3, UserId = 2 }
                };

                db2.UserLectures.AddRange(userLectures);
                db2.SaveChanges();
            }
        }
    }
}

efcore-multi-db/MultiDb.App/LectureModel.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MultiDb.App
{
    public class LectureModel
    {
        public int id { get; set; }
        public string name { get; set; }
        public UserModel proctor { get; set; }

        public List<UserModel> attendees { get; set; }
    }
}

efcore-multi-db/MultiDb.App/DataExtensions.cs

using MultiDb.One;
using MultiDb.Two;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MultiDb.App
{
    public static class DataExtensions
    {
        public static List<LectureModel> GetLectures(this DbOneContext db1, DbTwoContext db2)
        {
            var model = db1.Lectures.Select(x => new LectureModel
            {
                id = x.Id,
                name = x.Name,
                proctor = db2.GetUser(x.UserId), //讲座主持人
                attendees = db2.GetAttendees(x.Id)// 出席人数
            }).OrderBy(x => x.name).ToList();

            return model;
        }

        public static UserModel GetUser(this DbTwoContext db2, int id)
        {
            var user = db2.Users.Find(id);

            return new UserModel
            {
                id = user.Id,
                name = user.Name
            };
        }

        public static List<UserModel> GetAttendees(this DbTwoContext db2, int id)
        {
            var model = db2.UserLectures.Where(x => x.LectureId == id).Select(x => new UserModel
            {
                id = x.UserId,
                name = x.User.Name
            }).OrderBy(x => x.name).ToList();

            return model;
        }
    }
}

efcore-multi-db/MultiDb.App/Program.cs

using MultiDb.One;
using MultiDb.Two;
using System;

namespace MultiDb.App
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db1 = new DbOneContext())
            {
                using (var db2 = new DbTwoContext())
                {
                    DbInitializer.Initialize(db1, db2);

                    var lectures = db1.GetLectures(db2);

                    foreach (var l in lectures)
                    {
                        Console.WriteLine(l.name);
                        Console.WriteLine($"Proctor: {l.proctor.name}");
                        Console.WriteLine("Attendees:");

                        foreach (var a in l.attendees)
                        {
                            Console.WriteLine(a.name);
                        }
                        Console.WriteLine();
                    }

                    Console.ReadLine();
                }
            }
        }
    }
}

标签:sqlite,efcore,CPU,System,entity,MultiDb,Any,using,public
From: https://www.cnblogs.com/zhuoss/p/17976297

相关文章

  • dotnet efcore 多数据库 使用
    efcore的使用依赖包efcore-multi-db/MultiDb.Two/MultiDb.Two.csproj<ProjectSdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net7.0</TargetFramework></PropertyGroup><ItemGroup><Packa......
  • 在 .net 8 Blazor Identity 中添加Claim
    .net8BlazorIdentity使用IndividualAccount模版时,默认的UserInfo只有Id,Email和UserName。如果想让客户端共享更多用户信息,可以使用自定义的ClaimsPrincipalFactory。代码如下:publicclassFlowYogaClaimsPrincipalFactory(UserManager<YourCustomUserClass>userMana......
  • 无涯教程-SQLite - 连接Perl
    在本章中,您将学习如何在Perl程序中使用SQLite。安装可以使用PerlDBI模块将SQLite3与Perl集成,该模块是Perl编程语言的数据库访问模块。它定义了一组提供标准数据库接口的方法,变量和约定。以下是在Linux/UNIX计算机上安装DBI模块的简单步骤-$wgethttp://search.cpan.org/CPAN......
  • 无涯教程-SQLite - 连接PHP
    在本章中,您将学习如何在PHP程序中使用SQLite。安装从PHP5.3.0起默认启用SQLite3扩展。可以在编译时使用-without-sqlite3禁用它。Windows用户必须启用php_sqlite3.dll才能使用此扩展名。从PHP5.3.0开始,此DLL包含在Windows的PHP发行版中。有关详细的安装说明,请查看无涯教程......
  • 无涯教程-SQLite - Autoincrement(自增)
    SQLiteAUTOINCREMENT是用于自动递增表中字段值的关键字,只能与INTEGER字段一起使用。AUTOINCREMENT-语法AUTOINCREMENT关键字的基本用法如下-CREATETABLEtable_name(column1INTEGERAUTOINCREMENT,column2datatype,column3datatype,.....colum......
  • 无涯教程-SQLite - NULL语句
    SQLiteNULL是用于表示缺失值的术语,表中的NULL值是显示为空白的字段中的值。NULL-语法以下是在创建表时使用NULL的基本语法。SQLite>CREATETABLECOMPANY(IDINTPRIMARYKEYNOTNULL,NAMETEXTNOTNULL,AGEINTNOTN......
  • 无涯教程-SQLite - 表达式
    SQL表达式类似于公式,它们以查询语言编写,您还可以用于向数据库查询一组特定的数据。Expressions-语法考虑一下SELECT语句的基本语法,如下所示:SELECTcolumn1,column2,columnNFROMtable_nameWHERE[CONDITION|EXPRESSION];以下是不同类型的SQLite表达式。Expressio......
  • 无涯教程-SQLite - 数据类型
    SQLite数据类型是一个属性,用于指定任何对象的数据类型,每个列,变量和表达式在SQLite中都有相关的数据类型。SQLite存储类存储在SQLite数据库中的每个值都具有以下存储类别之一-Sr.No.StorageClass&描述1NULL该值为NULL值。2INTEGER该值为带符号整数,根据值的大小......
  • 无涯教程-SQLite - 安装步骤
    SQLite以其出色的零配置功能而闻名,这意味着不需要复杂的设置或管理,本章将引导您完成在Windows,Linux和MacOSX上设置SQLite的过程。在Windows上安装步骤1-转到SQLite下载页面,并从Windows部分下载预编译的二进制文件。步骤2-下载sqlite-shell-win32-*.zip和sqlite-dll-win......
  • SQLite的帮助类
    由于sqlserver安装复杂,维护困难,对于存储一些简单的数据,使用SQLite无疑是个好的选择经过我的了解,我发现无论是SQLSERVER,还是MySQL,还是SQLite,他们的关键词都是大差不差的比如SQLSERVER的connection关键词就是SqlConnection,SQLite的就是SQLiteConnection今天是第一次使用SQLite,写了个......