1

I'm using asp.net mvc 4 and Entity Framework 6 to make a website where I can store data in MSSQL database. I want to make a function where it'll make a copy of an entry with different id. I want to put those copied values in an array and push it to the model to make the back-end processing faster. But I'm not sure how to do it.

Here are my codes:

    public ActionResult FlatCopy(FlManagement FlTable, int FlId = 0)
    {
        var getOwner = rentdb.OwnerRegs.Where(a => a.username == User.Identity.Name).FirstOrDefault();
        var getId = getOwner.serial;
        var getLimit = getOwner.limit;
        var getPosted = getOwner.posted;

        FlInfo model = FlTable.Flats;
        if (ModelState.IsValid)
        {
            if (getLimit != 0)
            {
                try
                {
                    getOwner.limit = getLimit - 1;
                    getOwner.posted = getPosted + 1;
                    var dbPost = rentdb.FlatInfoes.Where(p => p.serial == FlId).FirstOrDefault();
                    if (dbPost == null)
                    {
                        TempData["flat_edit_fail"] = "Error! Information update failed!";
                        return RedirectToAction("FlatManager", new { FlPanelId = "AllFl" });
                    }
                    model.flatno = "Copy of " + dbPost.flatno;
                    model.type = dbPost.type;
                    model.owner_id = getId;
                    model.t_id = 0;
                    model.t_name = "N/A";
                    model.t_phone = "N/A";
                    model.basic = dbPost.basic;
                    model.electric = dbPost.electric;
                    model.gas = dbPost.gas;
                    model.water = dbPost.water;
                    model.total = dbPost.total;
                    model.advancerent = dbPost.advancerent;
                    model.currency = dbPost.currency;
                    model.title = dbPost.title;
                    model.status = "N/A";
                    db.FlatInfoes.Add(model);
                    db.SaveChanges();

                    TempData["success"] = "Information Added Successfully!";
                    return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
                }
                catch
                {
                    TempData["fail"] = "Error! Please Provide Valid Information.";
                    return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
                }
            }
            else
            {
                TempData["fail"] = "You've reached your post limit!";
                return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
            }
        }
        else
        {
            TempData["fail"] = "Error! Please Provide Valid Information.";
            return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
        }
    }

How can I put the values in an array and push the array in model to add a new entry?

1 Answer 1

1

You could detach the entity from the DbContext then re-add it to the EntityCollection.

rentdb.Entry(dbPost).State = EntityState.Detached;
rentdb.FlatInfoes.Add(dbPost);

solution 2:(is better)

var model = new FlInfo();
rentdb.FlatInfoes.Add(model);
var sourceValues = rentdb.Entry(dbPost).CurrentValues;
rentdb.Entry(model).CurrentValues.SetValues(sourceValues);
rentdb.SaveChanges();
Sign up to request clarification or add additional context in comments.

4 Comments

Will the original copy be removed or it'll stay as it is?
original dbPost stay, and a new post added to db with new identity
Hi, Thanks a lot for your suggestion. I've tried your solutions but it seems I'm getting error everytime when I try to make a copy since the id of every record is auto-incremented in DB. Any idea how can I fix this?
If entity id is auto incremented, suggested code work, I test again. Plz add entities model to question for more checking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.