mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 优化 DbContext/Repository Orm 属性进行 CURD 与自身事务相同【新突破】;#270
This commit is contained in:
@ -13,7 +13,7 @@ namespace FreeSql
|
||||
{
|
||||
|
||||
internal RepositoryDbContext _dbPriv; //这个不能私有化,有地方需要反射获取它
|
||||
internal RepositoryDbContext _db => _dbPriv ?? (_dbPriv = new RepositoryDbContext(Orm, this));
|
||||
internal RepositoryDbContext _db => _dbPriv ?? (_dbPriv = new RepositoryDbContext(OrmOriginal, this));
|
||||
internal RepositoryDbSet<TEntity> _dbsetPriv;
|
||||
internal RepositoryDbSet<TEntity> _dbset => _dbsetPriv ?? (_dbsetPriv = _db.Set<TEntity>() as RepositoryDbSet<TEntity>);
|
||||
|
||||
@ -25,7 +25,7 @@ namespace FreeSql
|
||||
|
||||
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null)
|
||||
{
|
||||
Orm = fsql;
|
||||
_ormScoped = DbContextScopedFreeSql.Create(fsql, () => _db, () => UnitOfWork);
|
||||
DataFilterUtil.SetRepositoryDataFilter(this, null);
|
||||
DataFilter.Apply("", filter);
|
||||
AsTable(asTable);
|
||||
@ -56,7 +56,9 @@ namespace FreeSql
|
||||
}
|
||||
public DbContextOptions DbContextOptions { get => _db.Options; set => _db.Options = value; }
|
||||
|
||||
public IFreeSql Orm { get; private set; }
|
||||
internal DbContextScopedFreeSql _ormScoped;
|
||||
internal IFreeSql OrmOriginal => _ormScoped?._originalFsql;
|
||||
public IFreeSql Orm => _ormScoped;
|
||||
IUnitOfWork _unitOfWork;
|
||||
public IUnitOfWork UnitOfWork
|
||||
{
|
||||
@ -64,6 +66,7 @@ namespace FreeSql
|
||||
{
|
||||
_unitOfWork = value;
|
||||
if (_dbsetPriv != null) _dbsetPriv._uow = _unitOfWork; //防止 dbset 对象已经存在,再次设置 UnitOfWork 无法生效,所以作此判断重新设置
|
||||
if (_dbPriv != null) _dbPriv.UnitOfWork = _unitOfWork;
|
||||
}
|
||||
get => _unitOfWork;
|
||||
}
|
||||
@ -147,11 +150,11 @@ namespace FreeSql
|
||||
|
||||
TEntity CheckTKeyAndReturnIdEntity(TKey id)
|
||||
{
|
||||
var tb = _db.Orm.CodeFirst.GetTableByEntity(EntityType);
|
||||
var tb = _db.OrmOriginal.CodeFirst.GetTableByEntity(EntityType);
|
||||
if (tb.Primarys.Length != 1) throw new Exception($"实体类型 {EntityType.Name} 主键数量不为 1,无法使用该方法");
|
||||
if (tb.Primarys[0].CsType.NullableTypeOrThis() != typeof(TKey).NullableTypeOrThis()) throw new Exception($"实体类型 {EntityType.Name} 主键类型不为 {typeof(TKey).FullName},无法使用该方法");
|
||||
var obj = Activator.CreateInstance(tb.Type);
|
||||
_db.Orm.SetEntityValueWithPropertyName(tb.Type, obj, tb.Primarys[0].CsName, id);
|
||||
_db.OrmOriginal.SetEntityValueWithPropertyName(tb.Type, obj, tb.Primarys[0].CsName, id);
|
||||
var ret = obj as TEntity;
|
||||
if (ret == null) throw new Exception($"实体类型 {EntityType.Name} 无法转换为 {typeof(TEntity).Name},无法使用该方法");
|
||||
return ret;
|
||||
|
@ -10,11 +10,6 @@ namespace FreeSql
|
||||
{
|
||||
Type EntityType { get; }
|
||||
IUnitOfWork UnitOfWork { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 注意:IFreeSql 属于顶级对象,事务无法自动传递。<para></para>
|
||||
/// 手工传递事务:ISelect/IInsert/IDelete/IUpdate 可以使用 WithTransaction(uow.GetOrBeginTransaction())
|
||||
/// </summary>
|
||||
IFreeSql Orm { get; }
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user