Records in parent table are saving successfully but child table records are not saving and no error is coming, using Entity Framework with repository pattern available here .
Below is the sample schema design of my tables, might be a special case.
Considering above tables if I try to insert records in Table2 and Table3 together no record inserted into Table3.
Please let me know if need further clarification on the question.
Thanks in advance!!
EDIT 1
Below is the code I am using to insert parent and child records together.
var parent = new Table2()
{
Table1Id = 1,
SecondColumn = 1
};
parent.Tables3.Add(new Table3()
{
Table1Id = 1,
SecondColumn = 1,
AnotherColumn = 1
});
context.Set<Table2>().Attach(parent);
context.Entry<Table2>(parent).State = EntityState.Added;
context.SaveChanges();
EDIT 2
public partial class Table2
{
public Table2()
{
this.Tables3 = new HashSet<Table3>();
}
public int Table1Id { get; set; }
public int SecondColumn { get; set; }
public virtual ICollection<Table3> Tables3 { get; set; }
}
public partial class Table3
{
public int Table1Id { get; set; }
public int SecondColumn { get; set; }
public int AnotherColumn { get; set; }
}
You should do one of two:
Use add instead of attach
var parent = new Table2() { Table1Id = 1, SecondColumn = 1 }; parent.Tables3.Add(new Table3() { Table1Id = 1, SecondColumn = 1, AnotherColumn = 1 }); context.Set<Table2>().Add(parent); context.SaveChanges();
In this case all navigation properties will also be added.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.