Skip to content

Commit

Permalink
## v0.3.26
Browse files Browse the repository at this point in the history
- 修复 SqlServer CodeFirst 迁移多主键的 bug #23
  • Loading branch information
28810 authored and 28810 committed Mar 28, 2019
1 parent c4c1057 commit 025259b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion FreeSql.DbContext/FreeSql.DbContext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.25</Version>
<Version>0.3.216</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.Repository/FreeSql.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.25</Version>
<Version>0.3.26</Version>
<Authors>YeXiangQin</Authors>
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table.</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
Expand Down
25 changes: 1 addition & 24 deletions FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
namespace FreeSql.Tests.DataAnnotations {
public class MySqlFluentTest {

SqlServerFixture _sqlserverFixture;

public MySqlFluentTest(SqlServerFixture sqlserverFixture) {
_sqlserverFixture = sqlserverFixture;
public MySqlFluentTest() {
}

[Fact]
Expand Down Expand Up @@ -44,15 +41,6 @@ public void Fluent() {
var t2 = g.mysql.Select<TestFluenttb2>(t2lastId + 1).ToOne();
}

[Fact]
public void GroupPrimaryKey() {
_sqlserverFixture.SqlServer.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.mysql.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.pgsql.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.sqlite.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.oracle.CodeFirst.SyncStructure<TestgroupkeyTb>();
}

class TestFluenttb1
{
public int Id { get; set; }
Expand All @@ -66,17 +54,6 @@ class TestFluenttb2
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
public int Id { get; set; }

public string name { get; set; } = "defaultValue";
}

[Table(Name = "test_groupkey")]
class TestgroupkeyTb {
[Column(IsPrimary = true)]
public int Id { get; set; }
[Column(IsPrimary = true)]
public int id2 { get; set; }


public string name { get; set; } = "defaultValue";
}
}
Expand Down
20 changes: 20 additions & 0 deletions FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public void Fluent() {
var t2 = _sqlserverFixture.SqlServer.Select<TestFluenttb2>(t2lastId + 1).ToOne();
}

[Fact]
public void GroupPrimaryKey() {
_sqlserverFixture.SqlServer.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.mysql.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.pgsql.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.sqlite.CodeFirst.SyncStructure<TestgroupkeyTb>();
g.oracle.CodeFirst.SyncStructure<TestgroupkeyTb>();
}

class TestFluenttb1
{
public int Id { get; set; }
Expand All @@ -59,6 +68,17 @@ class TestFluenttb2
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
public int Id { get; set; }

public string name { get; set; } = "defaultValue";
}

[Table(Name = "test_groupkey")]
class TestgroupkeyTb {
[Column(IsPrimary = true)]
public int Id { get; set; }
[Column(IsPrimary = true)]
public int id2 { get; set; }


public string name { get; set; } = "defaultValue";
}
}
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/FreeSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.25</Version>
<Version>0.3.26</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
Expand Down
20 changes: 18 additions & 2 deletions FreeSql/SqlServer/SqlServerCodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,19 @@ public string GetComparisonDDLStatements(params Type[] entityTypes) {
if (tboldname == null) {
//创建新表
sb.Append("use ").Append(_commonUtils.QuoteSqlName(tbname[0])).Append(";\r\nCREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[1]}.{tbname[2]}")).Append(" (");
var pkidx = 0;
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
if (tbcol.Attribute.IsPrimary == true) sb.Append(" primary key");
if (tbcol.Attribute.IsPrimary == true) {
if (tb.Primarys.Length > 1) {
if (pkidx == tb.Primarys.Length - 1)
sb.Append(" primary key (").Append(string.Join(", ", tb.Primarys.Select(a => _commonUtils.QuoteSqlName(a.Attribute.Name)))).Append(")");
} else
sb.Append(" primary key");
pkidx++;
}
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n");
Expand Down Expand Up @@ -220,11 +228,19 @@ from sys.columns a
sb.Append("BEGIN TRANSACTION;\r\n");
//创建临时表
sb.Append("CREATE TABLE ").Append(tmptablename).Append(" (");
var pkidx2 = 0;
foreach (var tbcol in tb.Columns.Values) {
sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ");
sb.Append(tbcol.Attribute.DbType);
if (tbcol.Attribute.IsIdentity == true && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)");
if (tbcol.Attribute.IsPrimary == true) sb.Append(" primary key");
if (tbcol.Attribute.IsPrimary == true) {
if (tb.Primarys.Length > 1) {
if (pkidx2 == tb.Primarys.Length - 1)
sb.Append(" primary key (").Append(string.Join(", ", tb.Primarys.Select(a => _commonUtils.QuoteSqlName(a.Attribute.Name)))).Append(")");
} else
sb.Append(" primary key");
pkidx2++;
}
sb.Append(",");
idents = idents || tbcol.Attribute.IsIdentity == true;
}
Expand Down

0 comments on commit 025259b

Please sign in to comment.