Cloning data on Entity Framework

前端 未结 6 1699
轻奢々
轻奢々 2020-12-02 09:53

I am creating software where user can create new product based on older product.

Now I need to make copying / cloning operations with Entity Framework. First I start

6条回答
  •  忘掉有多难
    2020-12-02 10:22

    To add a new row whose content is based on an existing row, follow these steps:

    1. Get an entity based on the starting row.
    2. Set the entry state for the entity to Added.
    3. Modify the entity.
    4. Save changes.

    Here's an example:

    var rabbit = db.Rabbits.First(r => r.Name == "Hopper");
    db.Entry(rabbit).State = EntityState.Added;
    rabbit.IsFlop = false;
    db.SaveChanges();
    

提交回复
热议问题