Entity Framework 6: Clone object except ID

前端 未结 5 1085
旧时难觅i
旧时难觅i 2020-12-01 04:48

In my MVVM program I have a Model class (say MyModel) from which I have an instance of reading from the database (using Entity Framework). When retrieving the o

5条回答
  •  醉酒成梦
    2020-12-01 05:22

    I use postgres database:

    CREATE TABLE public."Table" ( 
        "Id" integer NOT NULL DEFAULT nextval('"Table_Id_seq"'::regclass),
        ...
    

    No one of mentioned methods do not work in my case. I use secondly:

    Table table = _context.Table.AsNoTracking().Select(s => new Table {
     // some properties, exept id
        }).FirstOrDefault();
    _context.Table.Add(table);
    await _context.SaveChangesAsync();
    

提交回复
热议问题