Former-commit-id: 488d9ecfc46257c6fa247941f7eb5ffa4426622d
TangShanKaiPing
wanggang 5 years ago
parent f6c91c72b3
commit bd13954d6f

Binary file not shown.

@ -0,0 +1,16 @@
*.bak
*.suo
*.db
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules

@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
namespace DbContextTest1
{
public class Test1
{
public Guid Id { get; set; }
public string TestField1 { get; set; }
}
public class DbContext1 : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=data.db;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Test1>();
}
}
}

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
</ItemGroup>
</Project>

@ -0,0 +1,16 @@
*.bak
*.suo
*.db
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules

@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using System;
namespace DbContextTest2
{
public class Test2
{
public Guid Id { get; set; }
public string TestField1 { get; set; }
}
public class DbContext2 : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=data.db;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Test2>();
}
}
}

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
</ItemGroup>
</Project>

@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29814.53
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreTest", "EFCoreTest.csproj", "{4BB784D0-B6BB-4450-9E34-F64EA74B5555}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCoreTest", "EFCoreTest\EFCoreTest.csproj", "{4BB784D0-B6BB-4450-9E34-F64EA74B5555}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextTest1", "DbContextTest1\DbContextTest1.csproj", "{CF862BF8-BFE3-400A-9E51-258FEA107DDB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextTest2", "DbContextTest2\DbContextTest2.csproj", "{F0676659-C83E-46BB-BBB1-5CFBCD19319B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +19,14 @@ Global
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Release|Any CPU.Build.0 = Release|Any CPU
{CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF862BF8-BFE3-400A-9E51-258FEA107DDB}.Release|Any CPU.Build.0 = Release|Any CPU
{F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0676659-C83E-46BB-BBB1-5CFBCD19319B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,19 @@
*.bak
*.suo
*.db
*.db-shm
*.db-wal
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules
cockroach-data

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@ -12,4 +12,9 @@
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextTest1\DbContextTest1.csproj" />
<ProjectReference Include="..\DbContextTest2\DbContextTest2.csproj" />
</ItemGroup>
</Project>

@ -8,7 +8,7 @@ namespace EFCoreTest
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseSqlite("Data Source=data.db;");
optionsBuilder.UseSqlite("Data Source=data.db;");
//optionsBuilder.UseMySql("Server=139.180.143.95;Port=3306;Database=test;Uid=admin;Pwd=admin;");
///https://www.cockroachlabs.com/docs/stable/build-a-csharp-app-with-cockroachdb.html
/// cockroach start --insecure --host=localhost --http-port=9090
@ -16,7 +16,7 @@ namespace EFCoreTest
/// CREATE DATABASE test;
/// DROP DATABASE test CASCADE;
/// \q
optionsBuilder.UseNpgsql("User ID=root;Host=localhost;Port=26257;Database=test;");
//optionsBuilder.UseNpgsql("User ID=root;Host=localhost;Port=26257;Database=test;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)

@ -0,0 +1,72 @@
using DbContextTest1;
using DbContextTest2;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using System.Linq;
namespace EFCoreTest
{
internal class Program
{
private static void Main(string[] args)
{
using (var db = new MyDbContext())
{
if (db.Database.EnsureCreated())
{
}
//var set = db.Set<User>();
//if (db.Database.EnsureCreated())
//{
// //set.Add(new User
// //{
// // UserName = "test",
// // UserRoles = new List<UserRole>
// // {
// // new UserRole
// // {
// // Role=new Role{
// // Name="admin"
// // }
// // }
// // }
// //});
// //db.SaveChanges();
//}
////foreach (var item in set.ToList())
////{
//// set.Remove(item);
//// db.SaveChanges();
////}
//var user = set.Include(o => o.UserRoles).ThenInclude(o => o.Role).FirstOrDefault();
//user.UserRoles.FirstOrDefault().Role.Name = "test";
//db.SaveChanges();
}
using (var db = new DbContext1())
{
try
{
db.Set<Test1>().Any();
}
catch
{
var databaseCreator = db.GetService<IRelationalDatabaseCreator>();
databaseCreator.CreateTables();
}
}
using (var db = new DbContext2())
{
try
{
db.Set<Test1>().Any();
}
catch
{
var databaseCreator = db.GetService<IRelationalDatabaseCreator>();
databaseCreator.CreateTables();
}
}
}
}
}

@ -1,42 +0,0 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
namespace EFCoreTest
{
internal class Program
{
private static void Main(string[] args)
{
using (var db = new MyDbContext())
{
var set = db.Set<User>();
if (db.Database.EnsureCreated())
{
set.Add(new User
{
UserName = "test",
UserRoles = new List<UserRole>
{
new UserRole
{
Role=new Role{
Name="admin"
}
}
}
});
db.SaveChanges();
}
//foreach (var item in set.ToList())
//{
// set.Remove(item);
// db.SaveChanges();
//}
var user = set.Include(o => o.UserRoles).ThenInclude(o => o.Role).FirstOrDefault();
user.UserRoles.FirstOrDefault().Role.Name = "test";
db.SaveChanges();
}
}
}
}
Loading…
Cancel
Save