CockroachDB分布式数据库EF Core验证

Former-commit-id: 6809896cfe222e12d73d8ba3f153c4610ae3be88
TangShanKaiPing
wanggang 5 years ago
parent 9bd852f7b4
commit a84088220e

Binary file not shown.

@ -15,4 +15,5 @@ release
Release
Logs
logs
node_modules
node_modules
cockroach-data

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
</ItemGroup>

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
namespace EFCoreTest
{
public class Entity
{
public Guid Id { get; set; }
public DateTime Created { get; set; }
public DateTime LastChanged { get; set; }
public string RowVersion { get; set; }
public Entity()
{
this.Id = Guid.NewGuid();
}
public override string ToString()
{
return this.Id.ToString();
}
}
public class User : Entity
{
public string UserName { get; set; }
public List<UserRole> UserRoles { get; set; } = new List<UserRole>();
}
public class Role : Entity
{
public string Name { get; set; }
public List<UserRole> UserRoles { get; set; } = new List<UserRole>();
}
public class UserRole : Entity
{
public Guid UserId { get; set; }
public Guid RoleId { get; set; }
public User User { get; set; }
public Role Role { get; set; }
}
}

@ -0,0 +1,73 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace EFCoreTest
{
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//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 sql --insecure
/// CREATE DATABASE test;
/// DROP DATABASE test CASCADE;
/// \q
optionsBuilder.UseNpgsql("User ID=root;Host=localhost;Port=26257;Database=test;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
if (entity.GetProperties().Any(o => o.Name == "Id"))
{
modelBuilder.Entity(entity.Name).HasKey("Id");
modelBuilder.Entity(entity.Name).Property("Id").ValueGeneratedNever();
}
if (entity.GetProperties().Any(o => o.Name == "RowVersion"))
{
modelBuilder.Entity(entity.Name).Property("RowVersion")?.IsConcurrencyToken().ValueGeneratedNever();
}
}
modelBuilder.Entity<User>().Property(o => o.UserName).IsRequired().HasMaxLength(64);
modelBuilder.Entity<Role>().Property(o => o.Name).IsRequired().HasMaxLength(64); ;
modelBuilder.Entity<UserRole>().HasOne(o => o.User).WithMany(o => o.UserRoles).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserRole>().HasOne(o => o.Role).WithMany(o => o.UserRoles).HasForeignKey(o => o.RoleId);
modelBuilder.Entity<UserRole>().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique();
}
public override int SaveChanges()
{
var entries = this.ChangeTracker.Entries().ToList();
foreach (var entry in entries)
{
if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
{
if(entry.State == EntityState.Added)
{
var created = entry.Property("Created");
if (created != null)
{
created.CurrentValue = DateTime.UtcNow;
}
}
var rowVersionProperty = entry.Property("RowVersion");
if (rowVersionProperty != null)
{
rowVersionProperty.CurrentValue = Guid.NewGuid().ToString();
}
var lastModified = entry.Property("LastModified");
if (rowVersionProperty != lastModified)
{
lastModified.CurrentValue = DateTime.UtcNow;
}
}
Console.WriteLine($"id:{entry.Entity.ToString()},state:{entry.State}");
}
return base.SaveChanges();
}
}
}

@ -1,71 +1,42 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EFCoreTest
{
public class MyDbContext : DbContext
internal class Program
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=data.db;");
//optionsBuilder.UseMySql("Server=139.180.143.95;Port=3306;Database=test;Uid=admin;Pwd=admin;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>();
modelBuilder.Entity<Role>();
modelBuilder.Entity<UserRole>().HasOne(o => o.User).WithMany(o => o.UserRoles).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserRole>().HasOne(o => o.Role).WithMany(o => o.UserRoles).HasForeignKey(o => o.RoleId);
modelBuilder.Entity<UserRole>().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique();
}
}
public class Entity
{
public Guid Id { get; set; }
public Entity()
{
this.Id = Guid.NewGuid();
}
}
public class User : Entity
{
public string UserName { get; set; }
public List<UserRole> UserRoles { get; set; } = new List<UserRole>();
}
public class Role : Entity
{
public string Name { get; set; }
public List<UserRole> UserRoles { get; set; } = new List<UserRole>();
}
public class UserRole : Entity
{
public Guid UserId { get; set; }
public Guid RoleId { get; set; }
public User User { get; set; }
public Role Role { get; set; }
}
class Program
{
static void Main(string[] args)
private static void Main(string[] args)
{
using (var db = new MyDbContext())
{
var set = db.Set<User>();
if (db.Database.EnsureCreated())
{
db.Set<User>().Add(new User { UserName = "test" });
set.Add(new User
{
UserName = "test",
UserRoles = new List<UserRole>
{
new UserRole
{
Role=new Role{
Name="admin"
}
}
}
});
db.SaveChanges();
}
Console.WriteLine(db.Set<User>().FirstOrDefault()?.UserName);
//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