2

I am using silverlight client with ado.net dataservices on entity framework.

I have an Entity Contact and an Entity Address which is related with a foreign key relation ship... A contact can have 1 or more Adresses but a Address needs always at least 1 contact.. pretty basic...

I have a Repository for my contacts and Address which has a Method Add(T entity),...

On my Client I have a form with allows users to add a contact with an address

and I want to save both away to the database...

Address a = new Address();
a.Street="Street",
a.City = "City"
a.Contact = 
 new Contact(){ Name="Name",Age="60"}

_repository.Add(a);

....

Ok i figured out that I can not save a related object graph right away so i did something like this

DataBaseEntities.AddToContact(obj2Badded.Contact);
DataBaseEntities.AddToAddress(obj2Badded);
DataBaseEntities.SetLink(obj2Badded,"Contact",object2Badded.Contact);
DataBaseEinties.BeginSaveChanges(...)

Is there away to do this in transations like when contact is not added address will also not be added and vice versa...

... and all what I have tried is not working...

It would be great if someone can point me in the right direction on this topic I simply want to add a related object graph =} ...

4

2 回答 2

0

您的第一个示例应该可以正常工作,相关实体和所有。如果您要为这两个实体插入新对象,则无需单独将相关实体添加到上下文中。因此,除非您在运行此代码时告诉我们问题出在哪里,否则将很难提供帮助。

于 2009-03-17T14:31:06.867 回答
0

我有一个类似的问题,在添加了类似的东西后它对我有用

DatabaseEntities.UpdateObject(obj2Badded);

就在之前

DatabaseEntities.BeginSavingChanged();

如果您在项目中包含 edmx 并使用 ObjectContext,则不需要这样做,但如果您创建服务引用并使用生成的 DataServiceContext,则需要这样做。

于 2012-03-20T14:06:38.760 回答