@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,8 @@ 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)
|
||||
{
|
||||
|
||||
+78
-8
@@ -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")
|
||||
+22
-1
@@ -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");
|
||||
}
|
||||
+40
-7
@@ -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")
|
||||
+3
-15
@@ -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");
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user