diff --git a/IRaCIS.Core.API/1Test.cs b/IRaCIS.Core.API/1Test.cs deleted file mode 100644 index f7567c3ca..000000000 --- a/IRaCIS.Core.API/1Test.cs +++ /dev/null @@ -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] = ; - } - - // 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 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; } - } - } -} \ No newline at end of file diff --git a/IRaCIS.Core.Infra.EFCore/CodeFirst使用说明 b/IRaCIS.Core.Infra.EFCore/CodeFirst使用说明 index c5e75d12e..6a24debc8 100644 --- a/IRaCIS.Core.Infra.EFCore/CodeFirst使用说明 +++ b/IRaCIS.Core.Infra.EFCore/CodeFirst使用说明 @@ -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 \ No newline at end of file diff --git a/IRaCIS.Core.Infra.EFCore/UnitOfWork/EFUnitOfWork.cs b/IRaCIS.Core.Infra.EFCore/UnitOfWork/EFUnitOfWork.cs index d12af4b4b..2b80fab4e 100644 --- a/IRaCIS.Core.Infra.EFCore/UnitOfWork/EFUnitOfWork.cs +++ b/IRaCIS.Core.Infra.EFCore/UnitOfWork/EFUnitOfWork.cs @@ -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 : - IEFUnitOfWork 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 : +// IEFUnitOfWork 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(); +// } - } -} +// } +//} diff --git a/IRaCIS.Core.Infra.EFCore/UnitOfWork/IEFUnitOfWork.cs b/IRaCIS.Core.Infra.EFCore/UnitOfWork/IEFUnitOfWork.cs index 6d04c783c..9fb045614 100644 --- a/IRaCIS.Core.Infra.EFCore/UnitOfWork/IEFUnitOfWork.cs +++ b/IRaCIS.Core.Infra.EFCore/UnitOfWork/IEFUnitOfWork.cs @@ -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 :IDisposable where TDbContext : DbContext - { - DbContext DbContext { get; } +//namespace IRaCIS.Core.Infra.EFCore +//{ +// public interface IEFUnitOfWork :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); - } -} +// } +//} diff --git a/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 b/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 index 8f755d360..900b5b978 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 +++ b/IRaCIS.Core.Test/CodeFirstTest/CodeFirst使用说明 @@ -20,7 +20,8 @@ https://www.cnblogs.com/cqpanda/p/16815263.html 5ijθµݿǨƣԶִdown dotnet ef database update ijǨƵǰһǨ -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 + diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs index 934413c0a..3cc41766f 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Convention/NoForeignKeyMigrationsSqlGenerator.cs @@ -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; + } + } } diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs index d89991305..5780659dd 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/IRCContext.cs @@ -34,7 +34,8 @@ public partial class IRCContext : DbContext public virtual DbSet ProjectUser { get; set; } public virtual DbSet ProjectUser2 { get; set; } - + public virtual DbSet TestFieldName { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.Designer.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917144649_TestName.Designer.cs similarity index 64% rename from IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.Designer.cs rename to IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917144649_TestName.Designer.cs index d0f7385ae..3ebd0d69f 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.Designer.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917144649_TestName.Designer.cs @@ -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 { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -65,12 +65,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations b.Property("CreateUserId") .HasColumnType("uniqueidentifier"); - b.Property("Name2") - .IsRequired() - .HasMaxLength(200) - .HasColumnType("nvarchar(200)"); - - b.Property("OtherName") + b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("NAME1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NAme2") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("naMe3") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.HasKey("Id"); + + b.ToTable("TestFieldName"); + }); + + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("DefineLength") + .IsRequired() + .HasMaxLength(400) + .HasColumnType("nvarchar(400)"); + + b.Property("DefualtLength") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("MaxLength") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("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") diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917144649_TestName.cs similarity index 56% rename from IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.cs rename to IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917144649_TestName.cs index 6b04291ac..f504d5115 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917144649_TestName.cs @@ -6,11 +6,29 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations { /// - public partial class testSTR : Migration + public partial class TestName : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { + migrationBuilder.CreateTable( + name: "TestFieldName", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + NAME1 = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + NAme2 = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + naMe3 = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + CreateUserId = table.Column(type: "uniqueidentifier", nullable: false), + CreateTime = table.Column(type: "datetime2", nullable: false), + UpdateUserId = table.Column(type: "uniqueidentifier", nullable: false), + UpdateTime = table.Column(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 /// protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "TestFieldName"); + migrationBuilder.DropTable( name: "TestStr"); } diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.Designer.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917145100_TestNameChange.Designer.cs similarity index 82% rename from IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.Designer.cs rename to IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917145100_TestNameChange.Designer.cs index 9c516cb77..108267f5a 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912152420_testSTR.Designer.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917145100_TestNameChange.Designer.cs @@ -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 { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -70,11 +70,6 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations .HasMaxLength(200) .HasColumnType("nvarchar(200)"); - b.Property("OtherName") - .IsRequired() - .HasMaxLength(200) - .HasColumnType("nvarchar(200)"); - b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("NAME1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NAme2") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("naMe3") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.HasKey("Id"); + + b.ToTable("TestFieldName"); + }); + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b => { b.Property("Id") diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917145100_TestNameChange.cs similarity index 59% rename from IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.cs rename to IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917145100_TestNameChange.cs index f4e031f05..7086e78b2 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240912145208_test.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/20240917145100_TestNameChange.cs @@ -5,7 +5,7 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations { /// - public partial class test : Migration + public partial class TestNameChange : Migration { /// 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( - name: "Name2", - table: "ProjectUser", - type: "nvarchar(200)", - maxLength: 200, - nullable: false, - defaultValue: ""); + newName: "Name2"); } /// protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.DropColumn( - name: "Name2", - table: "ProjectUser"); - migrationBuilder.RenameColumn( - name: "OtherName", + name: "Name2", table: "ProjectUser", newName: "Name"); } diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs index 58210d549..ddacb9d81 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/Migrations/IRCContextModelSnapshot.cs @@ -67,11 +67,6 @@ namespace IRaCIS.Core.Test.CodeFirstTest.MSSQL.Migrations .HasMaxLength(200) .HasColumnType("nvarchar(200)"); - b.Property("OtherName") - .IsRequired() - .HasMaxLength(200) - .HasColumnType("nvarchar(200)"); - b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreateTime") + .HasColumnType("datetime2"); + + b.Property("CreateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("NAME1") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NAme2") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("uniqueidentifier"); + + b.Property("naMe3") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.HasKey("Id"); + + b.ToTable("TestFieldName"); + }); + modelBuilder.Entity("IRaCIS.Core.Test.CodeFirstTest.MSSQL.TestStr", b => { b.Property("Id") diff --git a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs index 1a9805d06..111fea871 100644 --- a/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs +++ b/IRaCIS.Core.Test/CodeFirstTest/MSSQL/TrialImageDownload.cs @@ -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 } diff --git a/IRaCIS.Core.Test/CodeTemplates/EFCore/DbContext.t4 b/IRaCIS.Core.Test/CodeTemplates/EFCore/DbContext.t4 index 1351902b0..c45f19507 100644 --- a/IRaCIS.Core.Test/CodeTemplates/EFCore/DbContext.t4 +++ b/IRaCIS.Core.Test/CodeTemplates/EFCore/DbContext.t4 @@ -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; diff --git a/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 b/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 index 25249cb1d..db80a2a87 100644 --- a/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 +++ b/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 @@ -49,7 +49,7 @@ namespace <#= NamespaceHint #>; if (!string.IsNullOrEmpty(EntityType.GetComment())) { #> -[Comment("<#= code.XmlComment(EntityType.GetComment()) #>")] +[Comment("<#= code.XmlComment(EntityType.GetComment().Replace("\\", "|").Trim()) #>")] <# } @@ -80,9 +80,10 @@ 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) { @@ -93,14 +94,9 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity if (maxLength.HasValue && maxLength != 200) // ȲΪ200ʱ[StringLength] { - - #> [StringLength(<#= maxLength.Value #>)] -<# - - - +<# } else { @@ -134,7 +130,7 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator) .Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType) && a.Type != typeof(StringLengthAttribute) // ų StringLengthAttribute - ); + ); foreach (var dataAnnotation in dataAnnotations) { @@ -143,6 +139,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; }