-
Notifications
You must be signed in to change notification settings - Fork 856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ColumnAttribute能否增加一个Write属性? #99
Comments
可以有。 不过如果只是不想写 createtime updatetime 这样的字段,有 fsql.Aop.AuditValue 事件可以处理这个问题。 AOP文档地址:https://github.com/2881099/FreeSql/wiki/AOP |
@2881099 我昨天照着文档试了一下 |
可以可以,技多不压身 |
[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; }
} 使用方法,单元测试通过了。 当指明了 InsertColumn/UpdateColumns 等方法时,该特性作用可能失效。例如 CanInsert = false 时,又指明了 InsertColumns 该属性,则仍然会插入。 |
大佬!!!神速啊!!!膜拜 |
国产的优势,沟涌直接 |
@2881099 对啊,国人就是效率! |
刚推送了 nuget 包,过10分钟可以下载 v0.10.4 |
参考Dapper.Contrib
可以为某列属性指定是否可写
当Write=false的时候,代表只可查,不可插入或更新
这样某些类似createtime updatetime这种在数据库自动填充的字段就不会被覆盖了。
免去了在代码里要指定IgnoreColumns的麻烦
The text was updated successfully, but these errors were encountered: