I am using Entity Framework. I have a table Person that has a foreign key on Table PersonCategory. Person Id is an auto-incremented identity (1,1). Each time i update the person entity the entity framework inserts a row for PersonCategory Table Even if category is found.
I am using Database first design.
class Person
{
public string Name {get;set}
public PersonCategory {get;set;}
}
class PersonCategory
{
public int ID {get;set}
public string Name{get;set;}
}
First you need to attach the existing PersonCategory
to the context;
var myPersonCat = new PersonCategory { ID = 1, Name = "Foo" };
var myPerson = new Person { Name = "Bar", PersonCategory = myPersonCat };
context.PersonCategories.Attach(myPersonCat);
context.People.Add(myPerson);