I have a simple relationship:
I have a "Note" object, which has a related "Company" object. When creating a "Note", you select an existing "Company", and enter the other neccessary fields.
My issue is that whenever I save, it inserts a new version of the "Company" object, and I can't figure out why.
I have tried all the options in the accepted answer here Insert new object with existing object but nothing works.
Currently, I am doing the following:
var company = db.Company.FirstOrDefault(i => i.ID == note.Company.ID);
note.Company = company;
db.Notes.AddObject(note);
db.ObjectStateManager.ChangeObjectState(company, EntityState.Unchanged);
db.ObjectStateManager.ChangeObjectState(note, EntityState.Added);
db.SaveChanges();
But, it keeps adding a new "Company" object. What is strange, is that it doesn't add all the fields, just the CompanyName" field, (and creates a new Id).