我有以下情況EF:更新引用的對象
public static void UpdateUserinfo(int id, Model model)
{
using (Entities context = new Entities())
{
userinfo userinfo = (from u in context.userinfoes where u.Id == id select u).FirstOrDefault();
userinfo.BirthDate = model.Birthdate;
var langauges = (from l in context.languages where model.LanguageIDs.Contains((int)l.LanguageID) select l);
foreach (var l in langauges)
{
userinfo.languages.Add(l);
}
context.SaveChanges();
}
}
當我嘗試那樣做,我得到一個錯誤:
{"There is already an open DataReader associated with this Connection which must be closed first."}
所以基本上我有一個表的語言和我有表userinfo和一個關聯表userlanguages,所以基本上在EF你可以導航userinfo.langauges ...
我的問題我如何正確更新它,我如何添加新的語言到userinfo呢?
或者我應該mabye有類似:
userinfo.languages = languages
但我不得不放棄ICollection的,但怎麼辦呢?
作爲一種解決方案,它確實是一個糟糕的黑客攻擊。使用兩個上下文會導致不一致。 – kkocabiyik
@kkocabiyikv啊夠公平的,我不知道你提供的連接字符串的答案,我會在這裏留下這個,但+1 – JMK
我提供了你的答案後:) – kkocabiyik