sqlserver CodeFirst DbFirst 测试通过

This commit is contained in:
28810
2018-12-19 15:17:44 +08:00
parent 751b240533
commit d619d29cfa
22 changed files with 1336 additions and 332 deletions

View File

@ -1,126 +1 @@

//using System;
//using System.Collections;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Text.RegularExpressions;
//using FreeSql;
//using FreeSql.DatabaseModel;
////namespace TplDynamicCodeGenerate {
//public class TplDynamicCodeGenerate_view1 : FreeSql.Generator.TemplateEngin.ITemplateOutput {
// public FreeSql.Generator.TemplateEngin.TemplateReturnInfo OuTpUt(StringBuilder tOuTpUt, IDictionary oPtIoNs, string rEfErErFiLeNaMe, FreeSql.Generator.TemplateEngin tEmPlAtEsEnDeR) {
// FreeSql.Generator.TemplateEngin.TemplateReturnInfo rTn = tOuTpUt == null ?
// new FreeSql.Generator.TemplateEngin.TemplateReturnInfo { Sb = (tOuTpUt = new StringBuilder()), Blocks = new Dictionary<string, int[]>() } :
// new FreeSql.Generator.TemplateEngin.TemplateReturnInfo { Sb = tOuTpUt, Blocks = new Dictionary<string, int[]>() };
// Dictionary<string, int[]> TPL__blocks = rTn.Blocks;
// Stack<int[]> TPL__blocks_stack = new Stack<int[]>();
// int[] TPL__blocks_stack_peek;
// List<IDictionary> TPL__forc = new List<IDictionary>();
// Func<IDictionary> pRoCeSsOpTiOnS = new Func<IDictionary>(delegate () {
// IDictionary nEwoPtIoNs = new Hashtable();
// foreach (DictionaryEntry oPtIoNs_dE in oPtIoNs)
// nEwoPtIoNs[oPtIoNs_dE.Key] = oPtIoNs_dE.Value;
// foreach (IDictionary TPL__forc_dIc in TPL__forc)
// foreach (DictionaryEntry TPL__forc_dIc_dE in TPL__forc_dIc)
// nEwoPtIoNs[TPL__forc_dIc_dE.Key] = TPL__forc_dIc_dE.Value;
// return nEwoPtIoNs;
// });
// FreeSql.Generator.TemplateEngin.TemplateIf tPlIf = delegate (object exp) {
// if (exp is bool) return (bool)exp;
// if (exp == null) return false;
// if (exp is int && (int)exp == 0) return false;
// if (exp is string && (string)exp == string.Empty) return false;
// if (exp is long && (long)exp == 0) return false;
// if (exp is short && (short)exp == 0) return false;
// if (exp is byte && (byte)exp == 0) return false;
// if (exp is double && (double)exp == 0) return false;
// if (exp is float && (float)exp == 0) return false;
// if (exp is decimal && (decimal)exp == 0) return false;
// return true;
// };
// FreeSql.Generator.TemplateEngin.TemplatePrint print = delegate (object[] pArMs) {
// if (pArMs == null || pArMs.Length == 0) return;
// foreach (object pArMs_A in pArMs) if (pArMs_A != null) tOuTpUt.Append(pArMs_A);
// };
// FreeSql.Generator.TemplateEngin.TemplatePrint Print = prin
// dynamic index = oPtIoNs["index"];
// dynamic col = oPtIoNs["col"];
// dynamic table = oPtIoNs["table"];
// dynamic dbfirst = oPtIoNs["dbfirst"]; t;
// tOuTpUt.Append("using System;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Reflection;\r\nusing System.Threading.Tasks;\r\nusing Newtonsoft.Json;\r\nusing FreeSql.DataAnnotations;\r\n");
// var dbf = dbfirst as FreeSql.IDbFirst;
// var cols = (table.Columns as List<DbColumnInfo>);
// Func<string, string> UString = stra => stra.Substring(0, 1).ToUpper() + stra.Substring(1);
// Func<DbColumnInfo, string> GetCsType = cola3 => {
// if (cola3.DbType == (int)MySql.Data.MySqlClient.MySqlDbType.Enum || cola3.DbType == (int)MySql.Data.MySqlClient.MySqlDbType.Set) {
// return $"{UString(cola3.Table.Name)}{cola3.Name.ToUpper()}{(cola3.IsNullable ? "?" : "")}";
// }
// return dbf.GetCsType(cola3);
// };
// tOuTpUt.Append("\r\nnamespace test.Model {\r\n\r\n [JsonObject(MemberSerialization.OptIn), Table(Name = \"");
// Print(!string.IsNullOrEmpty(table.Schema) ? table.Schema + "." : "");
// tOuTpUt.Append("");
// Print(table.Name);
// tOuTpUt.Append("\"");
// if (tPlIf(cols.Where(cola003 => cola003.Name.ToLower() == "is_deleted" || cola003.Name.ToLower() == "isdeleted").Any())) {
// tOuTpUt.Append(", SelectFilter = \"a.IsDeleted = 1\"");
// }
// tOuTpUt.Append(")]\r\n public partial class ");
// Print(UString(table.Name));
// tOuTpUt.Append(" {");
// //new Action(delegate () {
// IDictionary TPL__tmp1 = new Hashtable();
// TPL__forc.Add(TPL__tmp1);
// var TPL__tmp2 = table.Columns;
// var TPL__tmp3 = col;
// var TPL__tmp4 = index;
// index = 0;
// if (TPL__tmp2 != null)
// foreach (var TPL__tmp5 in TPL__tmp2) {
// TPL__tmp1["index"] = ++index;
// TPL__tmp1["col"] = TPL__tmp5;
// col = TPL__tmp5;
// tOuTpUt.Append("\r\n ");
// if (tPlIf(string.IsNullOrEmpty(col.Coment) == false)) {
// tOuTpUt.Append("/// <summary>\r\n /// ");
// Print(col.Coment.Replace("\r\n", "\n").Replace("\n", "\r\n /// "));
// tOuTpUt.Append("\r\n /// </summary>");
// }
// tOuTpUt.Append("\r\n [JsonProperty, Column(Name = \"");
// Print(col.Name);
// tOuTpUt.Append("\", DbType = \"");
// Print(col.DbTypeTextFull);
// tOuTpUt.Append("\"");
// if (tPlIf(col.IsPrimary == true)) {
// tOuTpUt.Append(", IsPrimary = true");
// }
// tOuTpUt.Append("");
// if (tPlIf(col.IsIdentity == true)) {
// tOuTpUt.Append(", IsIdentity = true");
// }
// tOuTpUt.Append("");
// if (tPlIf(col.IsNullable == true)) {
// tOuTpUt.Append(", IsNullable = true");
// }
// tOuTpUt.Append(")]\r\n public ");
// Print(GetCsType(col));
// tOuTpUt.Append(" ");
// Print(UString(col.Name));
// tOuTpUt.Append(" { get; set; }\r\n ");
// }
// col = TPL__tmp3;
// index = TPL__tmp4;
// TPL__forc.RemoveAt(TPL__forc.Count - 1);
// //})();
// tOuTpUt.Append("\r\n }\r\n");
// tEmPlAtEsEnDeR.RenderFile2(tOuTpUt, pRoCeSsOpTiOnS(), "../../include/enumtype.tpl", rEfErErFiLeNaMe);
// tOuTpUt.Append("\r\n}");
// return rTn;
// }
//}
////}


