Skip to content

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

Closed

Description

@fishjimi

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

Activity

  1. 2881099 commented on Sep 26, 2019

    @2881099
    Collaborator

    可以有。

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

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

  2. fishjimi commented on Sep 26, 2019

    @fishjimi
    Author

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

  3. 2881099 commented on Sep 26, 2019

    @2881099
    Collaborator

    可以可以,技多不压身

  4. 2881099 commented on Sep 26, 2019

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

  5. fishjimi commented on Sep 26, 2019

    @fishjimi
    Author

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

  6. 2881099 commented on Sep 26, 2019

    @2881099
    Collaborator

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

    国产的优势,沟涌直接

  7. fishjimi commented on Sep 26, 2019

    @fishjimi
    Author

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

  8. 2881099 commented on Sep 26, 2019

    @2881099
    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions