All I want is just to create an exact copy of an object.
I have a class
[Serializable]
public class Project
{
public int Id { get; set; }
public String Name { get; set; }
//navigational fields..
public virtual List<BusinessRequirement> BusinessRequirements { get; set; }
}
and another
[Serializable]
public class BusinessRequirement
{
public int Id { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public virtual List<Project> Projects { get; set; }
}
so somewhere I've configured the many-to-many relationship b/w Project
and BusinessRequirement
like this:
HasMany(s => s.BusinessRequirements)
.WithMany(s => s.Projects)
.Map(m =>
{
m.MapLeftKey("ProjectId");
m.MapRightKey("BusinessRequirementId");
m.ToTable("ProjectBusinessRequirementMapping");
});
and also I've made my dbcontext static i.e.
public static class DataLayer
{
public static MyDbContext db;
}
now, all m doing is, trying to make a copy of an object of Project
i.e.
public Project Clone(Project source)
{
Project target = new Project();
target.Name = source.Name;
//1.
// target = source;
//2.
target.BusinessRequirements = new List<BusinessRequirement>();
foreach(BusinessRequirement br in source.BusinessRequirements)
{
BusinessRequirement nbr = DataLayer.Get<BusinessRequirement>(s=>s.Id=br.Id).SingleOrDefault();
if(nbr!=null)
target.BusinessRequirements.Add(nbr);
}
//3.
//target.BusinessRequirements = source.BusinessRequirements;
//4.
//target.BusinessRequirements = new List<BusinessRequirement>();
//foreach(BusinessRequirement br in source.BusinessRequirements)
//{
// BusinessRequirement nbr = br;
// if(nbr!=null)
// target.BusinessRequirements.Add(nbr);
//}
return target;
}
none of the four methods work properly.
the one which is closest to working is 2, but a strange thing happens.
Now, If i add any BusinessRequirements to Original Project
, it also gets added to Clonned One
and vice-versa, same goes for deletion.
Somehow, entityframework is treating both the projects as one. Though this behavior occurs only in many-to-many related navigational properties.
Why EntityFramework is behaving like this???. What am I missing? please help..
Its been almost a day, but I cannot get it to work.
I have tried this, this, this and this but they didn't work either..