dotnetcore / FreeSql

🦄 .NET aot orm, C# orm, VB.NET orm, Mysql orm, Postgresql orm, SqlServer orm, Oracle orm, Sqlite orm, Firebird orm, 达梦 orm, 人大金仓 orm, 神通 orm, 翰高 orm, 南大通用 orm, 虚谷 orm, 国产 orm, Clickhouse orm, QuestDB orm, MsAccess orm.
http://freesql.net
MIT License
3.99k stars 842 forks source link

IgnoreColumns不能忽略On Conflict Do Update的自增列吗? #1799

Open vla opened 3 weeks ago

vla commented 3 weeks ago

 [Table(Name = "CodeFirstEntity")]
 public class CodeFirstEntity
 {
     [Column(Name = "id", IsPrimary = true)]
     public long Id { get; set; }

     [Column(Name = "identity", IsIdentity = true, CanInsert = false)]
     public long Identity { get; set; }

     [Column(Name = "str_name", StringLength = 20, IsNullable = false)]
     public string str_name { get; set; } = string.Empty;
 }

var entity = new CodeFirstEntity
{
    Id = 1,
    str_name = "name"
};

var sql = fsql.Insert(entity)
         .IgnoreColumns(s => s.Identity)
         .NoneParameter()
         .OnConflictDoUpdate()
         .DoNothing()
         .ToSql();

 var sql = fsql.Insert(entity)
        .InsertColumns(s => new { s.Id, s.str_name })
        .NoneParameter()
        .OnConflictDoUpdate(q => q.Id)
        .DoNothing()
        .ToSql();

以上两种情况都会生成identity插入,只有删除IsIdentity特性才可以解决,但有时候是需要根据实体自动建表的,这个有办法解决吗?

    INSERT INTO CodeFirstEntity(id, identity, str_name) VALUES(1, 0, 'name')
    ON CONFLICT(id) DO NOTHING

忽略此列可否生成如下输出?

    INSERT INTO CodeFirstEntity(id, str_name) VALUES(1,  'name')
    ON CONFLICT(id) DO NOTHING
2881099 commented 3 weeks ago

是的,自增默认都会插入,可以考虑用一个新实体类型解决

vla commented 2 weeks ago

Reference in

ok, thx!