Referencing same Entity with different names in an Entity

asp.net-mvc c# entity-framework

Question

I have a model like:

public class Sample
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Guid SampleId { get; set; }

    public virtual SampleProperty Saloon { get; set; }
    public virtual SampleProperty Room { get; set; }
    public virtual SampleProperty Balcony { get; set; }
}

and a related entity:

public class SampleProperty
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public Piece Piece { get; set; }
    public FloorType Floor { get; set; }
    public WallType Wall { get; set; }
    public DoorType Door { get; set; }
    public WindowType Window { get; set; }
}

and ofcourse related Enums for the types in SampleProperty Entity.

Related controller is:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Sample(Sample model)
    {
        if (ModelState.IsValid)
        {
            db.Entry(model.Saloon).State = EntityState.Modified;
            db.Entry(model.Room).State = EntityState.Modified;
            db.Entry(model.Balcony).State = EntityState.Modified;
            db.Entry(model).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(model);
    }

Controller during get operation is:

    [HttpGet]
    public ActionResult Sample(Guid? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Sample @sample = db.Samples.Where(x => x.SampleId == id).FirstOrDefault();
        if (@sample == null)
        {
            return HttpNotFound();
        }
        @sample.Room = new SampleProperties();
        @sample.Saloon = new SampleProperties();
        @sample.Balcony = new SampleProperties();

        return View(@sample);
    }

By the way; I am including following properties in View;

                        @Html.HiddenFor(model => model.Saloon.Id)
                        @Html.HiddenFor(model => model.Room.Id)
                        @Html.HiddenFor(model => model.Balcony.Id)

My problem is: When I try to update this entity. I am having error below. Do you have any idea?

Attaching an entity of type 'Project.Models.SampleProperties' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

1
1
1/1/2014 9:13:08 PM

Popular Answer

In my case this exception was fired because I was Attaching the same Entity (class) with the same Pk twice. E.g.:

db.Users.Attach(dbRecord.CreatorUser);
db.Users.Attach(dbRecord.OwnerUser); //<- If CreatorUser== OwnerUser then ERROR

You can avoid this using a simple comparison:

db.Users.Attach(dbRecord.CreatorUser);
if (dbRecord.CreatorUser.Pk != dbRecord.OwnerUser.Pk)
{
    db.Cabinets.Attach(dbRecord.OwnerUser);
}
1
4/9/2014 2:53:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow