v0.0.13 #4 - 修复和丰富 ICodeFirst.ConfigEntity 方法;

- 增加 FreeSql.Extensions.EFCoreModelBuilder 扩展库,现实与 EFCore 实体共存;
- 增加 FreeSql.RESTful.Demo 示例项目;
This commit is contained in:
28810
2019-02-14 19:07:52 +08:00
parent 45bc234160
commit 488a6edd4d
18 changed files with 190 additions and 22 deletions

View File

@ -1,7 +1,7 @@
using System;
namespace FreeSql.DataAnnotations {
public class ColumnFluent<T> {
public class ColumnFluent {
public ColumnFluent(ColumnAttribute column) {
_column = column;
@ -11,21 +11,21 @@ namespace FreeSql.DataAnnotations {
/// <summary>
/// 数据库列名
/// </summary>
public ColumnFluent<T> Name(string value) {
public ColumnFluent Name(string value) {
_column.Name = value;
return this;
}
/// <summary>
/// 指定数据库旧的列名修改实体属性命名时同时设置此参数为修改之前的值CodeFirst才可以正确修改数据库字段否则将视为【新增字段】
/// </summary>
public ColumnFluent<T> OldName(string value) {
public ColumnFluent OldName(string value) {
_column.OldName = value;
return this;
}
/// <summary>
/// 数据库类型,如: varchar(255)
/// </summary>
public ColumnFluent<T> DbType(string value) {
public ColumnFluent DbType(string value) {
_column.DbType = value;
return this;
}
@ -33,20 +33,20 @@ namespace FreeSql.DataAnnotations {
/// <summary>
/// 主键
/// </summary>
public ColumnFluent<T> IsPrimary(bool value) {
public ColumnFluent IsPrimary(bool value) {
_column.IsPrimary = value;
return this;
}
/// 自增标识
/// </summary>
public ColumnFluent<T> IsIdentity(bool value) {
public ColumnFluent IsIdentity(bool value) {
_column.IsIdentity = value;
return this;
}
/// <summary>
/// 是否可DBNull
/// </summary>
public ColumnFluent<T> IsNullable(bool value) {
public ColumnFluent IsNullable(bool value) {
_column.IsNullable = value;
return this;
}

View File

@ -17,6 +17,6 @@ namespace FreeSql.DataAnnotations {
/// </summary>
public string SelectFilter { get; set; }
internal ConcurrentDictionary<string, ColumnAttribute> _columns => new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
internal ConcurrentDictionary<string, ColumnAttribute> _columns { get; } = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
}
}

View File

@ -1,9 +1,51 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace FreeSql.DataAnnotations {
public class TableFluent {
public TableFluent(Type entityType, TableAttribute table) {
_entityType = entityType;
_properties = _entityType.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
_table = table;
}
Type _entityType;
Dictionary<string, PropertyInfo> _properties;
TableAttribute _table;
/// <summary>
/// 数据库表名
/// </summary>
public TableFluent Name(string value) {
_table.Name = value;
return this;
}
/// <summary>
/// 指定数据库旧的表名修改实体命名时同时设置此参数为修改之前的值CodeFirst才可以正确修改数据库表否则将视为【创建新表】
/// </summary>
public TableFluent OldName(string value) {
_table.OldName = value;
return this;
}
/// <summary>
/// 查询过滤SQL实现类似 a.IsDeleted = 1 功能
/// </summary>
public TableFluent SelectFilter(string value) {
_table.SelectFilter = value;
return this;
}
public ColumnFluent Property(string proto) {
if (_properties.ContainsKey(proto) == false) throw new KeyNotFoundException($"找不到属性名 {proto}");
var col = _table._columns.GetOrAdd(proto, name => new ColumnAttribute { Name = proto });
return new ColumnFluent(col);
}
}
public class TableFluent<T> {
public TableFluent(TableAttribute table) {
@ -33,11 +75,11 @@ namespace FreeSql.DataAnnotations {
return this;
}
public ColumnFluent<TProto> Property<TProto>(Expression<Func<T, TProto>> column) {
public ColumnFluent Property<TProto>(Expression<Func<T, TProto>> column) {
var proto = (column.Body as MemberExpression)?.Member;
if (proto == null) throw new FormatException($"错误的表达式格式 {column}");
var col = _table._columns.GetOrAdd(proto.Name, name => new ColumnAttribute { Name = proto.Name });
return new ColumnFluent<TProto>(col);
return new ColumnFluent(col);
}
}
}

View File

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

View File

@ -50,5 +50,6 @@ namespace FreeSql {
/// <returns></returns>
(int type, string dbtype, string dbtypeFull, bool? isnullable, object defaultValue)? GetDbInfo(Type type);
ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity);
ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity);
}
}

View File

@ -40,9 +40,15 @@ namespace FreeSql.Internal {
entity?.Invoke(fluent);
return _orm.CodeFirst;
}
internal TableAttribute GetEntityTableAttribute(Type entityType) {
var attr = entityType.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
if (dicConfigEntity.TryGetValue(entityType, out var trytb) == false) return attr;
internal ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) {
var table = dicConfigEntity.GetOrAdd(type, new TableAttribute());
var fluent = new TableFluent(type, table);
entity?.Invoke(fluent);
return _orm.CodeFirst;
}
internal TableAttribute GetEntityTableAttribute(Type type) {
var attr = type.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
if (dicConfigEntity.TryGetValue(type, out var trytb) == false) return attr;
if (attr == null) attr = new TableAttribute();
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trytb.Name;
@ -50,9 +56,9 @@ namespace FreeSql.Internal {
if (string.IsNullOrEmpty(attr.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
return attr;
}
internal ColumnAttribute GetEntityColumnAttribute(Type entityType, PropertyInfo proto) {
internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) {
var attr = proto.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
if (dicConfigEntity.TryGetValue(entityType, out var trytb) == false) return attr;
if (dicConfigEntity.TryGetValue(type, out var trytb) == false) return attr;
if (trytb._columns.TryGetValue(proto.Name, out var trycol) == false) return attr;
if (attr == null) attr = new ColumnAttribute();

View File

@ -272,5 +272,6 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(tboldname ?
}
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
public ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) => _commonUtils.ConfigEntity(type, entity);
}
}

View File

@ -310,5 +310,6 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
}
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
public ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) => _commonUtils.ConfigEntity(type, entity);
}
}

View File

@ -324,5 +324,6 @@ where pg_namespace.nspname={0} and pg_class.relname={1} and pg_constraint.contyp
}
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
public ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) => _commonUtils.ConfigEntity(type, entity);
}
}

View File

@ -282,5 +282,6 @@ use " + database, tboldname ?? tbname);
}
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
public ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) => _commonUtils.ConfigEntity(type, entity);
}
}

View File

@ -240,5 +240,6 @@ namespace FreeSql.Sqlite {
}
}
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) => _commonUtils.ConfigEntity(entity);
public ICodeFirst ConfigEntity(Type type, Action<TableFluent> entity) => _commonUtils.ConfigEntity(type, entity);
}
}