- 增加 IndexAttribute 特性,自动迁移索引,以及对应的 FluentApi 方法;

- 移除 ColumnAttribute.Unique 属性设置,改为 IndexAttribute 特性设置唯一键;
This commit is contained in:
28810
2019-10-03 04:31:04 +08:00
parent cb18b74830
commit 78fded3f8e
43 changed files with 1010 additions and 607 deletions

View File

@ -3,6 +3,7 @@ using System.Linq;
namespace FreeSql.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute : Attribute
{
@ -42,35 +43,6 @@ namespace FreeSql.DataAnnotations
/// </summary>
public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; }
internal string[] _Uniques;
/// <summary>
/// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
/// </summary>
public string Unique
{
get => _Uniques == null ? null : string.Join(", ", _Uniques);
set
{
if (string.IsNullOrEmpty(value))
{
_Uniques = null;
return;
}
var val = value?.Trim(' ', '\t', ',');
if (string.IsNullOrEmpty(val))
{
_Uniques = null;
return;
}
var arr = val.Split(',').Select(a => a.Trim(' ', '\t').Trim()).Where(a => !string.IsNullOrEmpty(a)).ToArray();
if (arr.Any() == false)
{
_Uniques = null;
return;
}
_Uniques = arr;
}
}
/// <summary>
/// 数据库默认值
/// </summary>

View File

@ -77,16 +77,6 @@ namespace FreeSql.DataAnnotations
return this;
}
/// <summary>
/// 唯一键,在多个属性指定相同的标识,代表联合键;可使用逗号分割多个 UniqueKey 名。
/// </summary>
/// <param name="value">标识</param>
/// <returns></returns>
public ColumnFluent Unique(string value)
{
_column.Unique = value;
return this;
}
/// <summary>
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
/// </summary>
/// <param name="value"></param>

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
namespace FreeSql.DataAnnotations
{
[AttributeUsage(AttributeTargets.Class)]
public class TableAttribute : Attribute
{
@ -27,5 +29,36 @@ namespace FreeSql.DataAnnotations
internal ConcurrentDictionary<string, ColumnAttribute> _columns { get; } = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
internal ConcurrentDictionary<string, NavigateAttribute> _navigates { get; } = new ConcurrentDictionary<string, NavigateAttribute>(StringComparer.CurrentCultureIgnoreCase);
internal ConcurrentDictionary<string, IndexAttribute> _indexs { get; } = new ConcurrentDictionary<string, IndexAttribute>(StringComparer.CurrentCultureIgnoreCase);
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class IndexAttribute : Attribute
{
public IndexAttribute(string name, string fields)
{
this.Name = name;
this.Fields = fields;
}
public IndexAttribute(string name, string fields, bool isUnique)
{
this.Name = name;
this.Fields = fields;
this.IsUnique = isUnique;
}
/// <summary>
/// 索引名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 索引字段为属性名以逗号分隔Create_time ASC, Title ASC
/// </summary>
public string Fields { get; set; }
internal bool? _IsUnique;
/// <summary>
/// 是否唯一
/// </summary>
public bool IsUnique { get => _IsUnique ?? false; set => _IsUnique = value; }
}
}

View File

@ -59,6 +59,20 @@ namespace FreeSql.DataAnnotations
var col = _table._columns.GetOrAdd(proto, name => new ColumnAttribute { Name = proto });
return new ColumnFluent(col);
}
/// <summary>
/// 设置实体的索引
/// </summary>
/// <param name="name">索引名</param>
/// <param name="fields">索引字段为属性名以逗号分隔Create_time ASC, Title ASC</param>
/// <param name="isUnique">是否唯一</param>
/// <returns></returns>
public TableFluent Index(string name, string fields, bool isUnique = false)
{
var idx = new IndexAttribute(name, fields, isUnique);
_table._indexs.AddOrUpdate(name, idx, (_, __) => idx);
return this;
}
}
public class TableFluent<T>
@ -128,5 +142,19 @@ namespace FreeSql.DataAnnotations
_table._navigates.AddOrUpdate(member.Name, nav, (name, old) => nav);
return this;
}
/// <summary>
/// 设置实体的索引
/// </summary>
/// <param name="name">索引名</param>
/// <param name="fields">索引字段为属性名以逗号分隔Create_time ASC, Title ASC</param>
/// <param name="isUnique">是否唯一</param>
/// <returns></returns>
public TableFluent<T> Index(string name, string fields, bool isUnique = false)
{
var idx = new IndexAttribute(name, fields, isUnique);
_table._indexs.AddOrUpdate(name, idx, (_, __) => idx);
return this;
}
}
}