修改迁移配置以及测试
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-09-17 23:16:11 +08:00
parent 65e47d9918
commit 19b647fb4a
15 changed files with 296 additions and 222 deletions

View File

@ -1,99 +0,0 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EFSaving.Concurrency
{
public class Sample
{
public static void Run()
{
// Ensure database is created and has a person in it
using (var setupContext = new PersonContext())
{
setupContext.Database.EnsureDeleted();
setupContext.Database.EnsureCreated();
setupContext.People.Add(new Person { FirstName = "John", LastName = "Doe" });
setupContext.SaveChanges();
}
#region ConcurrencyHandlingCode
using var context = new PersonContext();
// Fetch a person from database and change phone number
var person = context.People.Single(p => p.PersonId == 1);
person.PhoneNumber = "555-555-5555";
// Change the person's name in the database to simulate a concurrency conflict
context.Database.ExecuteSqlRaw(
"UPDATE dbo.People SET FirstName = 'Jane' WHERE PersonId = 1");
var saved = false;
while (!saved)
{
try
{
// Attempt to save changes to the database
context.SaveChanges();
saved = true;
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
if (entry.Entity is Person)
{
var proposedValues = entry.CurrentValues;
var databaseValues = entry.GetDatabaseValues();
foreach (var property in proposedValues.Properties)
{
var proposedValue = proposedValues[property];
var databaseValue = databaseValues[property];
// TODO: decide which value should be written to database
// proposedValues[property] = <value to be saved>;
}
// Refresh original values to bypass next concurrency check
entry.OriginalValues.SetValues(databaseValues);
}
else
{
throw new NotSupportedException(
"Don't know how to handle concurrency conflicts for "
+ entry.Metadata.Name);
}
}
}
}
#endregion
}
public class PersonContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Requires NuGet package Microsoft.EntityFrameworkCore.SqlServer
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=EFSaving.Concurrency;Trusted_Connection=True");
}
}
public class Person
{
public int PersonId { get; set; }
[ConcurrencyCheck]
public string FirstName { get; set; }
[ConcurrencyCheck]
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}
}
}

View File

@ -3,17 +3,25 @@
# 程序包管理控制台使用方式
1、生成迁移文件
add-migration init -Context IRaCISDBContext
1、生成迁移文件(add-migration init -Project IRaCIS.Core.Infra.EFCore )
add-migration init
Remove-Migration
# 使用dotnet 命令迁移
# 使用dotnet 命令迁移(dotnet ef dbcontext list -p IRaCIS.Core.Infra.EFCore )
# dotnet ef migrations add 本地迁移名字 -p 项目名 -c 数据库上下文名 -o 迁移文件生成目录
1、生成迁移文件
dotnet ef migrations add Initial -s IRaCIS.Core.API -p IRaCIS.Core.Infra.EFCore -c IRaCISDBContext -o CodeFirst_MSSQL/Migrations
1、生成迁移文件 dotnet ef migrations add Initial -s IRaCIS.Core.API -p IRaCIS.Core.Infra.EFCore -c IRaCISDBContext -o CodeFirst_MSSQL/Migrations
dotnet ef migrations add Initial -p IRaCIS.Core.Infra.EFCore
2、撤销生成的迁移文件
dotnet ef migrations remove -p IRaCIS.Core.Infra.EFCore

View File

@ -1,62 +1,62 @@
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
//using System;
//using System.Data;
//using System.Threading;
//using System.Threading.Tasks;
//using Microsoft.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore.Storage;
namespace IRaCIS.Core.Infra.EFCore
{
public class EFUnitOfWork<TDbContext> :
IEFUnitOfWork<TDbContext> where TDbContext : DbContext
, IDisposable
{
public DbContext DbContext { get; }
// https://docs.microsoft.com/en-us/ef/core/saving/transactions
private IDbContextTransaction _transaction;
//namespace IRaCIS.Core.Infra.EFCore
//{
// public class EFUnitOfWork<TDbContext> :
// IEFUnitOfWork<TDbContext> where TDbContext : DbContext
// , IDisposable
// {
// public DbContext DbContext { get; }
// // https://docs.microsoft.com/en-us/ef/core/saving/transactions
// private IDbContextTransaction _transaction;
public EFUnitOfWork(TDbContext dbContext)
{
DbContext = dbContext;
}
// public EFUnitOfWork(TDbContext dbContext)
// {
// DbContext = dbContext;
// }
public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
if (DbContext.Database.IsRelational())
{
_transaction = DbContext.Database.BeginTransaction(isolationLevel);
}
}
// public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
// {
// if (DbContext.Database.IsRelational())
// {
// _transaction = DbContext.Database.BeginTransaction(isolationLevel);
// }
// }
public async Task BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
{
if (DbContext.Database.IsRelational())
{
_transaction = await DbContext.Database.BeginTransactionAsync(isolationLevel);
}
}
// public async Task BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
// {
// if (DbContext.Database.IsRelational())
// {
// _transaction = await DbContext.Database.BeginTransactionAsync(isolationLevel);
// }
// }
//失败 自动回退
public virtual void Commit()
{
DbContext.SaveChanges();
_transaction?.Commit();
}
// //失败 自动回退
// public virtual void Commit()
// {
// DbContext.SaveChanges();
// _transaction?.Commit();
// }
public virtual async Task CommitAsync(CancellationToken cancellationToken)
{
await DbContext.SaveChangesAsync(cancellationToken);
// public virtual async Task CommitAsync(CancellationToken cancellationToken)
// {
// await DbContext.SaveChangesAsync(cancellationToken);
await _transaction?.CommitAsync(cancellationToken);
// await _transaction?.CommitAsync(cancellationToken);
}
// }
public virtual void Dispose()
{
_transaction?.Dispose();
}
// public virtual void Dispose()
// {
// _transaction?.Dispose();
// }
}
}
// }
//}

