如何从父类修改数据创建它作为子类?

时间:2016-09-09 19:18:21

标签: c# entity-framework-6

我有4个模型类: 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; }
    }

来自父类的数据库数据中存在,但是当我尝试从子类插入数据时,给出了重复键错误,例如我已经这样做了:

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();

无法删除父类中的数据,因为还有另一种类型的数据引用了您的数据。我需要知道,当有来自父类的数据时,是否有人可以设法从子类插入数据,或者更改来自子数据父的转换数据,而无需修改或更改ID或主键。

实体框架是版本6 ..

0 个答案:

没有答案