Skip to content

Commit e5cbd40

Browse files
2881028810
authored andcommitted
- 增加 FreeSqlBuilder UseSeedData 设定 CodeFirst 种子数据;
1 parent 6ea5c5d commit e5cbd40

File tree

8 files changed

+221
-242
lines changed

8 files changed

+221
-242
lines changed

Examples/base_entity/Program.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,24 @@ static void Main(string[] args)
6565
//.UseConnectionString(FreeSql.DataType.OdbcOracle, "Driver={Oracle in XE};Server=//127.0.0.1:1521/XE;Persist Security Info=False;Trusted_Connection=Yes;UID=odbc1;PWD=123456")
6666
//.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
6767

68-
.UseConnectionString(FreeSql.DataType.OdbcDameng, "Driver={DM8 ODBC DRIVER};Server=127.0.0.1:5236;Persist Security Info=False;Trusted_Connection=Yes;UID=USER1;PWD=123456789")
68+
//.UseConnectionString(FreeSql.DataType.OdbcDameng, "Driver={DM8 ODBC DRIVER};Server=127.0.0.1:5236;Persist Security Info=False;Trusted_Connection=Yes;UID=USER1;PWD=123456789")
69+
70+
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
71+
.UseSeedData(sd => sd
72+
.Apply(new[]
73+
{
74+
new Products { Id = 1, title = "product-1" },
75+
new Products { Id = 2, title = "product-2" },
76+
new Products { Id = 3, title = "product-3" },
77+
new Products { Id = 4, title = "product-4" }
78+
})
79+
.Apply(new[]
80+
{
81+
new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11" } },
82+
new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } },
83+
new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }
84+
})
85+
)
6986

7087
.UseLazyLoading(true)
7188
.Build();

Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ static BaseEntity()
2525
{
2626
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
2727
if (tkeyType == typeof(int) || tkeyType == typeof(long))
28-
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
29-
t => t.Property("Id").IsIdentity(true));
28+
BaseEntity.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true));
3029
}
3130

3231
/// <summary>

Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ static BaseEntityAsync()
2121
{
2222
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
2323
if (tkeyType == typeof(int) || tkeyType == typeof(long))
24-
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
25-
t => t.Property("Id").IsIdentity(true));
24+
BaseEntity.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true));
2625
}
2726

2827
/// <summary>

Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Collections.Concurrent;
78
using System.Data;
89
using System.Diagnostics;
910
using System.Linq;
@@ -18,7 +19,7 @@ namespace FreeSql
1819
[Table(DisableSyncStructure = true)]
1920
public abstract class BaseEntity
2021
{
21-
static IFreeSql _ormPriv;
22+
internal static IFreeSql _ormPriv;
2223
/// <summary>
2324
/// 全局 IFreeSql orm 对象
2425
/// </summary>
@@ -42,6 +43,32 @@ public static void Initialization(IFreeSql fsql)
4243
{
4344
_ormPriv = fsql;
4445
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine($"\r\n线程{Thread.CurrentThread.ManagedThreadId}: {e.Sql}\r\n");
46+
if (_configEntityQueues.Any())
47+
{
48+
lock (_configEntityLock)
49+
{
50+
while (_configEntityQueues.TryDequeue(out var cei))
51+
_ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent);
52+
}
53+
}
54+
}
55+
56+
class ConfigEntityInfo
57+
{
58+
public Type EntityType;
59+
public Action<TableFluent> Fluent;
60+
}
61+
static ConcurrentQueue<ConfigEntityInfo> _configEntityQueues = new ConcurrentQueue<ConfigEntityInfo>();
62+
static object _configEntityLock = new object();
63+
internal static void ConfigEntity(Type entityType, Action<TableFluent> fluent)
64+
{
65+
lock (_configEntityLock)
66+
{
67+
if (_ormPriv == null)
68+
_configEntityQueues.Enqueue(new ConfigEntityInfo { EntityType = entityType, Fluent = fluent });
69+
else
70+
_ormPriv.CodeFirst.ConfigEntity(entityType, fluent);
71+
}
4572
}
4673

4774
/// <summary>

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/FreeSql.xml

Lines changed: 29 additions & 164 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)