I working with a local database on an ASP.NET MVC project using Visual Studio.
I'm having trouble modifying the database. When I call SaveChanges()
I always get this error:
The value cannot be null Parameter Name: Source
Here's an example of how I modify the database, and the error log / StackTrace.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateMedico(Medico medicoToCreate)
{
List<Medico> dedo = data.Medico.ToList();
if (ModelState.IsValid)
{
using (MedsEntities data = new MedsEntities())
{
medicoToCreate.Id = dedo.Count() + 1;
data.Medico.Add(medicoToCreate);
System.Diagnostics.Debug.WriteLine("RECURSO :" + medicoToCreate.RECURSO_MEDICO);
System.Diagnostics.Debug.WriteLine("NOMBRE: " + medicoToCreate.NOMBRE);
System.Diagnostics.Debug.WriteLine("TIPO: " + medicoToCreate.TIPO);
// and so on with all properties.....
System.Diagnostics.Debug.WriteLine("MEDICO A GRABAR:" + medicoToCreate.NOMBRE);
if (medicoToCreate == null)
{
System.Diagnostics.Debug.WriteLine("MEDICOTOCREATE, IS NULL");
}
else {
System.Diagnostics.Debug.WriteLine("MEDICOTOCREATE, IS NOT NULL");
}
data.SaveChanges();
return RedirectToAction("MedicosList");
}
}
return RedirectToAction("MedicosList");
}
Stack trace:
en System.Data.Entity.Internal.InternalContext.SaveChanges()
en System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
en System.Data.Entity.DbContext.SaveChanges()
en FINAL.Controllers.HomeController.CreateMedico(Medico medicoToCreate) en d:\IDEX8544 KARLO\Documents\Visual Studio 2012\Projects\FINAL\FINAL\Controllers\HomeController.cs:línea 70
en lambda_method(Closure , ControllerBase , Object[] )
en System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
en System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
en System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
en System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
en System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
en System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
en System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
en System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49(
Connection Strings (Thanks Oleg):
<connectionStrings>
<add name="MedsEntities" connectionString="metadata=res://*/Models.ModelMeds.csdl|res://*/Models.ModelMeds.ssdl|res://*/Models.ModelMeds.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="data source=|DataDirectory|\DB.sdf"" providerName="System.Data.EntityClient" />
<add name="HospitalEntities" connectionString="metadata=res://*/Models.ModelHosp.csdl|res://*/Models.ModelHosp.ssdl|res://*/Models.ModelHosp.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="data source=|DataDirectory|\DB.sdf"" providerName="System.Data.EntityClient" />
<add name="LabsEntities" connectionString="metadata=res://*/Models.ModelLabs.csdl|res://*/Models.ModelLabs.ssdl|res://*/Models.ModelLabs.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="data source=|DataDirectory|\DB.sdf"" providerName="System.Data.EntityClient" />
<add name="FarmaciasEntities" connectionString="metadata=res://*/Models.ModelFarms.csdl|res://*/Models.ModelFarms.ssdl|res://*/Models.ModelFarms.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="data source=|DataDirectory|\DB.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
MEDICO MODEL:
namespace FINAL.Models
{
using System;
using System.Collections.Generic;
public partial class Medico
{
public string RECURSO_MEDICO { get; set; }
public string NOMBRE { get; set; }
public string TIPO { get; set; }
public string TIPO2 { get; set; }
public string TIPO3 { get; set; }
public string CELULAR { get; set; }
public string EMAIL { get; set; }
public string TEL1 { get; set; }
public string TEL2 { get; set; }
public string EXT { get; set; }
public string HOSPITAL { get; set; }
public string DIRECCION { get; set; }
public string LAT { get; set; }
public string LON { get; set; }
public string C33DIGITOS { get; set; }
[Key]
public int Id { get; set; }
}
}
DB Model:
namespace FINAL.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MedsEntities : DbContext
{
public MedsEntities()
: base("name=MedsEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Medico> Medico { get; set; }
}
}
You are getting this exception because some of your
Model
property hasnull
value. But before checking this i would recommend you that you make akey
column in your model and it shouldn't beNullable
. And when you add a newobject
of yourmodel
value of thekey
column should be set before callingSaveChanges
.You key column should be like this
If you don't want to manually set the value of this
key
column when adding a newobject
you can make it identity column so its value will be inserted automatically for this you have to add[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
to yourkey
column.And make sure when you add a new
object
all of your properties have the value otherwise you will get the same exception.