View File

@ -21,7 +21,7 @@ namespace FreeSql.Tests.Generator {
[Fact]
public void BuildRichEntityNavigationObject() {
var gen = new TemplateGenerator();
gen.Build(g.mysql.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\rich-entity-navigation-object", @"C:\Users\28810\Desktop\新建文件夹 (9)", "ManagerDB");
gen.Build(g.mysql.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\MySql\rich-entity-navigation-object", @"C:\Users\28810\Desktop\新建文件夹 (9)", "cccddd");
}
}
}

View File

@ -0,0 +1,27 @@
using FreeSql.DataAnnotations;
using FreeSql.Generator;
using System;
using Xunit;
namespace FreeSql.Tests.Generator {
public class SqlServerTemplateGeneratorTest {
[Fact]
public void BuildSimpleEntity() {
var gen = new TemplateGenerator();
gen.Build(g.sqlserver.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\SqlServer\simple-entity", @"C:\Users\28810\Desktop\新建文件夹 (9)", "shop");
}
[Fact]
public void BuildSimpleEntityNavigationObject () {
var gen = new TemplateGenerator();
gen.Build(g.sqlserver.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\SqlServer\simple-entity-navigation-object", @"C:\Users\28810\Desktop\新建文件夹 (9)", "shop");
}
[Fact]
public void BuildRichEntityNavigationObject() {
var gen = new TemplateGenerator();
gen.Build(g.sqlserver.DbFirst, @"C:\Users\28810\Desktop\github\FreeSql\Templates\SqlServer\rich-entity-navigation-object", @"C:\Users\28810\Desktop\新建文件夹 (9)", "shop");
}
}
}

View File

@ -11,17 +11,175 @@ namespace FreeSql.Tests.SqlServer {
public void GetComparisonDDLStatements() {
var sql = g.sqlserver.CodeFirst.GetComparisonDDLStatements<TableAllType>();
sql = g.sqlserver.CodeFirst.GetComparisonDDLStatements<Tb_alltype>();
}
[JsonObject(MemberSerialization.OptIn), Table(Name = "dbo.tb_alltype")]
public partial class Tb_alltype {
[Table(Name = "tb_alltype")]
[JsonProperty, Column(Name = "Id", DbType = "int", IsPrimary = true, IsIdentity = true)]
public int Id { get; set; }
[JsonProperty, Column(Name = "testFieldBool1111", DbType = "bit")]
public bool TestFieldBool1111 { get; set; }
[JsonProperty, Column(Name = "testFieldBoolNullable", DbType = "bit", IsNullable = true)]
public bool? TestFieldBoolNullable { get; set; }
[JsonProperty, Column(Name = "testFieldByte", DbType = "tinyint")]
public sbyte TestFieldByte { get; set; }
[JsonProperty, Column(Name = "testFieldByteNullable", DbType = "tinyint", IsNullable = true)]
public sbyte? TestFieldByteNullable { get; set; }
[JsonProperty, Column(Name = "testFieldBytes", DbType = "varbinary(255)", IsNullable = true)]
public byte[] TestFieldBytes { get; set; }
[JsonProperty, Column(Name = "testFieldDateTime", DbType = "datetime")]
public DateTime TestFieldDateTime { get; set; }
[JsonProperty, Column(Name = "testFieldDateTimeNullable", DbType = "datetime", IsNullable = true)]
public DateTime? TestFieldDateTimeNullable { get; set; }
[JsonProperty, Column(Name = "testFieldDateTimeNullableOffset", DbType = "datetimeoffset", IsNullable = true)]
public DateTime? TestFieldDateTimeNullableOffset { get; set; }
[JsonProperty, Column(Name = "testFieldDateTimeOffset", DbType = "datetimeoffset")]
public DateTime TestFieldDateTimeOffset { get; set; }
[JsonProperty, Column(Name = "testFieldDecimal", DbType = "decimal(10,2)")]
public decimal TestFieldDecimal { get; set; }
[JsonProperty, Column(Name = "testFieldDecimalNullable", DbType = "decimal(10,2)", IsNullable = true)]
public decimal? TestFieldDecimalNullable { get; set; }
[JsonProperty, Column(Name = "testFieldDouble", DbType = "float")]
public double TestFieldDouble { get; set; }
[JsonProperty, Column(Name = "testFieldDoubleNullable", DbType = "float", IsNullable = true)]
public double? TestFieldDoubleNullable { get; set; }
[JsonProperty, Column(Name = "testFieldEnum1", DbType = "int")]
public int TestFieldEnum1 { get; set; }
[JsonProperty, Column(Name = "testFieldEnum1Nullable", DbType = "int", IsNullable = true)]
public int? TestFieldEnum1Nullable { get; set; }
[JsonProperty, Column(Name = "testFieldEnum2", DbType = "bigint")]
public long TestFieldEnum2 { get; set; }
[JsonProperty, Column(Name = "testFieldEnum2Nullable", DbType = "bigint", IsNullable = true)]
public long? TestFieldEnum2Nullable { get; set; }
[JsonProperty, Column(Name = "testFieldFloat", DbType = "real")]
public float TestFieldFloat { get; set; }
[JsonProperty, Column(Name = "testFieldFloatNullable", DbType = "real", IsNullable = true)]
public float? TestFieldFloatNullable { get; set; }
[JsonProperty, Column(Name = "testFieldGuid", DbType = "uniqueidentifier")]
public Guid TestFieldGuid { get; set; }
[JsonProperty, Column(Name = "testFieldGuidNullable", DbType = "uniqueidentifier", IsNullable = true)]
public Guid? TestFieldGuidNullable { get; set; }
[JsonProperty, Column(Name = "testFieldInt", DbType = "int")]
public int TestFieldInt { get; set; }
[JsonProperty, Column(Name = "testFieldIntNullable", DbType = "int", IsNullable = true)]
public int? TestFieldIntNullable { get; set; }
[JsonProperty, Column(Name = "testFieldLong", DbType = "bigint")]
public long TestFieldLong { get; set; }
[JsonProperty, Column(Name = "testFieldSByte", DbType = "tinyint")]
public sbyte TestFieldSByte { get; set; }
[JsonProperty, Column(Name = "testFieldSByteNullable", DbType = "tinyint", IsNullable = true)]
public sbyte? TestFieldSByteNullable { get; set; }
[JsonProperty, Column(Name = "testFieldShort", DbType = "smallint")]
public short TestFieldShort { get; set; }
[JsonProperty, Column(Name = "testFieldShortNullable", DbType = "smallint", IsNullable = true)]
public short? TestFieldShortNullable { get; set; }
[JsonProperty, Column(Name = "testFieldString", DbType = "nvarchar(255)", IsNullable = true)]
public string TestFieldString { get; set; }
[JsonProperty, Column(Name = "testFieldTimeSpan", DbType = "time")]
public TimeSpan TestFieldTimeSpan { get; set; }
[JsonProperty, Column(Name = "testFieldTimeSpanNullable", DbType = "time", IsNullable = true)]
public TimeSpan? TestFieldTimeSpanNullable { get; set; }
[JsonProperty, Column(Name = "testFieldUInt", DbType = "int")]
public int TestFieldUInt { get; set; }
[JsonProperty, Column(Name = "testFieldUIntNullable", DbType = "int", IsNullable = true)]
public int? TestFieldUIntNullable { get; set; }
[JsonProperty, Column(Name = "testFieldULong", DbType = "bigint")]
public long TestFieldULong { get; set; }
[JsonProperty, Column(Name = "testFieldULongNullable", DbType = "bigint", IsNullable = true)]
public long? TestFieldULongNullable { get; set; }
[JsonProperty, Column(Name = "testFieldUShort", DbType = "smallint")]
public short TestFieldUShort { get; set; }
[JsonProperty, Column(Name = "testFieldUShortNullable", DbType = "smallint", IsNullable = true)]
public short? TestFieldUShortNullable { get; set; }
[JsonProperty, Column(Name = "testFielLongNullable", DbType = "bigint", IsNullable = true)]
public long? TestFielLongNullable { get; set; }
}
[Table(Name = "tb_alltype")]
class TableAllType {
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
[Column(OldName = "testFieldBool1111")]
[Column(Name = "testFieldBool1111")]
public bool testFieldBool { get; set; }
public sbyte testFieldSByte { get; set; }
public short testFieldShort { get; set; }

View File

@ -0,0 +1,21 @@
using FreeSql.DataAnnotations;
using System;
using Xunit;
namespace FreeSql.Tests.SqlServer {
public class SqlServerDbFirstTest {
[Fact]
public void GetDatabases() {
var t1 = g.sqlserver.DbFirst.GetDatabases();
}
[Fact]
public void GetTablesByDatabase() {
var t2 = g.sqlserver.DbFirst.GetTablesByDatabase(g.sqlserver.DbFirst.GetDatabases()[0]);
}
}
}