所以,我有一个MS Access数据库与父表:为什么我的孩子记录与保存到Access的数据集中的父记录不相关?
ID Autonumber
Description Text
子表:
ID AutoNumber
Description Text
ParentID Number
我已经创建了关系:
Parent column: ID
Child column: ParentID
Enforce Ref Integrity, Cascade Update and Cascade Delete
我已经投进视觉此Studio 2010,该向导已创建数据集和适配器。 我修改了xsd以将关系设置为Both Relation and Foreign Key Constraint
和Update/Delete
规则为Cascade
。
所以我尝试添加父记录与相关的子记录:
var taParent = new TestDataSetTableAdapters.ParentTableAdapter();
var taChild = new TestDataSetTableAdapters.ChildTableAdapter();
var ds = new TestDataSet();
taParent.Fill(ds.Parent);
taChild.Fill(ds.Child);
var rowParent = ds.Parent.NewParentRow();
rowParent.Description = DateTime.Now.ToString();
ds.Parent.AddParentRow(rowParent);
var rowChild = ds.Child.NewChildRow();
rowChild.ChildText = DateTime.Now.ToString();
ds.Child.AddChildRow(rowChild);
taParent.Update(ds);
var parentId = rowParent.ID; // <-- This is still -1
// taParent.Fill(ds.Parent); // <-- Doing this hoping to reload the parent record gives: Cannot clear table Parent because ForeignKeyConstraint ParentChild enforces constraints and there are child rows in Child.
taChild.Update(ds.Child);
但孩子PARENTID记录为空。
如果我尝试:
rowChild.SetParentRow(rowParent);
我会得到
You cannot add or change a record because a related record is required in table 'Parent'.
我缺少什么?
谢谢,问题更新与我的尝试。父ID永远不会传播回数据集。重新加载失败。 –