Good Day!
I use EF 5 Beta 2 CodeFirst.
For my entities first I created ConsoleApplication and Repository working well, but when I create dll and use this Repository I have ObjectDisposedException, when tried to use Repository.
Sample (in var user = (new Repository().GetElementById<ReposirotyEF.User>(resultTesting.Users.Id) as ReposirotyEF.User);):
public static ResultModel PopulateResultModel(ResultTesting resultTesting)
{
string name = string.Empty;
var user = (new Repository().GetElementById<ReposirotyEF.User>(resultTesting.Users.Id) as ReposirotyEF.User);
name = user.Name;
return new ResultModel()
{
Id = resultTesting.Id,
Name = name,
Result = resultTesting.Result,
Answers = resultTesting.Answers,
Data = resultTesting.TimeEnd,
RightAnswers = resultTesting.RightAnswers,
Time = resultTesting.Time
};
}
Or:
public void SaveOrUpdate<T>(T obj)
{
using (var context = new ContextTest1())
{
switch (typeof(T).Name)
{
case "User":
User user = context.Users.ToList().Find(u => u.Id == (obj as User).Id);
User newUser = obj as User;
if (user != null)
{
user = newUser;
}
else
{
context.Users.Add(newUser);
}
break;
case "ResultTesting":
var resultTesting = context.ResultTestings.ToList().Find(u => u.Id == (obj as ResultTesting).Id);
var newRes = obj as ResultTesting;
if (resultTesting != null)
{
resultTesting = newRes;
}
else
{
context.ResultTestings.Add(newRes);
}
break;
case "Question":
var question = context.Questions.ToList().Find(u => u.Id == (obj as Question).Id);
var newQue = obj as Question;
if (question != null)
{
question = newQue;
}
else
{
context.Questions.Add(newQue);
}
break;
default:
//context.CurrentTestings.ToList().Remove(obj as CurrentTesting);
var currentTesting = context.CurrentTestings.ToList().Find(u => u.Id == (obj as CurrentTesting).Id);
var newCur = obj as CurrentTesting;
if (currentTesting != null)
{
currentTesting = newCur;
}
else
{
context.CurrentTestings.Add(newCur);
}
break;
}
context.SaveChanges();
}
}
in context.CurrentTestings.Add(newCur);
And my GetElementById (not good):
public IId GetElementById<T>(int id)
where T : IId
{
using (var context = new ContextTest1())
{
switch (typeof(T).Name)
{
case "User":
return context.Users.ToList().Find(u => u.Id == id);
case "ResultTesting":
return context.ResultTestings.ToList().Find(u => u.Id == id);
case "Question":
return context.Questions.ToList().Find(u => u.Id == id);
}
return context.CurrentTestings.ToList().Find(u => u.Id == id);
}
}
Thx for you help.
Find()onToList()instead of on the ObjectSets directly? You are causing your entire tables to be returned and using Linq to Objects to query. For your actual exception, though; could you please show the exact code where it's happening, and what the exception is exactly? Particularly - what object is supposed to have been disposed? Your assumption about that might be wrong.