mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 增加 FreeSql.DbContext OnModelCreating 虚方法,实现在 DbContext 使用 FluentApi;#4 - 移除 FreeSql.Extensions.EfCoreFluentApi,功能移至 FreeSql.DbContext;
This commit is contained in:
@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using FreeSql.Internal.Model;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
@ -71,18 +72,27 @@ namespace FreeSql
|
||||
}
|
||||
if (_ormScoped != null) InitPropSets();
|
||||
}
|
||||
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) { }
|
||||
protected virtual void OnConfiguring(DbContextOptionsBuilder options) { }
|
||||
protected virtual void OnModelCreating(ICodeFirst codefirst) { }
|
||||
|
||||
#region Set
|
||||
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
|
||||
static ConcurrentDictionary<Type, NaviteTuple<PropertyInfo[], bool>> _dicGetDbSetProps = new ConcurrentDictionary<Type, NaviteTuple<PropertyInfo[], bool>>();
|
||||
internal void InitPropSets()
|
||||
{
|
||||
var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp =>
|
||||
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||
.Where(a => a.PropertyType.IsGenericType &&
|
||||
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GetGenericArguments()[0])).ToArray());
|
||||
var thisType = this.GetType();
|
||||
var dicval = _dicGetDbSetProps.GetOrAdd(thisType, tp =>
|
||||
NaviteTuple.Create(
|
||||
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
||||
.Where(a => a.PropertyType.IsGenericType &&
|
||||
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GetGenericArguments()[0])).ToArray(),
|
||||
false));
|
||||
if (dicval.Item2 == false)
|
||||
{
|
||||
if (_dicGetDbSetProps.TryUpdate(thisType, NaviteTuple.Create(dicval.Item1, true), dicval))
|
||||
OnModelCreating(OrmOriginal.CodeFirst);
|
||||
}
|
||||
|
||||
foreach (var prop in props)
|
||||
foreach (var prop in dicval.Item1)
|
||||
{
|
||||
var set = this.Set(prop.PropertyType.GetGenericArguments()[0]);
|
||||
|
||||
|
58
FreeSql.DbContext/EfCoreFluentApi/EfCoreColumnFluent.cs
Normal file
58
FreeSql.DbContext/EfCoreFluentApi/EfCoreColumnFluent.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql.DataAnnotations;
|
||||
|
||||
namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
{
|
||||
public class EfCoreColumnFluent
|
||||
{
|
||||
ColumnFluent _cf;
|
||||
|
||||
internal EfCoreColumnFluent(ColumnFluent tf)
|
||||
{
|
||||
_cf = tf;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用 FreeSql FluentApi 方法,当 EFCore FluentApi 方法无法表示的时候使用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public ColumnFluent Help() => _cf;
|
||||
|
||||
public EfCoreColumnFluent HasColumnName(string name)
|
||||
{
|
||||
_cf.Name(name);
|
||||
return this;
|
||||
}
|
||||
public EfCoreColumnFluent HasColumnType(string dbtype)
|
||||
{
|
||||
_cf.DbType(dbtype);
|
||||
return this;
|
||||
}
|
||||
public EfCoreColumnFluent IsRequired()
|
||||
{
|
||||
_cf.IsNullable(false);
|
||||
return this;
|
||||
}
|
||||
public EfCoreColumnFluent HasMaxLength(int length)
|
||||
{
|
||||
_cf.StringLength(length);
|
||||
return this;
|
||||
}
|
||||
public EfCoreColumnFluent HasDefaultValueSql(string sqlValue)
|
||||
{
|
||||
_cf.InsertValueSql(sqlValue);
|
||||
return this;
|
||||
}
|
||||
public EfCoreColumnFluent IsRowVersion()
|
||||
{
|
||||
_cf.IsVersion(true);
|
||||
return this;
|
||||
}
|
||||
//public EfCoreColumnFluent HasConversion(Func<object, string> stringify, Func<string, object> parse)
|
||||
//{
|
||||
// return this;
|
||||
//}
|
||||
}
|
||||
}
|
120
FreeSql.DbContext/EfCoreFluentApi/EfCoreFluentApiExtensions.cs
Normal file
120
FreeSql.DbContext/EfCoreFluentApi/EfCoreFluentApiExtensions.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Extensions.EfCoreFluentApi;
|
||||
using FreeSql.Internal.CommonProvider;
|
||||
|
||||
partial class FreeSqlDbContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// EFCore 99% 相似的 FluentApi 扩展方法
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="codeFirst"></param>
|
||||
/// <param name="modelBuilder"></param>
|
||||
/// <returns></returns>
|
||||
public static ICodeFirst Entity<T>(this ICodeFirst codeFirst, Action<EfCoreTableFluent<T>> modelBuilder)
|
||||
{
|
||||
var cf = codeFirst as CodeFirstProvider;
|
||||
codeFirst.ConfigEntity<T>(tf => modelBuilder(new EfCoreTableFluent<T>(cf._orm, tf)));
|
||||
return codeFirst;
|
||||
}
|
||||
|
||||
public static void EfCoreFluentApiTest(IFreeSql fsql)
|
||||
{
|
||||
var cf = fsql.CodeFirst;
|
||||
cf.Entity<Song>(eb =>
|
||||
{
|
||||
eb.ToTable("tb_song");
|
||||
eb.Ignore(a => a.Field1);
|
||||
eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
|
||||
eb.Property(a => a.Url).HasMaxLength(100);
|
||||
|
||||
eb.Property(a => a.RowVersion).IsRowVersion();
|
||||
eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
|
||||
|
||||
eb.HasKey(a => a.Id);
|
||||
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_xxx11");
|
||||
|
||||
//一对多、多对一
|
||||
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
|
||||
|
||||
//多对多
|
||||
eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
|
||||
});
|
||||
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
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public List<Song> Songs { get; set; }
|
||||
}
|
||||
|
||||
public class Song
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Url { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
|
||||
public int TypeId { get; set; }
|
||||
public SongType Type { get; set; }
|
||||
public List<Tag> Tags { get; set; }
|
||||
|
||||
public int Field1 { get; set; }
|
||||
public long RowVersion { get; set; }
|
||||
}
|
||||
public class Song_tag
|
||||
{
|
||||
public int Song_id { get; set; }
|
||||
public Song Song { get; set; }
|
||||
|
||||
public int Tag_id { get; set; }
|
||||
public Tag Tag { get; set; }
|
||||
}
|
||||
|
||||
public class Tag
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public List<Song> Songs { get; set; }
|
||||
}
|
||||
}
|
365
FreeSql.DbContext/EfCoreFluentApi/EfCoreTableFluent.cs
Normal file
365
FreeSql.DbContext/EfCoreFluentApi/EfCoreTableFluent.cs
Normal file
@ -0,0 +1,365 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using FreeSql.DataAnnotations;
|
||||
|
||||
namespace FreeSql.Extensions.EfCoreFluentApi
|
||||
{
|
||||
public class EfCoreTableFluent<T>
|
||||
{
|
||||
IFreeSql _fsql;
|
||||
TableFluent<T> _tf;
|
||||
internal EfCoreTableFluent(IFreeSql fsql, TableFluent<T> tf)
|
||||
{
|
||||
_fsql = fsql;
|
||||
_tf = tf;
|
||||
}
|
||||
|
||||
public EfCoreTableFluent<T> ToTable(string name)
|
||||
{
|
||||
_tf.Name(name);
|
||||
return this;
|
||||
}
|
||||
public EfCoreTableFluent<T> ToView(string name)
|
||||
{
|
||||
_tf.DisableSyncStructure(true);
|
||||
_tf.Name(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EfCoreColumnFluent Property<TProperty>(Expression<Func<T, TProperty>> property) => new EfCoreColumnFluent(_tf.Property(property));
|
||||
public EfCoreColumnFluent Property(string property) => new EfCoreColumnFluent(_tf.Property(property));
|
||||
|
||||
/// <summary>
|
||||
/// 使用 FreeSql FluentApi 方法,当 EFCore FluentApi 方法无法表示的时候使用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public TableFluent<T> Help() => _tf;
|
||||
|
||||
#region HasKey
|
||||
public EfCoreTableFluent<T> HasKey(Expression<Func<T, object>> key)
|
||||
{
|
||||
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:
|
||||
_tf.Property((exp as MemberExpression).Member.Name).IsPrimary(true);
|
||||
break;
|
||||
case ExpressionType.New:
|
||||
foreach (var member in (exp as NewExpression).Members)
|
||||
_tf.Property(member.Name).IsPrimary(true);
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region HasIndex
|
||||
public HasIndexFluent HasIndex(Expression<Func<T, object>> index)
|
||||
{
|
||||
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>();
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
columns.Add((exp as MemberExpression).Member.Name);
|
||||
break;
|
||||
case ExpressionType.New:
|
||||
foreach (var member in (exp as NewExpression).Members)
|
||||
columns.Add(member.Name);
|
||||
break;
|
||||
}
|
||||
_tf.Index(indexName, string.Join(", ", columns), false);
|
||||
return new HasIndexFluent(_tf, indexName, columns);
|
||||
}
|
||||
public class HasIndexFluent
|
||||
{
|
||||
TableFluent<T> _modelBuilder;
|
||||
string _indexName;
|
||||
List<string> _columns;
|
||||
bool _isUnique;
|
||||
|
||||
internal HasIndexFluent(TableFluent<T> modelBuilder, string indexName, List<string> columns)
|
||||
{
|
||||
_modelBuilder = modelBuilder;
|
||||
_indexName = indexName;
|
||||
_columns = columns;
|
||||
}
|
||||
public HasIndexFluent IsUnique()
|
||||
{
|
||||
_isUnique = true;
|
||||
_modelBuilder.Index(_indexName, string.Join(", ", _columns), _isUnique);
|
||||
return this;
|
||||
}
|
||||
public HasIndexFluent HasName(string name)
|
||||
{
|
||||
_modelBuilder.IndexRemove(_indexName);
|
||||
_indexName = name;
|
||||
_modelBuilder.Index(_indexName, string.Join(", ", _columns), _isUnique);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region HasOne
|
||||
public HasOneFluent<T2> HasOne<T2>(Expression<Func<T, T2>> one)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
oneProperty = (exp as MemberExpression).Member.Name;
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(oneProperty)) throw new ArgumentException("参数错误 one");
|
||||
return new HasOneFluent<T2>(_fsql, _tf, oneProperty);
|
||||
}
|
||||
public class HasOneFluent<T2>
|
||||
{
|
||||
IFreeSql _fsql;
|
||||
TableFluent<T> _tf;
|
||||
string _selfProperty;
|
||||
string _selfBind;
|
||||
string _withManyProperty;
|
||||
string _withOneProperty;
|
||||
string _withOneBind;
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_withManyProperty = (exp as MemberExpression).Member.Name;
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withManyProperty)) throw new ArgumentException("参数错误 many");
|
||||
if (string.IsNullOrEmpty(_selfBind) == false)
|
||||
_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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_withOneProperty = (exp as MemberExpression).Member.Name;
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withOneProperty)) throw new ArgumentException("参数错误 one");
|
||||
|
||||
exp = foreignKey?.Body;
|
||||
if (exp?.NodeType == ExpressionType.Convert) exp = (exp as UnaryExpression)?.Operand;
|
||||
if (exp == null) throw new ArgumentException("参数错误 foreignKey 不能为 null");
|
||||
|
||||
switch (exp.NodeType)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_withOneBind = (exp as MemberExpression).Member.Name;
|
||||
_withOneBind = _withOneBind.TrimStart(',', ' ');
|
||||
break;
|
||||
case ExpressionType.New:
|
||||
_withOneBind = "";
|
||||
foreach (var member in (exp as NewExpression).Members)
|
||||
_withOneBind += ", " + member.Name;
|
||||
_withOneBind = _withOneBind.TrimStart(',', ' ');
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withOneBind)) throw new ArgumentException("参数错误 foreignKey");
|
||||
if (string.IsNullOrEmpty(_selfBind) == false)
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
|
||||
return this;
|
||||
}
|
||||
public HasOneFluent<T2> HasForeignKey(Expression<Func<T, object>> foreignKey)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_selfBind = (exp as MemberExpression).Member.Name;
|
||||
_selfBind = _selfBind.TrimStart(',', ' ');
|
||||
break;
|
||||
case ExpressionType.New:
|
||||
_selfBind = "";
|
||||
foreach (var member in (exp as NewExpression).Members)
|
||||
_selfBind += ", " + member.Name;
|
||||
_selfBind = _selfBind.TrimStart(',', ' ');
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_selfBind)) throw new ArgumentException("参数错误 foreignKey");
|
||||
_tf.Navigate(_selfProperty, _selfBind);
|
||||
if (string.IsNullOrEmpty(_withManyProperty) == false)
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, _selfBind));
|
||||
if (string.IsNullOrEmpty(_withOneProperty) == false && string.IsNullOrEmpty(_withOneBind) == false)
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region HasMany
|
||||
public HasManyFluent<T2> HasMany<T2>(Expression<Func<T, IEnumerable<T2>>> many)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
manyProperty = (exp as MemberExpression).Member.Name;
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(manyProperty)) throw new ArgumentException("参数错误 many");
|
||||
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(IFreeSql fsql, TableFluent<T> modelBuilder, string manyProperty)
|
||||
{
|
||||
_fsql = fsql;
|
||||
_tf = modelBuilder;
|
||||
_selfProperty = manyProperty;
|
||||
}
|
||||
|
||||
public void WithMany(Expression<Func<T2, IEnumerable<T>>> many, Type middleType)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_withManyProperty = (exp as MemberExpression).Member.Name;
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withManyProperty)) throw new ArgumentException("参数错误 many");
|
||||
|
||||
_tf.Navigate(_selfProperty, null, middleType);
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withManyProperty, null, middleType));
|
||||
}
|
||||
public HasManyFluent<T2> WithOne(Expression<Func<T2, T>> one)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_withOneProperty = (exp as MemberExpression).Member.Name;
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_withOneProperty)) throw new ArgumentException("参数错误 one");
|
||||
|
||||
if (string.IsNullOrEmpty(_selfBind) == false)
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
|
||||
return this;
|
||||
}
|
||||
public HasManyFluent<T2> HasForeignKey(Expression<Func<T2, object>> foreignKey)
|
||||
{
|
||||
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)
|
||||
{
|
||||
case ExpressionType.MemberAccess:
|
||||
_selfBind = (exp as MemberExpression).Member.Name;
|
||||
_selfBind = _selfBind.TrimStart(',', ' ');
|
||||
break;
|
||||
case ExpressionType.New:
|
||||
_selfBind = "";
|
||||
foreach (var member in (exp as NewExpression).Members)
|
||||
_selfBind += ", " + member.Name;
|
||||
_selfBind = _selfBind.TrimStart(',', ' ');
|
||||
break;
|
||||
}
|
||||
if (string.IsNullOrEmpty(_selfBind)) throw new ArgumentException("参数错误 foreignKey");
|
||||
_tf.Navigate(_selfProperty, _selfBind);
|
||||
if (string.IsNullOrEmpty(_withOneProperty) == false)
|
||||
_fsql.CodeFirst.ConfigEntity<T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public EfCoreTableFluent<T> Ignore<TProperty>(Expression<Func<T, TProperty>> property)
|
||||
{
|
||||
_tf.Property(property).IsIgnore(true);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
#if netcoreapp
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace FreeSql
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public static class DbContextDependencyInjection
|
||||
public static class FreeSqlDbContextDependencyInjection
|
||||
{
|
||||
static IServiceCollection AddFreeDbContext(this IServiceCollection services, Type dbContextType, Action<DbContextOptionsBuilder> options)
|
||||
{
|
||||
|
@ -3,7 +3,7 @@ using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class FreeSqlDbContextExtensions
|
||||
public static partial class FreeSqlDbContextExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
|
@ -162,6 +162,25 @@
|
||||
</summary>
|
||||
<param name="data"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.Extensions.EfCoreFluentApi.EfCoreColumnFluent.Help">
|
||||
<summary>
|
||||
使用 FreeSql FluentApi 方法,当 EFCore FluentApi 方法无法表示的时候使用
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.Extensions.EfCoreFluentApi.EfCoreTableFluent`1.Help">
|
||||
<summary>
|
||||
使用 FreeSql FluentApi 方法,当 EFCore FluentApi 方法无法表示的时候使用
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<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>
|
||||
<member name="M:FreeSql.IRepositoryUnitOfWork.GetRepository``2(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||
<summary>
|
||||
在工作单元内创建默认仓库类,工作单元下的仓储操作具有事务特点
|
||||
@ -302,6 +321,15 @@
|
||||
例如:20191121_214504_1
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.Entity``1(FreeSql.ICodeFirst,System.Action{FreeSql.Extensions.EfCoreFluentApi.EfCoreTableFluent{``0}})">
|
||||
<summary>
|
||||
EFCore 99% 相似的 FluentApi 扩展方法
|
||||
</summary>
|
||||
<typeparam name="T"></typeparam>
|
||||
<param name="codeFirst"></param>
|
||||
<param name="modelBuilder"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.CreateDbContext(IFreeSql)">
|
||||
<summary>
|
||||
创建普通数据上下文档对象
|
||||
@ -324,7 +352,7 @@
|
||||
<param name="that"></param>
|
||||
<param name="options"></param>
|
||||
</member>
|
||||
<member name="M:FreeSqlRepositoryExtensions.GetRepository``2(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||
<member name="M:FreeSqlDbContextExtensions.GetRepository``2(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||
<summary>
|
||||
返回默认仓库类
|
||||
</summary>
|
||||
@ -334,7 +362,7 @@
|
||||
<param name="filter">数据过滤 + 验证</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlRepositoryExtensions.GetRepository``1(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||
<member name="M:FreeSqlDbContextExtensions.GetRepository``1(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
|
||||
<summary>
|
||||
返回默认仓库类,适用联合主键的仓储类
|
||||
</summary>
|
||||
@ -343,7 +371,7 @@
|
||||
<param name="filter">数据过滤 + 验证</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlRepositoryExtensions.GetGuidRepository``1(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Func{System.String,System.String})">
|
||||
<member name="M:FreeSqlDbContextExtensions.GetGuidRepository``1(IFreeSql,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Func{System.String,System.String})">
|
||||
<summary>
|
||||
返回仓库类
|
||||
</summary>
|
||||
@ -353,7 +381,7 @@
|
||||
<param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSqlRepositoryExtensions.CreateUnitOfWork(IFreeSql)">
|
||||
<member name="M:FreeSqlDbContextExtensions.CreateUnitOfWork(IFreeSql)">
|
||||
<summary>
|
||||
创建基于仓储功能的工作单元,务必使用 using 包含使用
|
||||
</summary>
|
||||
|
@ -1,11 +1,10 @@
|
||||
#if netcoreapp
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace FreeSql
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
public static class FreeSqlRepositoryDependencyInjection
|
||||
{
|
||||
@ -41,5 +40,4 @@ namespace FreeSql
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -3,7 +3,7 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
public static class FreeSqlRepositoryExtensions
|
||||
partial class FreeSqlDbContextExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user