简体   繁体   中英

NullReferenceException on Entity Framework constructor

I have a DER.edmx , and in DER.Designer.cs there is this autogenerated code

public SiGMAEntities(string connectionString) : base(connectionString, "SiGMAEntities")
{
    this.ContextOptions.LazyLoadingEnabled = true;
    OnContextCreated();
}

the line that throws the exception is

public SiGMAEntities(string connectionString) : base(connectionString, "SiGMAEntities")

A couple of days ago, everything worked perfectly but I had to re-install Windows for other reasons, and of course VS and SQL Server. The exception happened when I wanted to start working on the project again.

Stack trace

    [NullReferenceException: Object reference not set to an instance of an object.]
   System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.Remove(Object key) +48
   System.Data.Metadata.Edm.MetadataWorkspace.RegisterDefaultObjectMappingItemCollection() +95
   System.Data.Metadata.Edm.MetadataWorkspace.GetItemCollection(DataSpace dataSpace, Boolean required) +124
   System.Data.Metadata.Edm.MetadataWorkspace.GetItemCollection(DataSpace dataSpace) +48
   System.Data.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor) +438
   System.Data.Objects.ObjectContext..ctor(String connectionString) +65
   System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) +37
   AccesoADatos.SiGMAEntities..ctor(String connectionString) in E:\Documentos(NO BORRAR)\Documentos de GonDC\Facu\SiGMA\asd - Copy\trunk\AccesoADatos\DER.Designer.cs:79
   AccesoADatos.Conexion.crearSegunServidor() in E:\Documentos(NO BORRAR)\Documentos de GonDC\Facu\SiGMA\asd - Copy\trunk\AccesoADatos\Conexion.cs:27
   AccesoADatos.Datos.TiposDNI() in E:\Documentos(NO BORRAR)\Documentos de GonDC\Facu\SiGMA\asd - Copy\trunk\AccesoADatos\Datos.cs:14
   Herramientas.CargarCombos.cargarTipoDocumento(DropDownList& ddl) in E:\Documentos(NO BORRAR)\Documentos de GonDC\Facu\SiGMA\asd - Copy\trunk\Herramientas\CargarCombos.cs:30
   SiGMA._Default.Page_Load(Object sender, EventArgs e) in E:\Documentos(NO BORRAR)\Documentos de GonDC\Facu\SiGMA\asd - Copy\trunk\SiGMA\RegistrarUsuario.aspx.cs:26
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

This can happen if you dont have the right connectionString specified in your App.config or web.config. Make sure that the running applications App.config or web.config respectively contains the connectionString that you specified when defining the database model. Something like this:

<connectionStrings>
  <add name="SiGMAEntities" connectionString="metadata=res://*/xxx.csdl|res://*/xxx.ssdl|res://*/xxx.msl;provider=System.Data.SqlClient;provider connection string=&quot;xxx&quot;" providerName="System.Data.EntityClient"/>
</connectionStrings>

xxx is just a placeholder for your stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM