问题
Completely revised:
Okay, I'm using Ninject with the MVC 2 extension as my DI container, and AutoMapper as my entity <--> view model mapper. I'm getting an 'ObjectContext is disposed' error in my view model -> entity mapping. My code is below.
Ninject bindings:
public class DIModule : NinjectModule
{
public override void Load()
{
this.Bind<HGEntities>().ToSelf().InRequestScope();
this.Bind<IArticleRepository>().To<HGArticleRepository>().InRequestScope();
this.Bind<IGameRepository>().To<HGGameRepository>().InRequestScope();
this.Bind<INewsRepository>().To<HGNewsRepository>().InRequestScope();
this.Bind<ErrorController>().ToSelf().InRequestScope();
this.Bind<HGController>().ToSelf().InRequestScope();
}
}
My repository:
public class HGGameRepository : IGameRepository, IDisposable
{
private HGEntities _context;
public HGGameRepository(HGEntities context)
{
this._context = context;
}
// methods
public void SaveGame(Game game)
{
if (game.GameID > 0)
{
_context.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Modified);
}
else
{
_context.Games.AddObject(game);
}
_context.SaveChanges();
}
public void Dispose()
{
if (this._context != null)
{
this._context.Dispose();
}
}
}
My controller's constructor, which includes my AutoMapper map definitions:
public AdminController(IArticleRepository articleRepository, IGameRepository gameRepository, INewsRepository newsRepository)
{
_articleRepository = articleRepository;
_gameRepository = gameRepository;
_newsRepository = newsRepository;
Mapper.CreateMap<Game, AdminGameViewModel>()
.BeforeMap((s, d) =>
{
int platCount = s.Platforms.Count;
var plats = s.Platforms.ToArray();
d.PlatformIDs = new int[platCount];
for (int i = 0; i < platCount; ++i)
{
d.PlatformIDs[i] = plats[i].PlatformID;
}
})
.ForMember(dest => dest.Pros, opt => opt.MapFrom(src => src.Pros.Split(new char[] {'|'})))
.ForMember(dest => dest.Cons, opt => opt.MapFrom(src => src.Cons.Split(new char[] {'|'})))
.ForMember(dest => dest.PlatformIDs, opt => opt.Ignore());
Mapper.CreateMap<AdminGameViewModel, Game>()
.BeforeMap((s, d) =>
{
if (d.Platforms != null && d.Platforms.Count > 0)
{
var oldPlats = d.Platforms.ToArray();
foreach (var oldPlat in oldPlats)
{
d.Platforms.Remove(oldPlat);
}
}
foreach (var platId in s.PlatformIDs)
{
var plat = _gameRepository.GetPlatform(platId);
d.Platforms.Add(plat);
}
})
.ForMember(dest => dest.Platforms, opt => opt.Ignore())
.ForMember(dest => dest.BoxArtPath, opt => opt.Ignore())
.ForMember(dest => dest.IndexImagePath, opt => opt.Ignore())
.ForMember(dest => dest.Cons, opt => opt.MapFrom(src => string.Join("|", src.Cons)))
.ForMember(dest => dest.Pros, opt => opt.MapFrom(src => string.Join("|", src.Pros)))
.ForMember(dest => dest.LastModified, opt => opt.UseValue(DateTime.Now));
}
It's the second mapping that's important here.
Next is my Edit method:
[HttpPost]
public ActionResult EditGame([Bind(Prefix="GameData")]AdminGameViewModel formData)
{
Game game = _gameRepository.GetGame(formData.GameID);
if (ModelState.IsValid)
{
game = AutoMapper.Mapper.Map<AdminGameViewModel, Game>(formData, game);
// it dies here, so the rest of the method is immaterial
}
Finally, the stack trace:
[ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.]
System.Data.Objects.ObjectContext.EnsureConnection() +87
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +90
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +96
System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +182
System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1(IEnumerable`1 sequence) +74
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +95
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +163
System.Linq.Queryable.FirstOrDefault(IQueryable`1 source, Expression`1 predicate) +300
HandiGamer.Domain.Concrete.HGGameRepository.GetPlatform(Int32 id) in C:\Users\Kevin\documents\visual studio 2010\Projects\HandiGamer\HandiGamer.Domain\Concrete\HGGameRepository.cs:68
HandiGamer.WebUI.Controllers.AdminController.<.ctor>b__a(AdminGameViewModel s, Game d) in C:\Users\Kevin\documents\visual studio 2010\Projects\HandiGamer\HandiGamer\Controllers\AdminController.cs:56
AutoMapper.<>c__DisplayClass1b.<BeforeMap>b__1a(Object src, Object dest) +139
AutoMapper.TypeMap.<get_BeforeMap>b__0(Object src, Object dest) +118
AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) +196
AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +256
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +459
[AutoMapperMappingException:
Mapping types:
AdminGameViewModel -> Game
HandiGamer.WebUI.ViewModels.AdminGameViewModel -> HandiGamer.Domain.Entities.Game
Destination path:
Game
Source value:
HandiGamer.WebUI.ViewModels.AdminGameViewModel]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +537
AutoMapper.MappingEngine.Map(Object source, Object destination, Type sourceType, Type destinationType, Action`1 opts) +179
AutoMapper.MappingEngine.Map(TSource source, TDestination destination, Action`1 opts) +190
AutoMapper.MappingEngine.Map(TSource source, TDestination destination) +146
AutoMapper.Mapper.Map(TSource source, TDestination destination) +105
HandiGamer.WebUI.Controllers.AdminController.EditGame(AdminGameViewModel formData) in C:\Users\Kevin\documents\visual studio 2010\Projects\HandiGamer\HandiGamer\Controllers\AdminController.cs:323
lambda_method(Closure , ControllerBase , Object[] ) +162
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +51
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +409
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +52
System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +127
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +436
System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +61
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +305
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830
System.Web.Mvc.Controller.ExecuteCore() +136
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +141
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +52
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +690
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +194
Following the process in the debugger, my ObjectContext's (HGEntities) connection remains intact until my view model -> game map is invoked. For some reason, the connection is disposed at that point. Any ideas as to why that's happening?
回答1:
Based on the documentation they actually discourage you from using factories. I use them but it is possible it might now solve your problem. I was getting this error at a point in my own DI with ninject journey, but it is hard to say why you are getting yours. For me it was being caused by relationships and lazy loading. With lazy loading enabled your objects hold on to a reference to the ObjectContext, so they can populate related objects and collections of related objects.
Bottom line, I think it would help if you posted the stack trace for your error so we can help you know exactly when it is occurring.
Hope this helps.
Edit: I think the issue is in your save method. If you dispose of your object context when you get the game to edit it. I don't think it will be in the ObjectStateManager see your line:
_context.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Modified);
Your current game object that you got from the AutoMapper is not attached to any ObjectContext. Here is the method I use for updates:
public void Update(TEntity entity)
{
object originalItem;
EntityKey key = Context.CreateEntityKey(Context.GetEntitySet<TEntity>().Name, entity);
if (Context.TryGetObjectByKey(key, out originalItem))
{
Context.ApplyCurrentValues(key.EntitySetName, entity);
}
}
Don't forget to call Context.SaveChanges();
I didn't come up with it myself found it somewhere on the web. It's works well for me. I would adjust your save method to incorporate it. If you need assistance let me know. The other thing I would recommend (small) is to set your Object context to null in your dispose method.
good luck!
Edit: Okay, another try... based on the constructor for your admin controller do you have private variables for your repositores?
private IGameRepository _gameRepository;
public AdminController(IArticleRepository articleRepository, IGameRepository gameRepository, INewsRepository newsRepository)
{
_articleRepository = articleRepository;
_gameRepository = gameRepository;
_newsRepository = newsRepository;
Are Controllers being created by ninject? I don't see AdminController in your list of bindings. I am not seeing the [Inject]
attribute on any of your constructors. Did you just omit it when you posted to stack or are they not there?
My other thought is if you are not having any memory issues (leaks) with the application InSingletonScope() for your context might be the appropriate answer.
Sorry for all the questions but I am just trying to understand the issue better.
回答2:
I see that IDisposable isn't implemented correctly. Maybee that could be the reason for your disposing related exception?
回答3:
My own static mapper works perfectly:
public static class GameMapper
{
public static Game MapFromEditModelToGame(IGameRepository repo, AdminGameViewModel formData, Game newGame)
{
newGame.GameID = formData.GameID;
newGame.GameTitle = formData.GameTitle;
newGame.GenreID = formData.GenreID;
newGame.LastModified = DateTime.Now;
newGame.ReviewScore = (short)formData.ReviewScore;
newGame.ReviewText = formData.ReviewText;
newGame.Cons = String.Join("|", formData.Cons);
newGame.Pros = String.Join("|", formData.Pros);
newGame.Slug = formData.Slug;
if (newGame.Platforms != null && newGame.Platforms.Count > 0)
{
var oldPlats = newGame.Platforms.ToArray();
foreach (var oldPlat in oldPlats)
{
newGame.Platforms.Remove(oldPlat);
}
}
foreach (var platId in formData.PlatformIDs)
{
var plat = repo.GetPlatform(platId);
newGame.Platforms.Add(plat);
}
return newGame;
}
}
Invoked as:
game = GameMapper.MapFromEditModelToGame(_gameRepository, formData, game);
Compare the above with my AutoMapper map definition:
Mapper.CreateMap<AdminGameViewModel, Game>()
.BeforeMap((s, d) =>
{
if (d.Platforms != null && d.Platforms.Count > 0)
{
var oldPlats = d.Platforms.ToArray();
foreach (var oldPlat in oldPlats)
{
d.Platforms.Remove(oldPlat);
}
}
foreach (var platId in s.PlatformIDs)
{
var plat = _gameRepository.GetPlatform(platId);
d.Platforms.Add(plat);
}
})
.ForMember(dest => dest.Platforms, opt => opt.Ignore())
.ForMember(dest => dest.BoxArtPath, opt => opt.Ignore())
.ForMember(dest => dest.IndexImagePath, opt => opt.Ignore())
.ForMember(dest => dest.Cons, opt => opt.MapFrom(src => string.Join("|", src.Cons)))
.ForMember(dest => dest.Pros, opt => opt.MapFrom(src => string.Join("|", src.Pros)))
.ForMember(dest => dest.LastModified, opt => opt.UseValue(DateTime.Now));
The only real difference is me passing in the repo as an argument.
All other code - my controller, my DI stuff, etc. - is exactly the same. The only difference is me using my own cludgy mapper. Not an ideal situation, but something.
来源:https://stackoverflow.com/questions/10526167/ninject-mvc-2-ef-4-automapper-objectcontext-has-been-disposed