v0.3.12 增加 ICodeFirst.IsConfigEntityFromDbFirst,若无配置实体类主键、自增,可从数据库导入;

This commit is contained in:
28810
2019-03-11 19:00:44 +08:00
parent 4f66c3b9eb
commit dae8eb7a67
17 changed files with 81 additions and 17 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.3.11</Version>
<Version>0.3.12</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>打造 .NETCore 最方便的 ORMDbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite读写分离、分表分库。</Description>

View File

@ -15,6 +15,7 @@ namespace FreeSql {
bool _isAutoSyncStructure = false;
bool _isSyncStructureToLower = false;
bool _isSyncStructureToUpper = false;
bool _isConfigEntityFromDbFirst = false;
bool _isLazyLoading = false;
Action<DbCommand> _aopCommandExecuting = null;
Action<DbCommand, string> _aopCommandExecuted = null;
@ -86,6 +87,15 @@ namespace FreeSql {
return this;
}
/// <summary>
/// 使用数据库的主键和自增,适用 DbFirst 模式,无须在实体类型上设置 [Column(IsPrimary)] 或者 ConfigEntity。此功能目前可用于 mysql/sqlserver/postgresql。
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public FreeSqlBuilder UseConfigEntityFromDbFirst(bool value) {
_isConfigEntityFromDbFirst = value;
return this;
}
/// <summary>
/// 延时加载导航属性对象,导航属性需要声明 virtual
/// </summary>
/// <param name="value"></param>
@ -120,6 +130,7 @@ namespace FreeSql {
ret.CodeFirst.IsSyncStructureToLower = _isSyncStructureToLower;
ret.CodeFirst.IsSyncStructureToUpper = _isSyncStructureToUpper;
ret.CodeFirst.IsConfigEntityFromDbFirst = _isConfigEntityFromDbFirst;
ret.CodeFirst.IsLazyLoading = _isLazyLoading;
var ado = ret.Ado as Internal.CommonProvider.AdoProvider;
ado.AopCommandExecuting += _aopCommandExecuting;

View File

@ -18,6 +18,10 @@ namespace FreeSql {
/// </summary>
bool IsSyncStructureToUpper { get; set; }
/// <summary>
/// 使用数据库的主键和自增,适用 DbFirst 模式,无须在实体类型上设置 [Column(IsPrimary)] 或者 ConfigEntity。此功能目前可用于 mysql/sqlserver/postgresql。
/// </summary>
bool IsConfigEntityFromDbFirst { get; set; }
/// <summary>
/// 延时加载导航属性对象,导航属性需要声明 virtual
/// </summary>
bool IsLazyLoading { get; set; }

View File

@ -1,4 +1,5 @@
using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal.Model;
using System;
using System.Collections;
@ -27,6 +28,8 @@ namespace FreeSql.Internal {
internal IFreeSql _orm { get; set; }
internal ICodeFirst CodeFirst => _orm.CodeFirst;
internal TableInfo GetTableByEntity(Type entity) => Utils.GetTableByEntity(entity, this);
internal List<DbTableInfo> dbTables { get; set; }
internal object dbTablesLock = new object();
public CommonUtils(IFreeSql orm) {
_orm = orm;

View File

@ -113,8 +113,8 @@ namespace FreeSql.Internal {
}
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (trytb.Primarys.Any() == false) {
var identcols = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).FirstOrDefault();
if (identcols != null) trytb.Primarys = new[] { identcols };
var identcol = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity == true).FirstOrDefault();
if (identcol != null) trytb.Primarys = new[] { identcol };
if (trytb.Primarys.Any() == false) {
trytb.Primarys = trytb.Columns.Values.Where(a => string.Compare(a.Attribute.Name, "id", true) == 0).ToArray();
if (trytb.Primarys.Any() == false) {
@ -127,6 +127,34 @@ namespace FreeSql.Internal {
foreach (var col in trytb.Primarys)
col.Attribute.IsPrimary = true;
}
//从数据库查找主键、自增
if (common.CodeFirst.IsConfigEntityFromDbFirst) {
try {
if (common._orm.DbFirst != null) {
if (common.dbTables == null)
lock (common.dbTablesLock)
if (common.dbTables == null)
common.dbTables = common._orm.DbFirst.GetTablesByDatabase();
var finddbtbs = common.dbTables.Where(a => string.Compare(a.Name, trytb.CsName, true) == 0 || string.Compare(a.Name, trytb.DbName, true) == 0);
foreach (var dbtb in finddbtbs) {
foreach (var dbident in dbtb.Identitys) {
if (trytb.Columns.TryGetValue(dbident.Name, out var trycol) && trycol.CsType == dbident.CsType ||
trytb.ColumnsByCs.TryGetValue(dbident.Name, out trycol) && trycol.CsType == dbident.CsType) {
trycol.Attribute.IsIdentity = true;
}
}
foreach (var dbpk in dbtb.Primarys) {
if (trytb.Columns.TryGetValue(dbpk.Name, out var trycol) && trycol.CsType == dbpk.CsType ||
trytb.ColumnsByCs.TryGetValue(dbpk.Name, out trycol) && trycol.CsType == dbpk.CsType) {
trycol.Attribute.IsPrimary = true;
}
}
}
}
} catch { }
trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
}
tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb);
#region virtual

View File

@ -26,6 +26,7 @@ namespace FreeSql.MySql {
public bool IsAutoSyncStructure { get; set; } = true;
public bool IsSyncStructureToLower { get; set; } = false;
public bool IsSyncStructureToUpper { get; set; } = false;
public bool IsConfigEntityFromDbFirst { get; set; } = false;
public bool IsLazyLoading { get; set; } = false;
static object _dicCsToDbLock = new object();

View File

@ -136,12 +136,18 @@ namespace FreeSql.MySql {
return ds.Select(a => a.FirstOrDefault()?.ToString()).ToList();
}
public List<DbTableInfo> GetTablesByDatabase(params string[] database) {
public List<DbTableInfo> GetTablesByDatabase(params string[] database2) {
var loc1 = new List<DbTableInfo>();
var loc2 = new Dictionary<string, DbTableInfo>();
var loc3 = new Dictionary<string, Dictionary<string, DbColumnInfo>>();
var database = database2?.ToArray();
if (database == null || database.Any() == false) return loc1;
if (database == null || database.Any() == false) {
using (var conn = _orm.Ado.MasterPool.Get()) {
if (string.IsNullOrEmpty(conn.Value.Database)) return loc1;
database = new[] { conn.Value.Database };
}
}
var databaseIn = string.Join(",", database.Select(a => "{0}".FormatMySql(a)));
var sql = string.Format(@"
select

View File

@ -27,6 +27,7 @@ namespace FreeSql.Oracle {
public bool IsQuoteSqlName { get; set; } = true;
public bool IsSyncStructureToLower { get; set; } = false;
public bool IsSyncStructureToUpper { get; set; } = false;
public bool IsConfigEntityFromDbFirst { get; set; } = false;
public bool IsLazyLoading { get; set; } = false;
static object _dicCsToDbLock = new object();

View File

@ -25,7 +25,7 @@ namespace FreeSql.Oracle {
public IAdo Ado { get; }
public ICache Cache { get; }
public ICodeFirst CodeFirst { get; }
public IDbFirst DbFirst { get { throw new NotImplementedException(); } }
public IDbFirst DbFirst => null;
public OracleProvider(IDistributedCache cache, ILogger log, string masterConnectionString, string[] slaveConnectionString) {
if (log == null) log = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }).CreateLogger("FreeSql.Oracle");

View File

@ -31,6 +31,7 @@ namespace FreeSql.PostgreSQL {
public bool IsAutoSyncStructure { get; set; } = true;
public bool IsSyncStructureToLower { get; set; } = false;
public bool IsSyncStructureToUpper { get; set; } = false;
public bool IsConfigEntityFromDbFirst { get; set; } = false;
public bool IsLazyLoading { get; set; } = false;
static object _dicCsToDbLock = new object();

View File

@ -25,6 +25,7 @@ namespace FreeSql.SqlServer {
public bool IsAutoSyncStructure { get; set; } = true;
public bool IsSyncStructureToLower { get; set; } = false;
public bool IsSyncStructureToUpper { get; set; } = false;
public bool IsConfigEntityFromDbFirst { get; set; } = false;
public bool IsLazyLoading { get; set; } = false;
static object _dicCsToDbLock = new object();

View File

@ -25,6 +25,7 @@ namespace FreeSql.Sqlite {
public bool IsAutoSyncStructure { get; set; } = true;
public bool IsSyncStructureToLower { get; set; } = false;
public bool IsSyncStructureToUpper { get; set; } = false;
public bool IsConfigEntityFromDbFirst { get; set; } = false;
public bool IsLazyLoading { get; set; } = false;
static object _dicCsToDbLock = new object();

View File

@ -25,7 +25,7 @@ namespace FreeSql.Sqlite {
public IAdo Ado { get; }
public ICache Cache { get; }
public ICodeFirst CodeFirst { get; }
public IDbFirst DbFirst { get { throw new NotImplementedException(); } }
public IDbFirst DbFirst => null;
public SqliteProvider(IDistributedCache cache, ILogger log, string masterConnectionString, string[] slaveConnectionString) {
if (log == null) log = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }).CreateLogger("FreeSql.Sqlite");