Entity Framework DbSet.Find throws an exception

I follow a very basic example of the first Entity Framework (version 4.1) in ASP.NET MVC 4 (using the EBuy example in O'Reilly Programming ASP.NET MVC 4). I have been looking for solutions for this and still made empty. The main problem is that this code in my controller:

public ActionResult Details(long id) { using (var db = new EbuyDataContext()) { var rep = db.Auctions; var auction = rep.Find(id); return View(auction); } } 

throws The value of ArgumentNullException cannot be null. Parameter Name: key. when it hits the rep.Find (id) call. Interestingly, when it breaks into code, the db.Auctions property has an β€œExpand Results View”, it will list IEnumerable, and if you click it, it will display my expected two objects that have just been extracted from the database. However, the rep object has the value "Children cannot be rated" when it is inspected.

Quite the same Create code, like this one, works fine:

 [HttpPost] public ActionResult Create(Auction auction) { var db = new EbuyDataContext(); db.Auctions.Add(auction); db.SaveChanges(); return View(auction); } 

This is my db context:

 namespace Ebuy.DataAccess { public class EbuyDataContext : DbContext { public DbSet<Auction> Auctions { get; set; } } } 

and this is my model class:

 namespace Ebuy.DomainModel { public class Auction { public long Id { get; set; } public string Title { get; set; } public string Description { get; set; } public decimal StartPrice { get; set; } public decimal CurrentPrice { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } } } 

I found one link that talked about POCO and dbcontext located in different namespaces or assemblies. Does anyone know why Create code works, but Find doesn't?

Edit: Stack:

System.ArgumentNullException is not handled by user code
Message = Value cannot be null. Parameter Name: key Source = mscorlib
ParamName = key StackTrace: in System.Collections.Generic.Dictionary 2.FindEntry(TKey key) at System.Collections.Generic.Dictionary 2.ContainsKey (TKey key) in System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForTypeyTypeTypeType object) in System.Data.Entity.Internal.InternalContext.IsEntityTypeMapped (Type the type of object) in System.Data.Entity.Internal.Linq.InternalSet 1.Find(Object[] keyValues) at System.Data.Entity.DbSet 1. Find (Object [] keyValues) on Ebuy.Website.Controllers.AuctionsController.Details (Int64 id) in C: \ Users \ John \ Documents \ Visual Studio 2010 \ Projects \ Ebuy.Website \ Ebuy.Website \ Controllers \ AuctionsController.cs : line 26 on lambda_method (Closure, ControllerBase, Object []) in System.Web.Mvc.ActionMethodDispatcher.Execute (ControllerBase, parameters Object []) in System.Web.Mvc.ReflectedActionDescriptor.Execute (ControllerCon text controllerContext, IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 parameters) in System.Web.Mvc.Async.AsyncControllerActionInvoker. <> c_DisplayClass42.b_41 () in System.Web.Mvc.Async.AsyncResultWrapper. <> c_DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 1.End () in System.Web.Mvc.Async.AsyncebecAndResultecoke .Mvc.Async.AsyncControllerActionInvoker. <> c_DisplayClass37. <> c_DisplayClass39.b_33 () in System.Web.Mvc.Async.AsyncControllerActionInvoker. <> c_DisplayClass4f.b_49 () InnerException:

Edit 2: The table has an Identifier column as a primary key. Update: Based on a nemesv comment, I checked the versions. I am using the .NET Framework version 4.0. The MVC web application, when created, uses Nuget to obtain EF version 5.0 (although the actual link is to EF runtime 4.0.30319 v4.4.0.0). I added a DataAccess project and gave a link to another EF (runtime 4.0.30319 v4.1.0.0). I changed the link to the DataAccess project to the same EF as in the web application, and now I get another exception: InvalidOperationException The context cannot be used during model creation. This seems like a problem when using EF in an N-tier application, so I will follow this flow and report back.

Correction: I read some information about the LazyLoading settings in EF, as well as how loading occurs when accessing an object property. Since I really do not access the property of objects before I try to find it, and I think that perhaps LazyLoading is used by default in EF 5.0, I added a default constructor to my EbuyDataContext class as follows:

 public class EbuyDataContext : DbContext { public EbuyDataContext() { //Configuration.LazyLoadingEnabled = false; } public DbSet<Auction> Auctions { get; set; } } 

I also set the LazyLoadingEnabled parameter to false, and then launched it and Voila! Success! However, I thought I would double check this, so I commented on the LazyLoadingEnabled parameter, did a clean rebuild, and still worked. I commented on the default constructor and now it still works - something changed when I ran it with the LazyLoadingEnabled parameter in place?

+4
source share
1 answer

In EF (at least 4 and 5), lazy loading is the default configuration. Also, try explicitly specifying your identifier as a key using the KeyAttribute decoration.

 public class Auction { [Key] public long Id { get; set; } public string Title { get; set; } public string Description { get; set; } public decimal StartPrice { get; set; } public decimal CurrentPrice { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } } 
0
source

Source: https://habr.com/ru/post/1443106/


All Articles