mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
commit
1b85c93a7d
@ -2,7 +2,7 @@ using FreeSql.DataAnnotations;
|
|||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace FreeSql.Tests.DataAnnotations {
|
namespace FreeSql.Tests.DataAnnotations {
|
||||||
public class FluentTest {
|
public class MySqlFluentTest {
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Fluent() {
|
public void Fluent() {
|
||||||
g.mysql.CodeFirst
|
g.mysql.CodeFirst
|
||||||
@ -35,19 +35,21 @@ namespace FreeSql.Tests.DataAnnotations {
|
|||||||
var t2affrows = g.mysql.Insert<TestFluenttb2>().AppendData(new TestFluenttb2 { Id = t2lastId + 1 }).ExecuteAffrows();
|
var t2affrows = g.mysql.Insert<TestFluenttb2>().AppendData(new TestFluenttb2 { Id = t2lastId + 1 }).ExecuteAffrows();
|
||||||
var t2 = g.mysql.Select<TestFluenttb2>(t2lastId + 1).ToOne();
|
var t2 = g.mysql.Select<TestFluenttb2>(t2lastId + 1).ToOne();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class TestFluenttb1 {
|
class TestFluenttb1
|
||||||
public int Id { get; set; }
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
public string name { get; set; } = "defaultValue";
|
public string name { get; set; } = "defaultValue";
|
||||||
}
|
}
|
||||||
|
|
||||||
[Table(Name = "cccccdddwww")]
|
[Table(Name = "cccccdddwww")]
|
||||||
class TestFluenttb2 {
|
class TestFluenttb2
|
||||||
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
|
{
|
||||||
public int Id { get; set; }
|
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
public string name { get; set; } = "defaultValue";
|
public string name { get; set; } = "defaultValue";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
66
FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs
Normal file
66
FreeSql.Tests/DataAnnotations/SqlServerFluentTest.cs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using FreeSql.Tests.DataContext.SqlServer;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace FreeSql.Tests.DataAnnotations {
|
||||||
|
[Collection("SqlServerCollection")]
|
||||||
|
public class SqlServerFluentTest {
|
||||||
|
|
||||||
|
SqlServerFixture _sqlserverFixture;
|
||||||
|
|
||||||
|
public SqlServerFluentTest(SqlServerFixture sqlserverFixture)
|
||||||
|
{
|
||||||
|
_sqlserverFixture = sqlserverFixture;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Fluent() {
|
||||||
|
_sqlserverFixture.SqlServer.CodeFirst
|
||||||
|
//.ConfigEntity<TestFluenttb1>(a => {
|
||||||
|
// a.Name("xxdkdkdk1").SelectFilter("a.Id22 > 0");
|
||||||
|
// a.Property(b => b.Id).Name("Id22").IsIdentity(true);
|
||||||
|
// a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
|
||||||
|
//})
|
||||||
|
|
||||||
|
.ConfigEntity(typeof(TestFluenttb1), a => {
|
||||||
|
a.Name("xxdkdkdk1222").SelectFilter("a.Id22dd > 1");
|
||||||
|
a.Property("Id").Name("Id22dd").IsIdentity(true);
|
||||||
|
a.Property("Name").DbType("varchar(101)").IsNullable(true);
|
||||||
|
})
|
||||||
|
|
||||||
|
.ConfigEntity<TestFluenttb2>(a => {
|
||||||
|
a.Name("xxdkdkdk2").SelectFilter("a.Idx > 0");
|
||||||
|
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
|
||||||
|
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
var ddl1 = _sqlserverFixture.SqlServer.CodeFirst.GetComparisonDDLStatements<TestFluenttb1>();
|
||||||
|
var ddl2 = _sqlserverFixture.SqlServer.CodeFirst.GetComparisonDDLStatements<TestFluenttb2>();
|
||||||
|
|
||||||
|
var t1id = _sqlserverFixture.SqlServer.Insert<TestFluenttb1>().AppendData(new TestFluenttb1 { }).ExecuteIdentity();
|
||||||
|
var t1 = _sqlserverFixture.SqlServer.Select<TestFluenttb1>(t1id).ToOne();
|
||||||
|
|
||||||
|
var t2lastId = _sqlserverFixture.SqlServer.Select<TestFluenttb2>().Max(a => a.Id);
|
||||||
|
var t2affrows = _sqlserverFixture.SqlServer.Insert<TestFluenttb2>().AppendData(new TestFluenttb2 { Id = t2lastId + 1 }).ExecuteAffrows();
|
||||||
|
var t2 = _sqlserverFixture.SqlServer.Select<TestFluenttb2>(t2lastId + 1).ToOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestFluenttb1
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public string name { get; set; } = "defaultValue";
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table(Name = "cccccdddwww")]
|
||||||
|
class TestFluenttb2
|
||||||
|
{
|
||||||
|
[Column(Name = "Idx", IsPrimary = true, IsIdentity = false)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public string name { get; set; } = "defaultValue";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FreeSql.Tests.DataContext.SqlServer
|
namespace FreeSql.Tests.DataContext.SqlServer
|
||||||
@ -9,8 +10,8 @@ namespace FreeSql.Tests.DataContext.SqlServer
|
|||||||
public SqlServerFixture()
|
public SqlServerFixture()
|
||||||
{
|
{
|
||||||
sqlServerLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
|
sqlServerLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
|
||||||
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
||||||
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=(localdb)\\mssqllocaldb;Integrated Security=True;Initial Catalog=cms;Pooling=true;Max Pool Size=10")
|
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=(localdb)\\mssqllocaldb;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
||||||
.UseAutoSyncStructure(true)
|
.UseAutoSyncStructure(true)
|
||||||
.UseLazyLoading(true)
|
.UseLazyLoading(true)
|
||||||
.Build());
|
.Build());
|
||||||
@ -21,6 +22,35 @@ namespace FreeSql.Tests.DataContext.SqlServer
|
|||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// ... clean up test data from the database ...
|
// ... clean up test data from the database ...
|
||||||
|
ClearDataBase();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ClearDataBase()
|
||||||
|
{
|
||||||
|
var dataTables = SqlServer.DbFirst.GetTablesByDatabase();
|
||||||
|
if (dataTables.Any(item => item.Name == "TopicAddField" && item.Schema == "dbo2"))
|
||||||
|
{
|
||||||
|
SqlServer.Ado.ExecuteNonQuery("TRUNCATE TABLE dbo2.TopicAddField ");
|
||||||
|
SqlServer.Ado.ExecuteNonQuery("DROP TABLE dbo2.TopicAddField");
|
||||||
|
SqlServer.Ado.ExecuteNonQuery("DROP SCHEMA dbo2");
|
||||||
|
}
|
||||||
|
|
||||||
|
var tempTables = new string[] { "cccccdddwww", "song", "tb_alltype", "tb_topic", "tb_topic22",
|
||||||
|
"tb_topic22211", "tb_topic111333", "TestTypeInfo", "TestTypeInfo333", "TestTypeParentInfo",
|
||||||
|
"TestTypeParentInfo23123", "xxdkdkdk1222", "xxx"};
|
||||||
|
foreach (var tempTable in tempTables)
|
||||||
|
{
|
||||||
|
DeleteTmpTable(dataTables, tempTable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DeleteTmpTable(List<DatabaseModel.DbTableInfo> dbTables, string deleteTableName, string schemaName = "dbo")
|
||||||
|
{
|
||||||
|
if (dbTables.Any(item => item.Name.ToLower() == deleteTableName.ToLower() && item.Schema.ToLower() == schemaName.ToLower()))
|
||||||
|
{
|
||||||
|
SqlServer.Ado.ExecuteNonQuery($"TRUNCATE TABLE {schemaName.ToLower()}.{deleteTableName}");
|
||||||
|
SqlServer.Ado.ExecuteNonQuery($"DROP TABLE {schemaName.ToLower()}.{deleteTableName}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Lazy<IFreeSql> sqlServerLazy;
|
private Lazy<IFreeSql> sqlServerLazy;
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Folder Include="DataAnnotations\MySql\" />
|
||||||
|
<Folder Include="DataAnnotations\SqlServer\" />
|
||||||
<Folder Include="DataContext\MySql\" />
|
<Folder Include="DataContext\MySql\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user