2013-03-29 46 views
0

我有問題將關係n與n關聯的對象。 (使用實體框架)關係實體框架中的n:n(列表錯誤)

Table:Item_Estoque 
..attributes.. 



Table2 : Item_Estoque_Producao 
Quantidade 
Item_Estoque_Produto(Item_Estoque) 
Item_Estoque_Mercadoria(Item_Estoque) 

的代碼:

List<Item_Estoque> listaEstoque = item.Item_Estoque.Where(ie => ie.Quantidade > 0); 

List<Item_Estoque_Producao> lstEstoqueProducao = new List<Item_Estoque_Producao>(); 

foreach (Item_Estoque itemEstoque in listaEstoque) 
{ 
    Item_Estoque_Producao estoqueProducao = new Item_Estoque_Producao(); 
    estoqueProducao.Item_Estoque_MateriaPrima = itemEstoque; 
    estoqueProducao.Quantidade = quantidade; 
    lstEstoqueProducao.Add(estoqueProducao); 
} 

Item_Estoque itemEstoqueProduto = new Item_Estoque(); 
... 

foreach (Item_Estoque_Producao estoqueProducao in lstEstoqueProducao) 
{ 
    itemEstoqueProduto.Item_Estoque_Producao_Produto.Add(estoqueProducao); 
    !!! ERROR!!! "Unable to define the relationship between the two objects because they are attached to different ObjectContext objects." 
} 

我嘗試使用2個不同的Item_Estoque創建Item_Estoque_Produção,但是當我設定第二Item_Estoque,出現問題。

我該怎麼辦?

回答

0

它已解決!

我把「使用(ModelContainer bc = new ModelContainer)」放在foreach的中間。我把這個代碼的開始!

已解決!