View File

@ -1,22 +1,22 @@
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
//using System;
//using System.Data;
//using System.Threading;
//using System.Threading.Tasks;
//using Microsoft.EntityFrameworkCore;
namespace IRaCIS.Core.Infra.EFCore
{
public interface IEFUnitOfWork<out TDbContext> :IDisposable where TDbContext : DbContext
{
DbContext DbContext { get; }
//namespace IRaCIS.Core.Infra.EFCore
//{
// public interface IEFUnitOfWork<out TDbContext> :IDisposable where TDbContext : DbContext
// {
// DbContext DbContext { get; }
void BeginTransaction(IsolationLevel isolationLevel= IsolationLevel.ReadCommitted);
// void BeginTransaction(IsolationLevel isolationLevel= IsolationLevel.ReadCommitted);
Task BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted);
// Task BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted);
void Commit();
Task CommitAsync(CancellationToken cancellationToken);
// void Commit();
// Task CommitAsync(CancellationToken cancellationToken);
}
}
// }
//}

View File

@ -20,7 +20,8 @@ https://www.cnblogs.com/cqpanda/p/16815263.html
5、撤销某次更新到数据库的迁移自动执行down 方法)
dotnet ef database update 某次迁移的前一次迁移名称 -p IRaCIS.Core.Test -c IRCContext
dotnet ef migrations add RemoveForeignKey -p IRaCIS.Core.Test -c IRCContext -o CodeFirstTest/MSSQL/Migrations
dotnet ef database update TestName -p IRaCIS.Core.Test -c IRCContext

View File

@ -16,5 +16,10 @@ namespace IRaCIS.Core.Test
operation.ForeignKeys.Clear();
base.Generate(operation, model, builder, terminate);
}
protected override void Generate(AddForeignKeyOperation operation, IModel? model, MigrationCommandListBuilder builder, bool terminate = true)
{
return;
}
}
}

View File

