0

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).

Community
  • 1
  • 1
mp3duck
  • 2,633
  • 7
  • 26
  • 40

5 Answers5

0

If this is an existing company, set the CompantID field and not the entity

note.CompanyID = company.ID;
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
0

I think the problem is with your first line when use use note.Company.Id in the FirstorDefault method. If you could post the entrie method I may be able to help further. If you have companyId as a parameter coming into the method then you could rework the code above as follows:

Company company = db.Company.Find(companyId);
note.Company = company;
db.SaveChanges();

Or as the other answer suggests just set note.CompanyId

Luke Baughan
  • 4,658
  • 3
  • 31
  • 54
0

The problem is you are creating a new instance of Note and setting the Company property therefore EF which I assume is re-creating the company record. If you are trying to link your Note to an existing Company object them just set the foreign key for the company on the note e.g.

var company = db.Company.FirstOrDefault(i => i.ID == note.Company.ID);    
note.CompanyID = company.ID; // will link the note to the specified company
db.Notes.AddObject(note);
db.SaveChanges();

It also doesn't make sense that you are using note.Company.ID to pull the company down, this property should be null if it's a new Note.

James
  • 80,725
  • 18
  • 167
  • 237
0

Here is how i do it, hope it helps.

using (DataContext dtx = new DataContext()) {

            Note note = new Note();

            dtx.Notes.Add(note);

            Company company = dtx.Companies.FirstOrDefault((System.Object obj) => obj.ServerId == 1);

            note.Server = company;

            dtx.SaveChanges();

        }
Afshin Gh
  • 7,918
  • 2
  • 26
  • 43
0

Thanks for every ones responses.

My problem in the end was, firstly, when creating my model for the page, I instantiated an initial new "Company", so I set company to null for creating.

Then, on the page, I had Html.TextBoxFor(model => model.Company.CompanyName,.... (it's an auto complete ajax text box that sets a hidden companyID field when chosen) when was meaning a new Company object was getting passed back to the controller.

I did try setting this to null before setting the company ID, but that didn't work. However, removing the above textboxfor bit, everything started working!

mp3duck
  • 2,633
  • 7
  • 26
  • 40