我正在尝试 Entity Framework 4.0,这是该案例的最简化版本 -
我有以下两个相关的表 -
地址
ID
城市
客户
ID
地址 ID
名称
我已经在 ComboBox 中加载了地址。我想要的只是在文本框中输入客户端名称,从组合框中选择一个地址作为客户端的地址,然后点击保存按钮。我希望将我的客户保存在客户表中。这是我尝试过的 -
/*loads Addresses to the ComboBox*/
private void LoadData()
{
using (CAEntities ctx = ModelAccess.GetContext())
this.AddressList = ctx.Addresses.ToList();
}
/*(tries) to insert the Client to the database*/
private void Save()
{
/*here this.Client is the Client object that is instantiated & initialized
by previous code and this.Address is the SelectedItem of the ComboBox*/
this.Client.Address = this.Address;
using (CAEntities ctx = ModelAccess.GetContext())
{
ctx.Clients.AddObject(this.Client);
ctx.SaveChanges();
}
}
编程实体框架中的 Julie Lerman说,...Because the
entities are joined, you can add either entity, and it will bring along the rest of the graph...
但是我所拥有的是一个 InvalidOperationException,它说“EntityKey 属性只能在属性的当前值为 null 时设置。”
如果我使用 -
this.Client.AddressId = this.Address.Id;
代替 -
this.Client.Address = this.Address;
客户端完美插入数据库。但我认为我也应该能够将实体直接相互关联,对吗?
我认为问题与我正在创建的单独上下文有关。所以我尝试了这个-
private void Save()
{
this.Client.Address = this.Address;
using (CAEntities ctx = ModelAccess.GetContext())
{
ctx.Addresses.Attach(this.Address);
ctx.SaveChanges();
}
}
但这次我收到了一个 InvalidOperationException,上面写着“无法将具有临时 EntityKey 值的对象附加到对象上下文。” 那么我在这里做错了什么?提前致谢。