mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 增加 NavigateAttribute 配置导航关系;
- 修复 LinqToSql 方法,开启自动迁移时,迁移了无关类的 bug; - 修复 Oracle DbFirst date(7) 类型未处理的 bug;#57 - 修复 AsSelect().Any() 未给其他条件时,产生 null bug; - 增加 FreeSql.Extensions.LazyLoading 对 .net 4.5 的支持; - 优化 MySql CodeFirst 增加 DateTime 迁移后,默认值为 0000-00-00 导致读取失败的 bug; - 优化 LazyLoading 友好错误提示;
This commit is contained in:
75
FreeSql/Internal/CommonProvider/CodeFirstProvider.cs
Normal file
75
FreeSql/Internal/CommonProvider/CodeFirstProvider.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql.Internal.CommonProvider {
|
||||
|
||||
public abstract partial class CodeFirstProvider : ICodeFirst {
|
||||
|
||||
protected IFreeSql _orm;
|
||||
protected CommonUtils _commonUtils;
|
||||
protected CommonExpression _commonExpression;
|
||||
public CodeFirstProvider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression) {
|
||||
_orm = orm;
|
||||
_commonUtils = commonUtils;
|
||||
_commonExpression = commonExpression;
|
||||
}
|
||||
|
||||
public bool IsAutoSyncStructure { get; set; } = false;
|
||||
public bool IsSyncStructureToLower { get; set; } = false;
|
||||
public bool IsSyncStructureToUpper { get; set; } = false;
|
||||
public bool IsConfigEntityFromDbFirst { get; set; } = false;
|
||||
public bool IsNoneCommandParameter { get; set; } = false;
|
||||
public bool IsLazyLoading { get; set; } = false;
|
||||
|
||||
public abstract (int type, string dbtype, string dbtypeFull, bool? isnullable, object defaultValue)? GetDbInfo(Type type);
|
||||
|
||||
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
|
||||
public ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) => _commonUtils.ConfigEntity(type, entity);
|
||||
public TableAttribute GetConfigEntity(Type type) => _commonUtils.GetConfigEntity(type);
|
||||
public TableInfo GetTableByEntity(Type type) => _commonUtils.GetTableByEntity(type);
|
||||
|
||||
public string GetComparisonDDLStatements<TEntity>() => this.GetComparisonDDLStatements(typeof(TEntity));
|
||||
public abstract string GetComparisonDDLStatements(params Type[] entityTypes);
|
||||
|
||||
static object syncStructureLock = new object();
|
||||
internal ConcurrentDictionary<string, bool> dicSyced = new ConcurrentDictionary<string, bool>();
|
||||
public bool SyncStructure<TEntity>() => this.SyncStructure(typeof(TEntity));
|
||||
public bool SyncStructure(params Type[] entityTypes) {
|
||||
if (entityTypes == null) return true;
|
||||
var syncTypes = entityTypes.Where(a => dicSyced.ContainsKey(a.FullName) == false).ToArray();
|
||||
if (syncTypes.Any() == false) return true;
|
||||
var before = new Aop.SyncStructureBeforeEventArgs(entityTypes);
|
||||
_orm.Aop.SyncStructureBefore?.Invoke(this, before);
|
||||
Exception exception = null;
|
||||
string ddl = null;
|
||||
try {
|
||||
lock (syncStructureLock) {
|
||||
ddl = this.GetComparisonDDLStatements(syncTypes);
|
||||
if (string.IsNullOrEmpty(ddl)) {
|
||||
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||
return true;
|
||||
}
|
||||
var affrows = _orm.Ado.ExecuteNonQuery(CommandType.Text, ddl);
|
||||
foreach (var syncType in syncTypes) dicSyced.TryAdd(syncType.FullName, true);
|
||||
return affrows > 0;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
exception = ex;
|
||||
throw ex;
|
||||
} finally {
|
||||
var after = new Aop.SyncStructureAfterEventArgs(before, ddl, exception);
|
||||
_orm.Aop.SyncStructureAfter?.Invoke(this, after);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -159,6 +159,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
if (typeof(TReturn) == typeof(T1)) return this as ISelect<TReturn>;
|
||||
_tables[0].Parameter = select.Parameters[0];
|
||||
_selectExpression = select.Body;
|
||||
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TReturn).FullName, true);
|
||||
var ret = _orm.Select<TReturn>();
|
||||
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
|
||||
return ret;
|
||||
@ -172,6 +173,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
), SelectTableInfoType.InnerJoin);
|
||||
if (typeof(TResult) == typeof(T1)) return this as ISelect<TResult>;
|
||||
_selectExpression = resultSelector.Body;
|
||||
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TResult).FullName, true);
|
||||
var ret = _orm.Select<TResult>() as Select1Provider<TResult>;
|
||||
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
|
||||
return ret;
|
||||
@ -185,6 +187,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
), SelectTableInfoType.InnerJoin);
|
||||
if (typeof(TResult) == typeof(T1)) return this as ISelect<TResult>;
|
||||
_selectExpression = resultSelector.Body;
|
||||
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TResult).FullName, true);
|
||||
var ret = _orm.Select<TResult>() as Select1Provider<TResult>;
|
||||
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
|
||||
return ret;
|
||||
@ -211,6 +214,7 @@ namespace FreeSql.Internal.CommonProvider {
|
||||
}
|
||||
if (typeof(TResult) == typeof(T1)) return this as ISelect<TResult>;
|
||||
_selectExpression = resultSelector.Body;
|
||||
(_orm.CodeFirst as CodeFirstProvider).dicSyced.TryAdd(typeof(TResult).FullName, true);
|
||||
var ret = _orm.Select<TResult>() as Select1Provider<TResult>;
|
||||
Select0Provider<ISelect<T1>, T1>.CopyData(this, ret, null);
|
||||
return ret;
|
||||
|
Reference in New Issue
Block a user