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

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

Open
vla opened this issue Apr 30, 2024 · 2 comments
Open

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

vla opened this issue Apr 30, 2024 · 2 comments

Comments

@vla
Copy link

vla commented Apr 30, 2024

 [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
  • postgresql16
  • FreeSql.Provider.PostgreSQL 3.2.821
  • net8.0

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

    INSERT INTO CodeFirstEntity(id, str_name) VALUES(1,  'name')
    ON CONFLICT(id) DO NOTHING
@2881099
Copy link
Collaborator

2881099 commented Apr 30, 2024

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

@vla
Copy link
Author

vla commented May 6, 2024

Reference in

ok, thx!

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