@ -34,6 +34,7 @@ public partial class IRCContext : DbContext
public virtual DbSet<ProjectUser> ProjectUser { get; set; }
public virtual DbSet<ProjectUser2> ProjectUser2 { get; set; }
public virtual DbSet<TestFieldName> TestFieldName { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

View File

@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
{
[DbContext(typeof(IRCContext))]
[Migration("20240912145208_test")]
partial class test
[Migration("20240917144649_TestName")]
partial class TestName
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -65,12 +65,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
b.Property<Guid>("CreateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Name2")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("OtherName")
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
@ -119,6 +114,81 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
b.ToTable("ProjectUser2");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestFieldName", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreateTime")
.HasColumnType("datetime2");
b.Property<Guid>("CreateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("NAME1")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("NAme2")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime2");
b.Property<Guid>("UpdateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("naMe3")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.HasKey("Id");
b.ToTable("TestFieldName");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreateTime")
.HasColumnType("datetime2");
b.Property<Guid>("CreateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("DefineLength")
.IsRequired()
.HasMaxLength(400)
.HasColumnType("nvarchar(400)");
b.Property<string>("DefualtLength")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("MaxLength")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime2");
b.Property<Guid>("UpdateUserId")
.HasColumnType("uniqueidentifier");
b.HasKey("Id");
b.ToTable("TestStr");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.ProjectUser", b =>
{
b.HasOne("IRaCIS.Core.Test.CodeFirstTest.MSSQL.Project", "Project")

View File

@ -6,11 +6,29 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
{
/// <inheritdoc />
public partial class testSTR : Migration
public partial class TestName : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TestFieldName",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
NAME1 = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
NAme2 = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
naMe3 = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
CreateUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
UpdateUserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_TestFieldName", x => x.Id);
});
migrationBuilder.CreateTable(
name: "TestStr",
columns: table => new
@ -33,6 +51,9 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TestFieldName");
migrationBuilder.DropTable(
name: "TestStr");
}

View File

@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
{
[DbContext(typeof(IRCContext))]
[Migration("20240912152420_testSTR")]
partial class testSTR
[Migration("20240917145100_TestNameChange")]
partial class TestNameChange
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -70,11 +70,6 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("OtherName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<Guid>("ProjectId")
.HasColumnType("uniqueidentifier");
@ -119,6 +114,44 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
b.ToTable("ProjectUser2");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestFieldName", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreateTime")
.HasColumnType("datetime2");
b.Property<Guid>("CreateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("NAME1")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("NAme2")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime2");
b.Property<Guid>("UpdateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("naMe3")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.HasKey("Id");
b.ToTable("TestFieldName");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b =>
{
b.Property<Guid>("Id")

View File

@ -5,7 +5,7 @@
namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
{
/// <inheritdoc />
public partial class test : Migration
public partial class TestNameChange : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@ -13,26 +13,14 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
migrationBuilder.RenameColumn(
name: "Name",
table: "ProjectUser",
newName: "OtherName");
migrationBuilder.AddColumn<string>(
name: "Name2",
table: "ProjectUser",
type: "nvarchar(200)",
maxLength: 200,
nullable: false,
defaultValue: "");
newName: "Name2");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Name2",
table: "ProjectUser");
migrationBuilder.RenameColumn(
name: "OtherName",
name: "Name2",
table: "ProjectUser",
newName: "Name");
}

View File

@ -67,11 +67,6 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("OtherName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<Guid>("ProjectId")
.HasColumnType("uniqueidentifier");
@ -116,6 +111,44 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations
b.ToTable("ProjectUser2");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestFieldName", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreateTime")
.HasColumnType("datetime2");
b.Property<Guid>("CreateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("NAME1")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("NAme2")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime2");
b.Property<Guid>("UpdateUserId")
.HasColumnType("uniqueidentifier");
b.Property<string>("naMe3")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.HasKey("Id");
b.ToTable("TestFieldName");
});
modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b =>
{
b.Property<Guid>("Id")

View File

@ -157,13 +157,13 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL
public class ProjectUser : BaseFullAuditEntity
{
public string Name2 { get; set; }
//public string Name2 { get; set; }
public string OtherName { get; set; }
//public string OtherName { get; set; }
public string Code { get; set; }
public string Name2 { get; set; }
//外键
public Guid ProjectId { get; set; }
@ -195,4 +195,16 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL
#endregion
#region 测试命名不符合规则
public class TestFieldName : BaseFullAuditEntity
{
public string NAME1 { get; set; }
public string NAme2 { get; set; }
public string naMe3 { get; set; }
}
#endregion
}

View File

@ -197,7 +197,7 @@ public partial class <#= Options.ContextName #> : DbContext
WriteLine("");
}
#>
entity.Property(e => e.<#= property.Name #>)<#= code.Fragment(propertyFluentApiCalls, indent: 4) #>;
entity.Property(e => e.<#= property.GetColumnName() #>)<#= code.Fragment(propertyFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
firstProperty = false;

View File

@ -49,7 +49,7 @@ namespace <#= NamespaceHint #>;
if (!string.IsNullOrEmpty(EntityType.GetComment()))
{
#>
[Comment("<#= code.XmlComment(EntityType.GetComment()) #>")]
[Comment("<#= code.XmlComment(EntityType.GetComment().Replace("\\", "|").Trim()) #>")]
<#
}
@ -80,10 +80,11 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity
if (!string.IsNullOrEmpty(property.GetComment()))
{
#>
[Comment(" <#= code.XmlComment(property.GetComment()) #>")]
[Comment(" <#= code.XmlComment(property.GetComment().Replace("\\", "|").Trim()) #>")]
<#
}
if (Options.UseDataAnnotations)
{
// 检查属性是否为字符串类型并根据MaxLength来决定是否生成[StringLength]注解
@ -93,14 +94,9 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity
if (maxLength.HasValue && maxLength != 200) // 仅当长度不为200时生成[StringLength]
{
#>
[StringLength(<#= maxLength.Value #>)]
<#
}
else
{
@ -144,6 +140,11 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity
continue;
}
if(dataAnnotation.Arguments?[0]== property.GetColumnName() && property.GetColumnName() !=property.Name )
{
continue;
}
#>
<#= code.Fragment(dataAnnotation) #>
<#
@ -155,7 +156,7 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity
var needsNullable = Options.UseNullableReferenceTypes && property.IsNullable && !property.ClrType.IsValueType;
var needsInitializer = Options.UseNullableReferenceTypes && !property.IsNullable && !property.ClrType.IsValueType;
#>
public <#= code.Reference(property.ClrType) #><#= needsNullable ? "?" : "" #> <#= property.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
public <#= code.Reference(property.ClrType) #><#= needsNullable ? "?" : "" #> <#= property.GetColumnName() #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
<#
firstProperty = false;
}