0

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

  • You can't have two entries into the table with the same identity. In the son, set otherId = father.Id and assign a new Id to son. Or conversely, you might add a parentId to Son and set it to father.Id. – Kevin Sep 09 '16 at 19:26
  • otherId is foriengn key of the other table you have reference it to son class so you can insert to other field if an only if it is have in other table please check the table their is data with id 2 then only you can insert it to sun table – Shakir Ahamed Sep 09 '16 at 19:30
  • The Son class has the same attributes of the parent class but the Father can not be related to Other class, so the Son is the class that is related to Other class. Ie, there may be data type Father who have no communication with Other data can exist and Son who have communication with Other class. But the data in the Father class can not be deleted. – Frank Núñez Rodríguez Sep 09 '16 at 20:24
  • Possible duplicate of [EF Codefirst Convert Base Class to Derived Class](http://stackoverflow.com/questions/9467643/ef-codefirst-convert-base-class-to-derived-class) – Sam I am says Reinstate Monica Sep 09 '16 at 20:25
  • 2
    Please fix the indentation. – Gert Arnold Sep 09 '16 at 20:28
  • Okay, this might be the answer but it was not seeking – Frank Núñez Rodríguez Sep 09 '16 at 21:10

0 Answers0