1

I am new in MVC Entity Framework.

I have one Table called Product. it contain 40 fields.

I have one task to copy the Product or to create the duplicate record in same table with new Product ID..

How can I do this in efficient ways?

I tried using the below code

Public ActionResult CopyProduct(long ProductID)
{
var oldProductScript = db.Products.FirstOrDefault(x=>x.ProductID == ProductID)


Product p = new Product();
p.name = oldProductScript.name;
p.price =oldProductScript.price;
p.model =oldProductScript.model;
p.image = oldProductScript.image;
p.status =oldProductScript.status;
.
.
.
.
.
like so till 40th field

db.Products.AddObject(p);
db.SaveChanges();

}

Is this the proper way to complete this task?

Please suggest.

Thanks

Bharath theorare
  • 524
  • 7
  • 27
ajay singh
  • 43
  • 5
  • It might be better to do the task in the stored procedure of the database I think... – Ian May 12 '16 at 08:34
  • 1
    I think the question is related to the C# code needed to copy the product. I suggest you to use an external library like AutoMapper that will do the perfect copy, with your custom behavior if needed, of the product. Hope this can help: http://automapper.org/ – Roberto Conte Rosito May 12 '16 at 08:36
  • Take a look here: http://stackoverflow.com/questions/78536/deep-cloning-objects. Would that do the trick for you? – LocEngineer May 12 '16 at 08:58

3 Answers3

0

If you want this to be done much easier(reduced coding), I would recommend you to use Code First Approach. Use this link to know what is it and code sample.

After you add the tables with models(DBContext) as using Code First Approach, you would be able to easily get and set properties of the table even faster than your usual method.

public class InheritanceMappingContext : DbContext
{
    public DbSet<TableName> TableProperty { get; set; }
}

Use a polymorphic query to retrieve the fields of the table.

IQueryable<TableName> linqQuery = from b in context.TableName select b;
List<TableName> tableFields = linqQuery.ToList(); 

linqQuery returns list of objects of the type

Bharath theorare
  • 524
  • 7
  • 27
  • My project is already in Database First Approach.. What about attach and detach of entity object ? – ajay singh May 12 '16 at 09:24
  • 2
    @ajaysingh It may not be an efficient way. The object that is passed to the Attach method must have a valid EntityKey value. If the object does not have a valid EntityKey value, use the AttachTo method to specify the name of the entity set. – Bharath theorare May 12 '16 at 09:30
  • @ajaysingh - If you want to use Database First approach, you could clone the object or rather serialize and transfer as JSON. It will be a considerable way to solve performance issues. – Bharath theorare May 12 '16 at 09:33
0

Try something like this:

        Products productToCopy = db.Products.FirstOrDefault(p => p.ProductId == productID);
        db.Entry(productToCopy).State = EntityState.Detached;
        productToCopy.ProductId = 0;
        db.Products.Add(productToCopy);
        db.SaveChanges();
Daniel Stackenland
  • 3,149
  • 1
  • 19
  • 22
0

Warning: I've tested this with one of my classes and it worked. Not sure if it will work with yours:

var prod = context.Products.FirstOrDefault(p => p.ProductID == ProductID);
var prod2 = new Products();
foreach (PropertyInfo pi in prod.GetType().GetProperties())
{
    prod2.GetType().GetProperties().First(p=>p.Name==pi.Name).SetValue(prod2, pi.GetValue(prod, null), null);
}

//prod2 is now a clone of prod. Make sure to adjust your product ID before adding the new product.
LocEngineer
  • 2,847
  • 1
  • 16
  • 28