- 补充 使用 IsIgnore 忽略后,表达式再使用时的友好错误提示;

This commit is contained in:
28810 2019-08-13 17:17:38 +08:00
parent dce7496b01
commit 79ab3ae217
5 changed files with 40 additions and 22 deletions

View File

@ -11,18 +11,16 @@ namespace FreeSql
{ {
internal IFreeSql _orm; internal IFreeSql _orm;
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql"); public IFreeSql Orm => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
public IFreeSql Orm => _fsql;
protected IUnitOfWork _uowPriv; protected IUnitOfWork _uowPriv;
internal IUnitOfWork _uow => _isUseUnitOfWork ? (_uowPriv ?? (_uowPriv = new UnitOfWork(_fsql))) : null; internal IUnitOfWork _uow => _isUseUnitOfWork ? (_uowPriv ?? (_uowPriv = new UnitOfWork(Orm))) : null;
internal bool _isUseUnitOfWork = true; //不使用工作单元事务 internal bool _isUseUnitOfWork = true; //不使用工作单元事务
public IUnitOfWork UnitOfWork => _uow; public IUnitOfWork UnitOfWork => _uow;
DbContextOptions _options; DbContextOptions _options;
internal DbContextOptions Options public DbContextOptions Options
{ {
get get
{ {
@ -31,19 +29,22 @@ namespace FreeSql
_options = new DbContextOptions(); _options = new DbContextOptions();
return _options; return _options;
} }
set => _options = value;
} }
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
protected DbContext() protected DbContext()
{ {
var builder = new DbContextOptionsBuilder(); var builder = new DbContextOptionsBuilder();
OnConfiguring(builder); OnConfiguring(builder);
_orm = builder._fsql; _orm = builder._fsql;
_options = builder._options;
if (_orm != null) InitPropSets(); if (_orm != null) InitPropSets();
} }
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) { }
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
internal void InitPropSets() internal void InitPropSets()
{ {
var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp => var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp =>
@ -60,11 +61,6 @@ namespace FreeSql
} }
} }
protected virtual void OnConfiguring(DbContextOptionsBuilder builder)
{
}
protected Dictionary<Type, IDbSet> _dicSet = new Dictionary<Type, IDbSet>(); protected Dictionary<Type, IDbSet> _dicSet = new Dictionary<Type, IDbSet>();
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>; public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
public virtual IDbSet Set(Type entityType) public virtual IDbSet Set(Type entityType)
@ -133,15 +129,10 @@ namespace FreeSql
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>(); Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
internal int _affrows = 0; internal int _affrows = 0;
internal void EnqueueAction(ExecCommandInfoType actionType, IDbSet dbSet, Type stateType, object state) internal void EnqueueAction(ExecCommandInfoType actionType, IDbSet dbSet, Type stateType, object state) =>
{
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state }); _actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
}
~DbContext() ~DbContext() => this.Dispose();
{
this.Dispose();
}
bool _isdisposed = false; bool _isdisposed = false;
public void Dispose() public void Dispose()
{ {

View File

@ -6,11 +6,17 @@ namespace FreeSql
{ {
internal IFreeSql _fsql; internal IFreeSql _fsql;
internal DbContextOptions _options;
public DbContextOptionsBuilder UseFreeSql(IFreeSql orm) public DbContextOptionsBuilder UseFreeSql(IFreeSql orm)
{ {
_fsql = orm; _fsql = orm;
return this; return this;
} }
public DbContextOptionsBuilder UseOptions(DbContextOptions options)
{
_options = options;
return this;
}
} }
} }

View File

@ -18,7 +18,7 @@ namespace FreeSql
{ {
_ctx = ctx; _ctx = ctx;
_uow = ctx._uow; _uow = ctx._uow;
_fsql = ctx._fsql; _fsql = ctx.Orm;
} }
} }

View File

@ -283,7 +283,7 @@ namespace FreeSql.Tests
{ {
[FreeSql.DataAnnotations.Column(IsPrimary = true)] [FreeSql.DataAnnotations.Column(IsPrimary = true)]
public Guid Id { get; set; } public Guid Id { get; protected set; }
public string TaskName { get; set; } public string TaskName { get; set; }
public Guid TemplatesId { get; set; } public Guid TemplatesId { get; set; }
public string GeneratePath { get; set; } public string GeneratePath { get; set; }
@ -338,9 +338,23 @@ namespace FreeSql.Tests
public DateTime class2 { set; get; } public DateTime class2 { set; get; }
} }
class TestDto
{
public Guid Id { get; set; }
public bool IsLeaf { get; set; }
}
[Fact] [Fact]
public void Test1() public void Test1()
{ {
var dkdkd = g.mysql.Select<TaskBuild>().AsTable((t,old) => "TaskBuild22")
.ToList< TestDto>(a => new TestDto()
{
Id = a.Id,
IsLeaf = g.mysql.Select<TaskBuild>().AsTable((t, old) => "TaskBuild22").Any(b => b.TemplatesId == a.Id)
});
var xxxkdkd = g.oracle.Select<Templates, TaskBuild>() var xxxkdkd = g.oracle.Select<Templates, TaskBuild>()
.InnerJoin((a,b) => true) .InnerJoin((a,b) => true)
.Where((a,b) => (DateTime.Now - a.EditTime).TotalMinutes > 100) .Where((a,b) => (DateTime.Now - a.EditTime).TotalMinutes > 100)

View File

@ -922,7 +922,12 @@ namespace FreeSql.Internal
var pp = expStack.Pop() as ParameterExpression; var pp = expStack.Pop() as ParameterExpression;
var memberExp = expStack.Pop() as MemberExpression; var memberExp = expStack.Pop() as MemberExpression;
var tb = _common.GetTableByEntity(pp.Type); var tb = _common.GetTableByEntity(pp.Type);
if (tb.ColumnsByCs.ContainsKey(memberExp.Member.Name) == false) throw new ArgumentException($"{tb.DbName} 找不到列 {memberExp.Member.Name}"); if (tb.ColumnsByCs.ContainsKey(memberExp.Member.Name) == false)
{
if (tb.ColumnsByCsIgnore.ContainsKey(memberExp.Member.Name))
throw new ArgumentException($"{tb.DbName}.{memberExp.Member.Name} 被忽略,请检查 IsIgnore 设置,确认 get/set 为 public");
throw new ArgumentException($"{tb.DbName} 找不到列 {memberExp.Member.Name}");
}
if (tsc._selectColumnMap != null) if (tsc._selectColumnMap != null)
{ {
tsc._selectColumnMap.Add(new SelectColumnInfo { Table = null, Column = tb.ColumnsByCs[memberExp.Member.Name] }); tsc._selectColumnMap.Add(new SelectColumnInfo { Table = null, Column = tb.ColumnsByCs[memberExp.Member.Name] });
@ -1079,6 +1084,8 @@ namespace FreeSql.Internal
if (tb3.Columns.Any()) return ""; if (tb3.Columns.Any()) return "";
} }
} }
if (tb2.ColumnsByCsIgnore.ContainsKey(mp2.Member.Name))
throw new ArgumentException($"{tb2.DbName}.{mp2.Member.Name} 被忽略,请检查 IsIgnore 设置,确认 get/set 为 public");
throw new ArgumentException($"{tb2.DbName} 找不到列 {mp2.Member.Name}"); throw new ArgumentException($"{tb2.DbName} 找不到列 {mp2.Member.Name}");
} }
var col2 = tb2.ColumnsByCs[mp2.Member.Name]; var col2 = tb2.ColumnsByCs[mp2.Member.Name];