mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 12:28:15 +08:00
ICodeFirst 增加 ConfigEntity 方法,现实干净实体无特性的需求
This commit is contained in:
@ -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>
|
||||
/// 数据库默认值
|
||||
|
54
FreeSql/DataAnnotations/ColumnFluent.cs
Normal file
54
FreeSql/DataAnnotations/ColumnFluent.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
43
FreeSql/DataAnnotations/TableFluent.cs
Normal file
43
FreeSql/DataAnnotations/TableFluent.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user