mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 增加 EfCoreFluentApi HasData 设定 CodeFirst 种子数据;
- 完善 EfCoreFluentApi 功能测试; - 增加 DbContextOptions.NoneParameter 设置是否使用参数化执行 Insert/Update;
This commit is contained in:
parent
e5cbd407cb
commit
03fe0921ee
@ -27,6 +27,8 @@ namespace efcore_to_freesql
|
||||
.UseAutoSyncStructure(true)
|
||||
.Build();
|
||||
|
||||
FreeSql.Extensions.EfCoreFluentApi.ICodeFirstExtensions.Test(Fsql);
|
||||
|
||||
DBContexts.BaseDBContext.Fsql = Fsql;
|
||||
|
||||
var sql11 = Fsql.Select<Topic1>().ToSql();
|
||||
@ -64,13 +66,20 @@ namespace efcore_to_freesql
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllersWithViews();
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
|
||||
Console.InputEncoding = Encoding.GetEncoding("GB2312");
|
||||
|
||||
app.UseHttpMethodOverride(new HttpMethodOverrideOptions { FormFieldName = "X-Http-Method-Override" });
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseRouting();
|
||||
app.UseEndpoints(a => a.MapControllers());
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.EfCoreFluentApi\FreeSql.Extensions.EfCoreFluentApi.csproj" />
|
||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql.DataAnnotations;
|
||||
|
||||
@ -7,9 +8,11 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
{
|
||||
public class EfCoreTableFluent<T>
|
||||
{
|
||||
IFreeSql _fsql;
|
||||
TableFluent<T> _tf;
|
||||
internal EfCoreTableFluent(TableFluent<T> tf)
|
||||
internal EfCoreTableFluent(IFreeSql fsql, TableFluent<T> tf)
|
||||
{
|
||||
_fsql = fsql;
|
||||
_tf = tf;
|
||||
}
|
||||
|
||||
@ -33,8 +36,10 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
#region HasKey
|
||||
public EfCoreTableFluent<T> HasKey(Expression<Func<T, object>> key)
|
||||
{
|
||||
if (key?.Body == null) return this;
|
||||
var exp = key.Body;
|
||||
var exp = key?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 key 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
@ -52,8 +57,9 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
#region HasIndex
|
||||
public HasIndexFluent HasIndex(Expression<Func<T, object>> index)
|
||||
{
|
||||
if (index?.Body == null) throw new ArgumentException("参数错误 index 不能为 null");
|
||||
var exp = index.Body;
|
||||
var exp = index?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 index 不能为 null");
|
||||
|
||||
var indexName = $"idx_{Guid.NewGuid().ToString("N").Substring(0, 8)}";
|
||||
var columns = new List<string>();
|
||||
@ -102,8 +108,9 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
#region HasOne
|
||||
public HasOneFluent<T2> HasOne<T2>(Expression<Func<T, T2>> one)
|
||||
{
|
||||
if (one?.Body == null) throw new ArgumentException("参数错误 one 不能为 null");
|
||||
var exp = one.Body;
|
||||
var exp = one?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 one 不能为 null");
|
||||
|
||||
var oneProperty = "";
|
||||
switch (exp.NodeType)
|
||||
@ -113,10 +120,11 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(oneProperty)) throw new ArgumentException("参数错误 one");
|
||||
return new HasOneFluent<T2>(_tf, oneProperty);
|
||||
return new HasOneFluent<T2>(_fsql, _tf, oneProperty);
|
||||
}
|
||||
public class HasOneFluent<T2>
|
||||
{
|
||||
IFreeSql _fsql;
|
||||
TableFluent<T> _tf;
|
||||
string _selfProperty;
|
||||
string _selfBind;
|
||||
@ -124,15 +132,17 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
string _withOneProperty;
|
||||
string _withOneBind;
|
||||
|
||||
internal HasOneFluent(TableFluent<T> modelBuilder, string oneProperty)
|
||||
internal HasOneFluent(IFreeSql fsql, TableFluent<T> modelBuilder, string oneProperty)
|
||||
{
|
||||
_fsql = fsql;
|
||||
_tf = modelBuilder;
|
||||
_selfProperty = oneProperty;
|
||||
}
|
||||
public HasOneFluent<T2> WithMany<TMany>(Expression<Func<T2, TMany>> many)
|
||||
{
|
||||
if (many?.Body == null) throw new ArgumentException("参数错误 many 不能为 null");
|
||||
var exp = many.Body;
|
||||
var exp = many?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 many 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -142,13 +152,14 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withManyProperty)) throw new ArgumentException("参数错误 many");
|
||||
if (string.IsNullOrEmpty(_selfBind) == false)
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, _selfBind));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, _selfBind));
|
||||
return this;
|
||||
}
|
||||
public HasOneFluent<T2> WithOne(Expression<Func<T2, T>> one, Expression<Func<T2, object>> foreignKey)
|
||||
{
|
||||
if (one?.Body == null) throw new ArgumentException("参数错误 one 不能为 null");
|
||||
var exp = one.Body;
|
||||
var exp = one?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 one 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -158,8 +169,9 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withOneProperty)) throw new ArgumentException("参数错误 one");
|
||||
|
||||
if (foreignKey?.Body == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
exp = foreignKey.Body;
|
||||
exp = foreignKey?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -176,13 +188,14 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withOneBind)) throw new ArgumentException("参数错误 foreignKey");
|
||||
if (string.IsNullOrEmpty(_selfBind) == false)
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
|
||||
return this;
|
||||
}
|
||||
public HasOneFluent<T2> HasForeignKey(Expression<Func<T, object>> foreignKey)
|
||||
{
|
||||
if (foreignKey?.Body == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
var exp = foreignKey.Body;
|
||||
var exp = foreignKey?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -200,9 +213,9 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
if (string.IsNullOrEmpty(_selfBind)) throw new ArgumentException("参数错误 foreignKey");
|
||||
_tf.Navigate(_selfProperty, _selfBind);
|
||||
if (string.IsNullOrEmpty(_withManyProperty) == false)
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, _selfBind));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, _selfBind));
|
||||
if (string.IsNullOrEmpty(_withOneProperty) == false && string.IsNullOrEmpty(_withOneBind) == false)
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@ -211,8 +224,9 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
#region HasMany
|
||||
public HasManyFluent<T2> HasMany<T2>(Expression<Func<T, IEnumerable<T2>>> many)
|
||||
{
|
||||
if (many?.Body == null) throw new ArgumentException("参数错误 many 不能为 null");
|
||||
var exp = many.Body;
|
||||
var exp = many?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 many 不能为 null");
|
||||
|
||||
var manyProperty = "";
|
||||
switch (exp.NodeType)
|
||||
@ -222,26 +236,29 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(manyProperty)) throw new ArgumentException("参数错误 many");
|
||||
return new HasManyFluent<T2>(_tf, manyProperty);
|
||||
return new HasManyFluent<T2>(_fsql, _tf, manyProperty);
|
||||
}
|
||||
public class HasManyFluent<T2>
|
||||
{
|
||||
IFreeSql _fsql;
|
||||
TableFluent<T> _tf;
|
||||
string _selfProperty;
|
||||
string _selfBind;
|
||||
string _withOneProperty;
|
||||
string _withManyProperty;
|
||||
|
||||
internal HasManyFluent(TableFluent<T> modelBuilder, string manyProperty)
|
||||
internal HasManyFluent(IFreeSql fsql, TableFluent<T> modelBuilder, string manyProperty)
|
||||
{
|
||||
_fsql = fsql;
|
||||
_tf = modelBuilder;
|
||||
_selfProperty = manyProperty;
|
||||
}
|
||||
|
||||
public void WithMany(Expression<Func<T2, IEnumerable<T>>> many, Type middleType)
|
||||
{
|
||||
if (many?.Body == null) throw new ArgumentException("参数错误 many 不能为 null");
|
||||
var exp = many.Body;
|
||||
var exp = many?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 many 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -252,12 +269,13 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
if (string.IsNullOrEmpty(_withManyProperty)) throw new ArgumentException("参数错误 many");
|
||||
|
||||
_tf.Navigate(_selfProperty, null, middleType);
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, null, middleType));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, null, middleType));
|
||||
}
|
||||
public HasManyFluent<T2> WithOne(Expression<Func<T2, T>> one)
|
||||
{
|
||||
if (one?.Body == null) throw new ArgumentException("参数错误 one 不能为 null");
|
||||
var exp = one.Body;
|
||||
var exp = one?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 one 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -268,13 +286,14 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
if (string.IsNullOrEmpty(_withOneProperty)) throw new ArgumentException("参数错误 one");
|
||||
|
||||
if (string.IsNullOrEmpty(_selfBind) == false)
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
|
||||
return this;
|
||||
}
|
||||
public HasManyFluent<T2> HasForeignKey(Expression<Func<T2, object>> foreignKey)
|
||||
{
|
||||
if (foreignKey?.Body == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
var exp = foreignKey.Body;
|
||||
var exp = foreignKey?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
@ -292,7 +311,7 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
if (string.IsNullOrEmpty(_selfBind)) throw new ArgumentException("参数错误 foreignKey");
|
||||
_tf.Navigate(_selfProperty, _selfBind);
|
||||
if (string.IsNullOrEmpty(_withOneProperty) == false)
|
||||
_tf.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@ -303,10 +322,40 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
_tf.Property(property).IsIgnore(true);
|
||||
return this;
|
||||
}
|
||||
//public EfCoreTableFluent<T> HasData(T data) => HasData(new[] { data });
|
||||
//public EfCoreTableFluent<T> HasData(IEnumerable<T> data)
|
||||
//{
|
||||
// return this;
|
||||
//}
|
||||
public EfCoreTableFluent<T> HasData(T data) => HasData(new[] { data });
|
||||
|
||||
/// <summary>
|
||||
/// 使用 Repository + EnableAddOrUpdateNavigateList + NoneParameter 方式插入种子数据
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public EfCoreTableFluent<T> HasData(IEnumerable<T> data)
|
||||
{
|
||||
if (data.Any() == false) return this;
|
||||
var sdCopy = data.Select(a => (object)a).ToList();
|
||||
var sdCopyLock = new object();
|
||||
_fsql.Aop.SyncStructureAfter += new EventHandler<Aop.SyncStructureAfterEventArgs>((s, e) =>
|
||||
{
|
||||
object[] sd = null;
|
||||
lock (sdCopyLock)
|
||||
sd = sdCopy?.ToArray();
|
||||
if (sd == null || sd.Any() == false) return;
|
||||
foreach (var et in e.EntityTypes)
|
||||
{
|
||||
if (et != typeof(T)) continue;
|
||||
if (_fsql.Select<object>().AsType(et).Any()) continue;
|
||||
|
||||
var repo = _fsql.GetRepository<object>();
|
||||
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
|
||||
repo.DbContextOptions.NoneParameter = true;
|
||||
repo.AsType(et);
|
||||
repo.Insert(sd);
|
||||
|
||||
lock (sdCopyLock)
|
||||
sdCopy = null;
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,13 +25,13 @@
|
||||
<None Include="../../logo.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
|
||||
<DocumentationFile>FreeSql.Extensions.EfCoreFluentApi.xml</DocumentationFile>
|
||||
<WarningLevel>3</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -4,5 +4,12 @@
|
||||
<name>FreeSql.Extensions.EfCoreFluentApi</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="M:FreeSql.Extensions.EfCoreFluentApi.EfCoreTableFluent`1.HasData(System.Collections.Generic.IEnumerable{`0})">
|
||||
<summary>
|
||||
使用 Repository + EnableAddOrUpdateNavigateList + NoneParameter 方式插入种子数据
|
||||
</summary>
|
||||
<param name="data"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Internal.CommonProvider;
|
||||
|
||||
namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
{
|
||||
@ -10,13 +11,14 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
|
||||
public static ICodeFirst Entity<T>(this ICodeFirst codeFirst, Action<EfCoreTableFluent<T>> modelBuilder)
|
||||
{
|
||||
codeFirst.ConfigEntity<T>(tf => modelBuilder(new EfCoreTableFluent<T>(tf)));
|
||||
var cf = codeFirst as CodeFirstProvider;
|
||||
codeFirst.ConfigEntity<T>(tf => modelBuilder(new EfCoreTableFluent<T>(cf._orm, tf)));
|
||||
return codeFirst;
|
||||
}
|
||||
|
||||
static void Test()
|
||||
public static void Test(IFreeSql fsql)
|
||||
{
|
||||
ICodeFirst cf = null;
|
||||
var cf = fsql.CodeFirst;
|
||||
cf.Entity<Song>(eb =>
|
||||
{
|
||||
eb.ToTable("tb_song");
|
||||
@ -25,7 +27,7 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
eb.Property(a => a.Url).HasMaxLength(100);
|
||||
|
||||
eb.Property(a => a.RowVersion).IsRowVersion();
|
||||
eb.Property(a => a.CreateTime).HasDefaultValueSql("getdate()");
|
||||
eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
|
||||
|
||||
eb.HasKey(a => a.Id);
|
||||
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_xxx11");
|
||||
@ -39,7 +41,33 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
cf.Entity<SongType>(eb =>
|
||||
{
|
||||
eb.HasMany(a => a.Songs).WithOne(a => a.Type).HasForeignKey(a => a.TypeId);
|
||||
|
||||
eb.HasData(new[]
|
||||
{
|
||||
new SongType
|
||||
{
|
||||
Id = 1,
|
||||
Name = "流行",
|
||||
Songs = new List<Song>(new[]
|
||||
{
|
||||
new Song{ Title = "真的爱你" },
|
||||
new Song{ Title = "爱你一万年" },
|
||||
})
|
||||
},
|
||||
new SongType
|
||||
{
|
||||
Id = 2,
|
||||
Name = "乡村",
|
||||
Songs = new List<Song>(new[]
|
||||
{
|
||||
new Song{ Title = "乡里乡亲" },
|
||||
})
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
cf.SyncStructure<SongType>();
|
||||
cf.SyncStructure<Song>();
|
||||
}
|
||||
|
||||
public class SongType
|
||||
@ -52,6 +80,7 @@ namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
|
||||
public class Song
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Url { get; set; }
|
||||
|
@ -22,6 +22,11 @@ namespace FreeSql
|
||||
/// </summary>
|
||||
public bool EnableAddOrUpdateNavigateList { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 使用无参数化设置(对应 IInsert/IUpdate)
|
||||
/// </summary>
|
||||
public bool? NoneParameter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实体变化事件
|
||||
/// </summary>
|
||||
|
@ -52,11 +52,21 @@ namespace FreeSql
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual IInsert<TEntity> OrmInsert() => _db.Orm.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => _db.Orm.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction()).AppendData(data);
|
||||
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _db.Orm.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction()).AppendData(data);
|
||||
protected virtual IInsert<TEntity> OrmInsert()
|
||||
{
|
||||
var insert = _db.Orm.Insert<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
if (_db.Options.NoneParameter != null) insert.NoneParameter(_db.Options.NoneParameter.Value);
|
||||
return insert;
|
||||
}
|
||||
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => OrmInsert().AppendData(data);
|
||||
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => OrmInsert().AppendData(data);
|
||||
|
||||
protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) => _db.Orm.Update<TEntity>().AsType(_entityType).SetSource(entitys).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
|
||||
{
|
||||
var update = _db.Orm.Update<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
if (_db.Options.NoneParameter != null) update.NoneParameter(_db.Options.NoneParameter.Value);
|
||||
return update.SetSource(entitys);
|
||||
}
|
||||
protected virtual IDelete<TEntity> OrmDelete(object dywhere) => _db.Orm.Delete<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction()).WhereDynamic(dywhere);
|
||||
|
||||
internal void EnqueueToDbContext(DbContext.EntityChangeType changeType, EntityState state) =>
|
||||
|
@ -87,6 +87,11 @@
|
||||
- 属性集合不为空时,与数据库存在的关联数据(中间表)完全对比,计算出应该删除和添加的记录
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.DbContextOptions.NoneParameter">
|
||||
<summary>
|
||||
使用无参数化设置(对应 IInsert/IUpdate)
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.DbContextOptions.OnEntityChange">
|
||||
<summary>
|
||||
实体变化事件
|
||||
|
@ -33,6 +33,7 @@ namespace FreeSql
|
||||
(repo as IBaseRepository).UnitOfWork = _repo.UnitOfWork;
|
||||
GetRepositoryDbField(entityType).SetValue(repo, this);
|
||||
|
||||
if (typeof(IBaseRepository<>).MakeGenericType(_repo.EntityType).IsAssignableFrom(_repo.GetType()))
|
||||
typeof(RepositoryDbContext).GetMethod("SetRepositoryDataFilter").MakeGenericMethod(_repo.EntityType)
|
||||
.Invoke(null, new object[] { repo, _repo });
|
||||
}
|
||||
@ -43,10 +44,10 @@ namespace FreeSql
|
||||
return sd;
|
||||
}
|
||||
|
||||
public static void SetRepositoryDataFilter<TEntity>(object repos, BaseRepository<TEntity> baseRepo) where TEntity : class
|
||||
public static void SetRepositoryDataFilter<TEntity>(object repo, IBaseRepository<TEntity> baseRepo) where TEntity : class
|
||||
{
|
||||
var filter = baseRepo.DataFilter as DataFilter<TEntity>;
|
||||
DataFilterUtil.SetRepositoryDataFilter(repos, fl =>
|
||||
DataFilterUtil.SetRepositoryDataFilter(repo, fl =>
|
||||
{
|
||||
foreach (var f in filter._filters)
|
||||
fl.Apply<TEntity>(f.Key, f.Value.Expression);
|
||||
|
@ -8,22 +8,17 @@ namespace FreeSql.DataAnnotations
|
||||
{
|
||||
public class TableFluent
|
||||
{
|
||||
|
||||
public TableFluent(ICodeFirst codeFirst, Type entityType, TableAttribute table)
|
||||
public TableFluent(Type entityType, TableAttribute table)
|
||||
{
|
||||
_codeFirst = codeFirst;
|
||||
_entityType = entityType;
|
||||
_properties = _entityType.GetPropertiesDictIgnoreCase();
|
||||
_table = table;
|
||||
}
|
||||
|
||||
ICodeFirst _codeFirst;
|
||||
Type _entityType;
|
||||
Dictionary<string, PropertyInfo> _properties;
|
||||
TableAttribute _table;
|
||||
|
||||
public void ConfigEntity<T2>(Action<TableFluent<T2>> fluent2) => _codeFirst.ConfigEntity<T2>(fluent2);
|
||||
|
||||
/// <summary>
|
||||
/// 数据库表名
|
||||
/// </summary>
|
||||
@ -89,20 +84,15 @@ namespace FreeSql.DataAnnotations
|
||||
|
||||
public class TableFluent<T>
|
||||
{
|
||||
|
||||
public TableFluent(ICodeFirst codeFirst, TableAttribute table)
|
||||
public TableFluent(TableAttribute table)
|
||||
{
|
||||
_codeFirst = codeFirst;
|
||||
_properties = typeof(T).GetPropertiesDictIgnoreCase();
|
||||
_table = table;
|
||||
}
|
||||
|
||||
ICodeFirst _codeFirst;
|
||||
Dictionary<string, PropertyInfo> _properties;
|
||||
TableAttribute _table;
|
||||
|
||||
public void ConfigEntity<T2>(Action<TableFluent<T2>> fluent2) => _codeFirst.ConfigEntity<T2>(fluent2);
|
||||
|
||||
/// <summary>
|
||||
/// 数据库表名
|
||||
/// </summary>
|
||||
@ -131,7 +121,9 @@ namespace FreeSql.DataAnnotations
|
||||
|
||||
public ColumnFluent Property<TProto>(Expression<Func<T, TProto>> column)
|
||||
{
|
||||
var proto = (column.Body as MemberExpression)?.Member;
|
||||
var exp = column?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
var proto = (exp as MemberExpression)?.Member;
|
||||
if (proto == null) throw new FormatException($"错误的表达式格式 {column}");
|
||||
return Property(proto.Name);
|
||||
}
|
||||
@ -152,7 +144,9 @@ namespace FreeSql.DataAnnotations
|
||||
/// <returns></returns>
|
||||
public TableFluent<T> Navigate<TProto>(Expression<Func<T, TProto>> proto, string bind, Type manyToMany = null)
|
||||
{
|
||||
var member = (proto.Body as MemberExpression)?.Member;
|
||||
var exp = proto?.Body;
|
||||
if (exp.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
var member = (exp as MemberExpression)?.Member;
|
||||
if (member == null) throw new FormatException($"错误的表达式格式 {proto}");
|
||||
return Navigate(member.Name, bind, manyToMany);
|
||||
}
|
||||
|
@ -58,6 +58,7 @@ public static partial class FreeSqlGlobalExtensions
|
||||
if (type == null) return null;
|
||||
if (type == typeof(void)) return "void";
|
||||
if (type.IsGenericParameter) return type.Name;
|
||||
if (type.IsArray) return $"{DisplayCsharp(type.GetElementType())}[]";
|
||||
var sb = new StringBuilder();
|
||||
var nestedType = type;
|
||||
while (nestedType.IsNested)
|
||||
|
@ -2317,6 +2317,137 @@
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteReaderAsync(System.Func{System.Data.Common.DbDataReader,System.Threading.Tasks.Task},System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】
|
||||
</summary>
|
||||
<param name="readerHander"></param>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteReaderAsync(System.Func{System.Data.Common.DbDataReader,System.Threading.Tasks.Task},System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteReaderAsync(dr => {}, "select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteArrayAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteArrayAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteArrayAsync("select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataSetAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataSetAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteDataSetAsync("select * from user where age > ?age; select 2", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataTableAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
查询
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteDataTableAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
查询,ExecuteDataTableAsync("select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteNonQueryAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
在【主库】执行
|
||||
</summary>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteNonQueryAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
在【主库】执行,ExecuteNonQueryAsync("delete from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteScalarAsync(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
在【主库】执行
|
||||
</summary>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.ExecuteScalarAsync(System.String,System.Object)">
|
||||
<summary>
|
||||
在【主库】执行,ExecuteScalarAsync("select 1 from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``1(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||
</summary>
|
||||
<typeparam name="T"></typeparam>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``1(System.String,System.Object)">
|
||||
<summary>
|
||||
执行SQL返回对象集合,QueryAsync<User>("select * from user where age > ?age", new { age = 25 })
|
||||
</summary>
|
||||
<typeparam name="T"></typeparam>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``2(System.Data.CommandType,System.String,System.Data.Common.DbParameter[])">
|
||||
<summary>
|
||||
执行SQL返回对象集合,Query<User>("select * from user where age > ?age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 })
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<param name="cmdType"></param>
|
||||
<param name="cmdText"></param>
|
||||
<param name="cmdParms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IAdo.QueryAsync``2(System.String,System.Object)">
|
||||
<summary>
|
||||
执行SQL返回对象集合,Query<User>("select * from user where age > ?age; select * from address", new { age = 25 })
|
||||
</summary>
|
||||
<typeparam name="T1"></typeparam>
|
||||
<param name="cmdText"></param>
|
||||
<param name="parms"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="E:FreeSql.IAop.ParseExpression">
|
||||
<summary>
|
||||
可自定义解析表达式
|
||||
@ -2840,6 +2971,12 @@
|
||||
<param name="timeout">超时</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IObjectPool`1.GetAsync">
|
||||
<summary>
|
||||
获取资源
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IObjectPool`1.Return(FreeSql.Internal.ObjectPool.Object{`0},System.Boolean)">
|
||||
<summary>
|
||||
使用完毕后,归还资源
|
||||
@ -2910,6 +3047,12 @@
|
||||
</summary>
|
||||
<param name="obj">资源对象</param>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnGetAsync(FreeSql.Internal.ObjectPool.Object{`0})">
|
||||
<summary>
|
||||
从对象池获取对象成功的时候触发,通过该方法统计或初始化对象
|
||||
</summary>
|
||||
<param name="obj">资源对象</param>
|
||||
</member>
|
||||
<member name="M:FreeSql.Internal.ObjectPool.IPolicy`1.OnReturn(FreeSql.Internal.ObjectPool.Object{`0})">
|
||||
<summary>
|
||||
归还对象给对象池的时候触发
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@ -308,7 +309,7 @@ namespace FreeSql
|
||||
{
|
||||
ret.Aop.SyncStructureAfter += new EventHandler<Aop.SyncStructureAfterEventArgs>((s, e) =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(e.Sql)) return;
|
||||
if (_seedData._data.Any() == false) return;
|
||||
foreach (var et in e.EntityTypes)
|
||||
{
|
||||
if (_seedData._data.TryGetValue(et, out var sd) == false) continue;
|
||||
@ -320,6 +321,7 @@ namespace FreeSql
|
||||
.InsertIdentity()
|
||||
.AppendData(sd.ToArray())
|
||||
.ExecuteAffrows();
|
||||
_seedData._data.TryRemove(et, out var old);
|
||||
}
|
||||
});
|
||||
|
||||
@ -335,11 +337,11 @@ namespace FreeSql
|
||||
|
||||
public class SeedDataBuilder
|
||||
{
|
||||
internal Dictionary<Type, List<object>> _data = new Dictionary<Type, List<object>>();
|
||||
internal ConcurrentDictionary<Type, List<object>> _data = new ConcurrentDictionary<Type, List<object>>();
|
||||
public SeedDataBuilder Apply<T>(T data) where T : class
|
||||
{
|
||||
if (_data.TryGetValue(typeof(T), out var ds) == false)
|
||||
_data.Add(typeof(T), ds = new List<object>());
|
||||
_data.TryAdd(typeof(T), ds = new List<object>());
|
||||
ds.Add(data);
|
||||
return this;
|
||||
}
|
||||
|
@ -17,9 +17,9 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public abstract partial class CodeFirstProvider : ICodeFirst
|
||||
{
|
||||
|
||||
protected IFreeSql _orm;
|
||||
protected CommonUtils _commonUtils;
|
||||
protected CommonExpression _commonExpression;
|
||||
public IFreeSql _orm;
|
||||
public CommonUtils _commonUtils;
|
||||
public CommonExpression _commonExpression;
|
||||
public CodeFirstProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression)
|
||||
{
|
||||
_orm = orm;
|
||||
|
@ -134,6 +134,8 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public static void AuditDataValue(object sender, T1 data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
|
||||
{
|
||||
if (data == null) return;
|
||||
if (typeof(T1) == typeof(object) && data.GetType() != table.Type)
|
||||
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
object val = col.GetMapValue(data);
|
||||
|
@ -351,6 +351,8 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
if (orm.Aop.AuditValueHandler == null) return;
|
||||
if (data == null) return;
|
||||
if (typeof(T1) == typeof(object) && data.GetType() != table.Type)
|
||||
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
|
||||
foreach (var col in table.Columns.Values)
|
||||
{
|
||||
object val = col.GetMapValue(data);
|
||||
|
@ -79,7 +79,7 @@ namespace FreeSql.Internal
|
||||
if (entity == null) return _orm.CodeFirst;
|
||||
var type = typeof(T);
|
||||
var table = dicConfigEntity.GetOrAdd(type, new TableAttribute());
|
||||
var fluent = new TableFluent<T>(CodeFirst, table);
|
||||
var fluent = new TableFluent<T>(table);
|
||||
entity.Invoke(fluent);
|
||||
Utils.RemoveTableByEntity(type, this); //remove cache
|
||||
return _orm.CodeFirst;
|
||||
@ -88,7 +88,7 @@ namespace FreeSql.Internal
|
||||
{
|
||||
if (entity == null) return _orm.CodeFirst;
|
||||
var table = dicConfigEntity.GetOrAdd(type, new TableAttribute());
|
||||
var fluent = new TableFluent(CodeFirst, type, table);
|
||||
var fluent = new TableFluent(type, table);
|
||||
entity.Invoke(fluent);
|
||||
Utils.RemoveTableByEntity(type, this); //remove cache
|
||||
return _orm.CodeFirst;
|
||||
|
Loading…
x
Reference in New Issue
Block a user