ICodeFirst 增加 ConfigEntity 方法,现实干净实体无特性的需求

This commit is contained in:
28810
2019-01-22 12:56:45 +08:00
parent 0bcacc706a
commit 4bf8d60361
22 changed files with 270 additions and 65 deletions

View File

@ -16,18 +16,19 @@ namespace FreeSql.DataAnnotations {
/// </summary>
public string DbType { get; set; }
internal bool? _IsPrimary, _IsIdentity, _IsNullable;
/// <summary>
/// 主键
/// </summary>
public bool IsPrimary { get; set; }
public bool IsPrimary { get => _IsPrimary ?? false; set => _IsPrimary = value; }
/// <summary>
/// 自增标识
/// </summary>
public bool IsIdentity { get; set; }
public bool IsIdentity { get => _IsIdentity ?? false; set => _IsIdentity = value; }
/// <summary>
/// 是否可DBNull
/// </summary>
public bool IsNullable { get; set; }
public bool IsNullable { get => _IsNullable ?? false; set => _IsNullable = value; }
/// <summary>
/// 数据库默认值

View File

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

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
namespace FreeSql.DataAnnotations {
public class TableAttribute : Attribute {
@ -15,5 +16,7 @@ namespace FreeSql.DataAnnotations {
/// 查询过滤SQL实现类似 a.IsDeleted = 1 功能
/// </summary>
public string SelectFilter { get; set; }
internal ConcurrentDictionary<string, ColumnAttribute> _columns = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq.Expressions;
namespace FreeSql.DataAnnotations {
public class TableFluent<T> {
public TableFluent(TableAttribute table) {
_table = table;
}
TableAttribute _table;
/// <summary>
/// 数据库表名
/// </summary>
public TableFluent<T> Name(string value) {
_table.Name = value;
return this;
}
/// <summary>
/// 指定数据库旧的表名修改实体命名时同时设置此参数为修改之前的值CodeFirst才可以正确修改数据库表否则将视为【创建新表】
/// </summary>
public TableFluent<T> OldName(string value) {
_table.OldName = value;
return this;
}
/// <summary>
/// 查询过滤SQL实现类似 a.IsDeleted = 1 功能
/// </summary>
public TableFluent<T> SelectFilter(string value) {
_table.SelectFilter = value;
return this;
}
public ColumnFluent<TProto> 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);
}
}
}