diff --git a/FreeSql.DbContext/FreeSql.DbContext.csproj b/FreeSql.DbContext/FreeSql.DbContext.csproj index e06c6fa2..398a5d1f 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.csproj +++ b/FreeSql.DbContext/FreeSql.DbContext.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.3.25 + 0.3.216 true YeXiangQin FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite. diff --git a/FreeSql.Repository/FreeSql.Repository.csproj b/FreeSql.Repository/FreeSql.Repository.csproj index 4f7bddcb..6d7b2b1a 100644 --- a/FreeSql.Repository/FreeSql.Repository.csproj +++ b/FreeSql.Repository/FreeSql.Repository.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.3.25 + 0.3.26 YeXiangQin FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite, and read/write separation、and split table. https://github.com/2881099/FreeSql/wiki/Repository diff --git a/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs b/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs index c66ad9b8..903766df 100644 --- a/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs +++ b/FreeSql.Tests/DataAnnotations/MySqlFluentTest.cs @@ -5,10 +5,7 @@ using Xunit; namespace FreeSql.Tests.DataAnnotations { public class MySqlFluentTest { - SqlServerFixture _sqlserverFixture; - - public MySqlFluentTest(SqlServerFixture sqlserverFixture) { - _sqlserverFixture = sqlserverFixture; + public MySqlFluentTest() { } [Fact] @@ -44,15 +41,6 @@ namespace FreeSql.Tests.DataAnnotations { var t2 = g.mysql.Select(t2lastId + 1).ToOne(); } - [Fact] - public void GroupPrimaryKey() { - _sqlserverFixture.SqlServer.CodeFirst.SyncStructure(); - g.mysql.CodeFirst.SyncStructure(); - g.pgsql.CodeFirst.SyncStructure(); - g.sqlite.CodeFirst.SyncStructure(); - g.oracle.CodeFirst.SyncStructure(); - } - class TestFluenttb1 { public int Id { get; set; } @@ -66,17 +54,6 @@ namespace FreeSql.Tests.DataAnnotations { [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"; } } diff --git a/FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs b/FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs index 3a550775..03746d7d 100644 --- a/FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs +++ b/FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs @@ -46,6 +46,15 @@ namespace FreeSql.Tests.DataAnnotations { var t2 = _sqlserverFixture.SqlServer.Select(t2lastId + 1).ToOne(); } + [Fact] + public void GroupPrimaryKey() { + _sqlserverFixture.SqlServer.CodeFirst.SyncStructure(); + g.mysql.CodeFirst.SyncStructure(); + g.pgsql.CodeFirst.SyncStructure(); + g.sqlite.CodeFirst.SyncStructure(); + g.oracle.CodeFirst.SyncStructure(); + } + class TestFluenttb1 { public int Id { get; set; } @@ -59,6 +68,17 @@ namespace FreeSql.Tests.DataAnnotations { [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"; } } diff --git a/FreeSql/FreeSql.csproj b/FreeSql/FreeSql.csproj index a904ac81..e175b2e0 100644 --- a/FreeSql/FreeSql.csproj +++ b/FreeSql/FreeSql.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.3.25 + 0.3.26 true YeXiangQin FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite. diff --git a/FreeSql/SqlServer/SqlServerCodeFirst.cs b/FreeSql/SqlServer/SqlServerCodeFirst.cs index 0b70f1c9..e6f62572 100644 --- a/FreeSql/SqlServer/SqlServerCodeFirst.cs +++ b/FreeSql/SqlServer/SqlServerCodeFirst.cs @@ -124,11 +124,19 @@ namespace FreeSql.SqlServer { 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"); @@ -220,11 +228,19 @@ use " + database, tboldname ?? tbname); 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; }