Using Entity Framework 6 as well as the repository template, I am trying to update a row where an existing row has a new foreign key object associated with it. For example, a record Personalready exists. Persongets a new one Dog. So, now the class Personhas a property Dogthat is created and populated with data.
This is my update method:
public async Task<TObject> UpdateAsync(TObject updated, int key)
{
if (updated == null)
return null;
var existing = await this._context.Set<TObject>().FindAsync(key);
if (existing != null)
{
this._context.Entry(existing).CurrentValues.SetValues(updated);
}
await this._context.SaveChangesAsync();
return existing;
}
And I call it this way:
personRecord.Dog = new Dog () { Name = "Max", Age = 7 };
await this.PersonRepository.UpdateAsync(personRecord, personRecord.Id)
But the database is not a Dognew entry Dog. What am I doing wrong?
source
share