mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
- 增加 ColumnAttribute 可插入(CanInsert)、可更新(CanUpdate);#99
This commit is contained in:
parent
d4c766e0b6
commit
6ca226a8e4
@ -125,7 +125,6 @@ namespace FreeSql.Tests.DataAnnotations
|
|||||||
Assert.NotNull(find);
|
Assert.NotNull(find);
|
||||||
Assert.Equal(item.id, find.id);
|
Assert.Equal(item.id, find.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestIsIgnore
|
class TestIsIgnore
|
||||||
{
|
{
|
||||||
public Guid id { get; set; }
|
public Guid id { get; set; }
|
||||||
@ -133,5 +132,45 @@ namespace FreeSql.Tests.DataAnnotations
|
|||||||
[Column(IsIgnore = true)]
|
[Column(IsIgnore = true)]
|
||||||
public bool isignore { get; set; }
|
public bool isignore { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanInsert_CanUpdate()
|
||||||
|
{
|
||||||
|
var item = new TestCanInsert { title = "testtitle", testfield1 = 1000, testfield2 = 1000 };
|
||||||
|
var sql = g.mysql.Insert(item).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal("INSERT INTO `TestCanInsert`(`id`, `title`, `testfield2`) VALUES(?id_0, ?title_0, ?testfield2_0)", sql);
|
||||||
|
|
||||||
|
Assert.Equal(1, g.mysql.Insert(item).ExecuteAffrows());
|
||||||
|
var find = g.mysql.Select<TestCanInsert>().Where(a => a.id == item.id).First();
|
||||||
|
Assert.NotNull(find);
|
||||||
|
Assert.Equal(item.id, find.id);
|
||||||
|
Assert.Equal(item.title, find.title);
|
||||||
|
Assert.NotEqual(item.testfield1, find.testfield1);
|
||||||
|
Assert.Equal(0, find.testfield1);
|
||||||
|
Assert.Equal(item.testfield2, find.testfield2);
|
||||||
|
|
||||||
|
item.title = "testtitle_update";
|
||||||
|
item.testfield2 = 0;
|
||||||
|
sql = g.mysql.Update<TestCanInsert>().SetSource(item).ToSql().Replace("\r\n", "");
|
||||||
|
Assert.Equal($"UPDATE `TestCanInsert` SET `id` = ?p_0, `title` = ?p_1, `testfield1` = ?p_2 WHERE (`id` = '{item.id}')", sql);
|
||||||
|
|
||||||
|
Assert.Equal(1, g.mysql.Update<TestCanInsert>().SetSource(item).ExecuteAffrows());
|
||||||
|
find = g.mysql.Select<TestCanInsert>().Where(a => a.id == item.id).First();
|
||||||
|
Assert.NotNull(find);
|
||||||
|
Assert.Equal(item.id, find.id);
|
||||||
|
Assert.Equal(item.title, find.title);
|
||||||
|
Assert.Equal(item.testfield1, find.testfield1);
|
||||||
|
Assert.NotEqual(item.testfield2, find.testfield2);
|
||||||
|
Assert.Equal(1000, find.testfield1);
|
||||||
|
}
|
||||||
|
class TestCanInsert
|
||||||
|
{
|
||||||
|
public Guid id { get; set; }
|
||||||
|
public string title { get; set; }
|
||||||
|
[Column(CanInsert = false)]
|
||||||
|
public long testfield1 { get; set; }
|
||||||
|
[Column(CanUpdate = false)]
|
||||||
|
public long testfield2 { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,5 +94,15 @@ namespace FreeSql.DataAnnotations
|
|||||||
/// <0时排后面,...-3,-2,-1
|
/// <0时排后面,...-3,-2,-1
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public short Position { get => _Position ?? 0; set => _Position = value; }
|
public short Position { get => _Position ?? 0; set => _Position = value; }
|
||||||
|
|
||||||
|
internal bool? _CanInsert, _CanUpdate;
|
||||||
|
/// <summary>
|
||||||
|
/// 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略
|
||||||
|
/// </summary>
|
||||||
|
public bool CanInsert { get => _CanInsert ?? true; set => _CanInsert = value; }
|
||||||
|
/// <summary>
|
||||||
|
/// 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略
|
||||||
|
/// </summary>
|
||||||
|
public bool CanUpdate { get => _CanUpdate ?? true; set => _CanUpdate = value; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,5 +113,26 @@ namespace FreeSql.DataAnnotations
|
|||||||
_column.Position = value;
|
_column.Position = value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public ColumnFluent CanInsert(bool value)
|
||||||
|
{
|
||||||
|
_column.CanInsert = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public ColumnFluent CanUpdate(bool value)
|
||||||
|
{
|
||||||
|
_column.CanUpdate = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,16 @@
|
|||||||
<0时排后面,...-3,-2,-1
|
<0时排后面,...-3,-2,-1
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.CanInsert">
|
||||||
|
<summary>
|
||||||
|
该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.CanUpdate">
|
||||||
|
<summary>
|
||||||
|
该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Name(System.String)">
|
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Name(System.String)">
|
||||||
<summary>
|
<summary>
|
||||||
数据库列名
|
数据库列名
|
||||||
@ -140,6 +150,20 @@
|
|||||||
<param name="value"></param>
|
<param name="value"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.DataAnnotations.ColumnFluent.CanInsert(System.Boolean)">
|
||||||
|
<summary>
|
||||||
|
该字段是否可以插入,默认值true,指定为false插入时该字段会被忽略
|
||||||
|
</summary>
|
||||||
|
<param name="value"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.DataAnnotations.ColumnFluent.CanUpdate(System.Boolean)">
|
||||||
|
<summary>
|
||||||
|
该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略
|
||||||
|
</summary>
|
||||||
|
<param name="value"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="P:FreeSql.DataAnnotations.NavigateAttribute.Bind">
|
<member name="P:FreeSql.DataAnnotations.NavigateAttribute.Bind">
|
||||||
<summary>
|
<summary>
|
||||||
手工绑定 OneToMany、ManyToOne 导航关系
|
手工绑定 OneToMany、ManyToOne 导航关系
|
||||||
@ -2418,6 +2442,16 @@
|
|||||||
<param name="database"></param>
|
<param name="database"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.Internal.CommonProvider.InsertProvider`1.IgnoreCanInsert">
|
||||||
|
<summary>
|
||||||
|
AsType, Ctor, ClearData 三处地方需要重新加载
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:FreeSql.Internal.CommonProvider.UpdateProvider`1.IgnoreCanUpdate">
|
||||||
|
<summary>
|
||||||
|
AsType, Ctor, ClearData 三处地方需要重新加载
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.Internal.CommonUtils.GetProperyCommentBySummary(System.Type)">
|
<member name="M:FreeSql.Internal.CommonUtils.GetProperyCommentBySummary(System.Type)">
|
||||||
<summary>
|
<summary>
|
||||||
通过属性的注释文本,通过 xml 读取
|
通过属性的注释文本,通过 xml 读取
|
||||||
|
@ -34,14 +34,26 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
_table = _commonUtils.GetTableByEntity(typeof(T1));
|
_table = _commonUtils.GetTableByEntity(typeof(T1));
|
||||||
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
|
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
|
||||||
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
||||||
|
IgnoreCanInsert();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AsType, Ctor, ClearData 三处地方需要重新加载
|
||||||
|
/// </summary>
|
||||||
|
protected void IgnoreCanInsert()
|
||||||
|
{
|
||||||
|
if (_table == null || _table.Type == typeof(object)) return;
|
||||||
|
foreach (var col in _table.Columns.Values)
|
||||||
|
if (col.Attribute.CanInsert == false)
|
||||||
|
_ignore.Add(col.Attribute.Name, true);
|
||||||
|
}
|
||||||
protected void ClearData()
|
protected void ClearData()
|
||||||
{
|
{
|
||||||
_insertIdentity = false;
|
_insertIdentity = false;
|
||||||
_source.Clear();
|
_source.Clear();
|
||||||
_ignore.Clear();
|
_ignore.Clear();
|
||||||
_params = null;
|
_params = null;
|
||||||
|
IgnoreCanInsert();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IInsert<T1> WithTransaction(DbTransaction transaction)
|
public IInsert<T1> WithTransaction(DbTransaction transaction)
|
||||||
@ -569,6 +581,7 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
var newtb = _commonUtils.GetTableByEntity(entityType);
|
var newtb = _commonUtils.GetTableByEntity(entityType);
|
||||||
_table = newtb ?? throw new Exception("IInsert.AsType 参数错误,请传入正确的实体类型");
|
_table = newtb ?? throw new Exception("IInsert.AsType 参数错误,请传入正确的实体类型");
|
||||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
||||||
|
IgnoreCanInsert();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,8 +40,19 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
|
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
|
||||||
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
|
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
|
||||||
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
|
||||||
|
IgnoreCanUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AsType, Ctor, ClearData 三处地方需要重新加载
|
||||||
|
/// </summary>
|
||||||
|
protected void IgnoreCanUpdate()
|
||||||
|
{
|
||||||
|
if (_table == null || _table.Type == typeof(object)) return;
|
||||||
|
foreach (var col in _table.Columns.Values)
|
||||||
|
if (col.Attribute.CanUpdate == false)
|
||||||
|
_ignore.Add(col.Attribute.Name, true);
|
||||||
|
}
|
||||||
protected void ClearData()
|
protected void ClearData()
|
||||||
{
|
{
|
||||||
_source.Clear();
|
_source.Clear();
|
||||||
@ -51,6 +62,7 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
_setIncr.Clear();
|
_setIncr.Clear();
|
||||||
_params.Clear();
|
_params.Clear();
|
||||||
_paramsSource.Clear();
|
_paramsSource.Clear();
|
||||||
|
IgnoreCanUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IUpdate<T1> WithTransaction(DbTransaction transaction)
|
public IUpdate<T1> WithTransaction(DbTransaction transaction)
|
||||||
@ -610,6 +622,7 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
var newtb = _commonUtils.GetTableByEntity(entityType);
|
var newtb = _commonUtils.GetTableByEntity(entityType);
|
||||||
_table = newtb ?? throw new Exception("IUpdate.AsType 参数错误,请传入正确的实体类型");
|
_table = newtb ?? throw new Exception("IUpdate.AsType 参数错误,请传入正确的实体类型");
|
||||||
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
|
||||||
|
IgnoreCanUpdate();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,6 +128,8 @@ namespace FreeSql.Internal
|
|||||||
if (trycol._Uniques != null) attr._Uniques = trycol._Uniques;
|
if (trycol._Uniques != null) attr._Uniques = trycol._Uniques;
|
||||||
if (trycol.MapType != null) attr.MapType = trycol.MapType;
|
if (trycol.MapType != null) attr.MapType = trycol.MapType;
|
||||||
if (trycol._Position != null) attr._Position = trycol.Position;
|
if (trycol._Position != null) attr._Position = trycol.Position;
|
||||||
|
if (trycol._CanInsert != null) attr._CanInsert = trycol.CanInsert;
|
||||||
|
if (trycol._CanUpdate != null) attr._CanUpdate = trycol.CanUpdate;
|
||||||
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
|
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
|
||||||
}
|
}
|
||||||
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
|
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
|
||||||
@ -146,6 +148,8 @@ namespace FreeSql.Internal
|
|||||||
if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques;
|
if (tryattr._Uniques != null) attr._Uniques = tryattr._Uniques;
|
||||||
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
|
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
|
||||||
if (tryattr._Position != null) attr._Position = tryattr.Position;
|
if (tryattr._Position != null) attr._Position = tryattr.Position;
|
||||||
|
if (tryattr._CanInsert != null) attr._CanInsert = tryattr.CanInsert;
|
||||||
|
if (tryattr._CanUpdate != null) attr._CanUpdate = tryattr.CanUpdate;
|
||||||
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
|
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
|
||||||
}
|
}
|
||||||
ColumnAttribute ret = null;
|
ColumnAttribute ret = null;
|
||||||
@ -160,6 +164,8 @@ namespace FreeSql.Internal
|
|||||||
if (attr._Uniques != null) ret = attr;
|
if (attr._Uniques != null) ret = attr;
|
||||||
if (attr.MapType != null) ret = attr;
|
if (attr.MapType != null) ret = attr;
|
||||||
if (attr._Position != null) ret = attr;
|
if (attr._Position != null) ret = attr;
|
||||||
|
if (attr._CanInsert != null) ret = attr;
|
||||||
|
if (attr._CanUpdate != null) ret = attr;
|
||||||
if (attr.DbDefautValue != null) ret = attr;
|
if (attr.DbDefautValue != null) ret = attr;
|
||||||
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;
|
if (ret != null && ret.MapType == null) ret.MapType = proto.PropertyType;
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user