在客户端上,我将所有相关实体(导航属性)添加到我的主要实体并将其附加到列表并致电 subsitchange 。但是在服务器端,所有相关实体都丢失了!

代码:

客户:

DomainService1 domainService1= new DomainService1();
.
.
.
WorkCode newWorkCode = new WorkCode();
newWorkCode.Date = DateTime.Now;

.
.
.

for(Work item in WorkList)
{
 newWorkCode.Works.Add(item) 
}

.
.
.

domainService1.WorkCodes.Attach(newWorkCode);
domainService1.InsertWorkCode(newWorkCode);     
      dsMaintenance.SubmitChanges(submitOperation =>
      {
        if (!submitOperation.HasError)
        {

        }
      },
            null);
.

服务器:

[Update(UsingCustomMethod = true)]
public void InsertWorkCode(WorkCode workCode)
{
    //////// workCode.Works = 0 ///////////////////

  this.ObjectContext.WorkCodes.AddObject(workCode);            
}
.

有帮助吗?

解决方案

I'm not sure what you are doing here. But if I want to add stuff I do it like this and it works:

Context = new DomainContext();

var customer = new Customer() { /* ... */ };
var order = new Order() { Customer = customer, /* ... */ };

Context.Customers.Add(customer);
Context.Orders.Add(order);

If you like the other approach though, you can do that too like this:

var customer = new Customer() { /* ... */ };
var order = new Order { /* ... */ };

customer.Orders.Add(order);
Context.Customers.Add(customer);

Now you just submit:

var submitOperation = Context.SubmitChanges();
submitOperation.Completed += // [...]

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top