Reputation: 747
I create a new project in MVC4. I get this error:
System.ArgumentNullException: Value can not be null.
Parameter name: source.
The database exists, the table exists and is filled with data.
Any tips?
Controler ShopController.cs
...
SQLEntities DB = new SQLEntities();
public ActionResult Index()
{
var x = DB.Types.ToList(); <-Here I get an exception
return View(x);
}
...
Model Type.cs
using System.Collections.Generic;
namespace xxx.Models
{
public class Type
{
public int TypeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
Model SQLEntities.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace xxx.Models
{
public class SQLEntities
{
public DbSet<Type> Types { get; set; }
}
}
Web.config
<connectionStrings>
<add name="SQLEntities" connectionString="Data Source=KOMPUTER\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
The compiler doesn't indicate any error. It was only when he wants to go to mypage/Shop
getting an exception.
stack trace
{System.ArgumentNullException: Value can not be null.
Parameter name: source.
w System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
w Print.Controllers.StoreController.Index() w c:\Users\John\Documents\Visual Studio 2013\Projects\Print\Print\Controllers\StoreController.cs:wiersz 18
w lambda_method(Closure , ControllerBase , Object[] )
w System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
w System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
w System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
w System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
w System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
w System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
w System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
w System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
w System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()}
Upvotes: 0
Views: 2019
Reputation: 59416
Your problem is that SQLEntities
should inherit from DbContext
.
Your problem is that SQLEntities.Types
is clearly null. You shouldn't be required to initialize this property as the EntityFramework will do it automatically when everything is setup correctly.
Change your code to:
public class SQLEntities: DbContext
{
public DbSet<Type> Types { get; set; }
}
Upvotes: 3