Skip to content
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

Closed
fishjimi opened this issue Sep 26, 2019 · 8 comments
Closed

ColumnAttribute能否增加一个Write属性? #99

fishjimi opened this issue Sep 26, 2019 · 8 comments

Comments

@fishjimi
Copy link

参考Dapper.Contrib
可以为某列属性指定是否可写
当Write=false的时候,代表只可查,不可插入或更新
这样某些类似createtime updatetime这种在数据库自动填充的字段就不会被覆盖了。
免去了在代码里要指定IgnoreColumns的麻烦

@2881099
Copy link
Collaborator

2881099 commented Sep 26, 2019

可以有。

不过如果只是不想写 createtime updatetime 这样的字段,有 fsql.Aop.AuditValue 事件可以处理这个问题。

AOP文档地址:https://github.com/2881099/FreeSql/wiki/AOP

@fishjimi
Copy link
Author

@2881099 我昨天照着文档试了一下
似乎AOP只能默认赋值,而不能取消对某个属性的插入或更新
例如Where里面就可以Cancel掉
但是AuditValue里面只能修改值,不能干别的

@2881099
Copy link
Collaborator

2881099 commented Sep 26, 2019

可以可以,技多不压身

@2881099
Copy link
Collaborator

2881099 commented Sep 26, 2019

[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 该属性,则仍然会插入。

@fishjimi
Copy link
Author

大佬!!!神速啊!!!膜拜

@2881099
Copy link
Collaborator

2881099 commented Sep 26, 2019

大佬!!!神速啊!!!膜拜

国产的优势,沟涌直接

@fishjimi
Copy link
Author

@2881099 对啊,国人就是效率!
Dapper里面关于是否增加一个ColumnAttribute属性来设置数据库中字段名,已经讨论了2年了 还没什么进展。
明明都有TableAttribute可以指定表名了。太过于保持精简,反而觉得有点不好用了

@2881099
Copy link
Collaborator

2881099 commented Sep 26, 2019

刚推送了 nuget 包,过10分钟可以下载 v0.10.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants