本文介绍了更改跟踪POCO&导航道具 - 无法添加记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

您好

 

我正在使用EF4 POCO与WCF。我有一个奇怪的情况 - 尝试解决这个问题5小时:-S

I'm using EF4 POCO with WCF. I have a strange situation - trying to solve this for 5 hours :-S

 

我有以下情况http:// img228。 imageshack.us/img228/155/unbenanntze.png

我从客户端获取启动时的所有用户和所有类别。

I have following situation http://img228.imageshack.us/img228/155/unbenanntze.png
I fetch all Users and all Categories at the startup from the client.

 

添加记录如下:

~~~~~客户~~~ ~~

~~~~~ Client ~~~~~

EbayEntities.Item item = new EbayEntities.Item();

item.Id = Guid.NewGuid();

item.Category = categories_ [0];

EbayEntities.Item item = new EbayEntities.Item();
item.Id = Guid.NewGuid();
item.Category = categories_[0];

...

...

EbayEntities.Auction auction = new EbayEntities.Auction();

auction.Id = Guid.NewGuid();

auction.Item = item;

auction.User = users_ [0];

EbayEntities.Auction auction = new EbayEntities.Auction();
auction.Id = Guid.NewGuid();
auction.Item = item;
auction.User = users_[0];

~~~~~服务r ~~~~~

public void CreateAuction(EbayEntities.Auction item)

{

   using( var context = new EbayContainer())

   {

Console.WriteLine(item.Id);

context.AuctionSet .ApplyChanges(项目);                

context.SaveChanges();

   }¥b $ b }

 

如果我点击"插入"按钮 - 工作。调试输出是"37685829-734b-403f-9e7c-ea7a36830f4f"。

if i click on "Insert" - works. Debug Output is "37685829-734b-403f-9e7c-ea7a36830f4f".

再次单击:调试输出为:"34fc03a7-3feb-4952-8021-d3e6e8008b32"

Clicking once more: Debug output is: "34fc03a7-3feb-4952-8021-d3e6e8008b32"

所以 - 不同的钥匙。但EF声称:"System.Data.UpdateException:必须是唯一索引  [表名= AuctionSet,约束名称= PK_AuctionSet]"}(翻译 - 例外是德语)。

So - different keys. But EF claims: "System.Data.UpdateException: Must be an unique index  [ Table name = AuctionSet,Constraint name = PK_AuctionSet ]"} (Translated - Exception is in german).

现在有趣的是:

如果我将客户更改为

~~~~~客户~~~~~

// NEW CODE

categories_  ; = server.GetAllCategories();

users_ = server.GetAllUsers();

~~~~~ Client ~~~~~
//NEW CODE
categories_  = server.GetAllCategories();
users_ = server.GetAllUsers();

//代码未更改

EbayEntities.Item item = new EbayEntities.Item() ; 

item.Id = Guid.NewGuid();

item.Category = categories_ [0];

EbayEntities.Item item = new EbayEntities.Item(); 
item.Id = Guid.NewGuid();
item.Category = categories_[0];

...

...

EbayEntities.Auction auction = new EbayEntities.Auction();

auction.Id = Guid .NewGuid();

auction.Item = item;

auction.User = users_ [0];

EbayEntities.Auction auction = new EbayEntities.Auction();
auction.Id = Guid.NewGuid();
auction.Item = item;
auction.User = users_[0];

因此,重新获取导航值有助于:-S但为什么?我从来没有改变它们: - /

So refetching the navigation values helps :-S But why? I have never changed them :-/

谢谢

Michael

 

推荐答案

 

public void CreateAuction(EbayEntities.Auction item)
{
 using (var context = new EbayContainer())
 {
  item.Item.Category = context.CategorySet.Where(c => c.Name == item.Item.Category.Name).First();
  item.User = context.UserSet.Where(u => u.Id == item.User.Id).First();

  context.AuctionSet.ApplyChanges(item);

  context.SaveChanges();
 }
}


所以如果我这样做 - 我不必在服务器上重新获取。但是 - 我认为问题的原因是导航道具类别和用户:-(

 

So if i do this - i dont have to refetch on the server. However - i think the cause of the problem are the nav props Category and User :-(


这篇关于更改跟踪POCO&导航道具 - 无法添加记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:52