I have 4 model classes are: Relationship diagram classes
public class Father
{
public Father()
{
}
[Key]
public int Id { get; set; }
public string Name{ get; set; }
public decimal Amount{ get; set}
}
public class Record
{
public Record()
{
}
[Key]
public int Id { get; set; }
public string User{ get; set; }
public Datetime Date{ get; set}
public int fatherId{ get; set}
[ForeignKey("fatherId")]
public virtual Father Father{ get; set; }
}
public class Son: Father
{
public Son(){}
public int otherId { get; set}
[ForeignKey("otherId ")]
public virtual Other Other { get; set; }
}
public class Other
{
public Other ()
{
Son= new HashSet<Son>();
}
[Key]
public int Id { get; set; }
public datetime Date{ get; set; }
public string State { get; set}
[InverseProperty("Other")]
public virtual ICollection<Son> Son{ get; set; }
}
Exists in the database data from the parent class but when I try to insert data from the child class gives me duplicate key error, for example I have done this:
var father=context.father.find(1);
context.Entry(father).State=EntityState.Detached;
var son= new Son()
{
Id=father.Id;
otherId=2;
}
context.Entry(son).State=EntityState.Modified;
context.SaveChanges();
Data from the parent class can not be deleted because there is another kind that have data that refer to your data. I need to know if someone could manage to insert data from a child class when there is data from a parent class or rather Convert data from a father of a daughter data without modifying or changing the ID or primary key.
The Entity Framework is version 6 ..