|
|
|
@ -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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|