Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我调用任何dbset类时,EF DBContext抛出null引用_C#_Asp.net_Entity Framework - Fatal编程技术网

C# 当我调用任何dbset类时,EF DBContext抛出null引用

C# 当我调用任何dbset类时,EF DBContext抛出null引用,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,晚安,我是Enitity framwork的新手。我创建了新的asp.net web应用程序,并在尝试调用任何dbset类时,首先从exist数据库添加了新的项目模型代码。我得到了异常null引用 以下是我的DbContext类: public partial class Model1 : DbContext { public Model1() : base("name=Model1") {} public virtual DbSet

晚安,我是Enitity framwork的新手。我创建了新的asp.net web应用程序,并在尝试调用任何dbset类时,首先从exist数据库添加了新的项目模型代码。我得到了异常null引用

以下是我的DbContext类:

  public partial class Model1 : DbContext
    {
       public Model1()
          : base("name=Model1")
      {}

 public virtual DbSet<Customers> Customers { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {}}
异常错误

 An exception of type 'System.NullReferenceException' occurred in       EntityFramework.dll but was not handled in user code  Additional information: Object reference not set to an instance of an   object.
堆栈跟踪

     at System.Web.UI.ParseChildrenAttribute.GetHashCode()
      at  System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(Tobj)
       at System.Linq.Set`1.InternalGetHashCode(TElement value)
     at System.Linq.Set`1.Find(TElement value, Boolean add)
    at System.Linq.Enumerable.<ExceptIterator>d__1`1.MoveNext()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<GetAttributes>b__3(PropertyInfo pi)
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo propertyInfo)
      at System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention`1.<.ctor>b__0(ConventionTypeConfiguration ec)
   at System.Data.Entity.ModelConfiguration.Conventions.TypeConvention.ApplyCore(Type memberInfo, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(Type memberInfo, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
     at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
     at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
   at System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Internal.Linq.InternalSet`1.Create()
   at System.Data.Entity.DbSet`1.Create()
   at QalamWeb.Customers.SaveBtn_Click(Object sender, EventArgs e) in [Project path]\QalamWeb\Customers.aspx.cs:line 48
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
位于System.Web.UI.ParseChildrenAttribute.GetHashCode()处
位于System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(Tobj)
在System.Linq.Set`1.InternalGetHashCode处(远程登录值)
在System.Linq.Set`1.Find处(远程输入值,布尔加法)
在System.Linq.Enumerable.d_u1`1.MoveNext()中
位于System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
位于System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.b__3(PropertyInfo pi)
位于System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey,Func`2 valueFactory)
位于System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo PropertyInfo)
位于System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention`1.b_u0(ConventionTypeConfiguration ec)
位于System.Data.Entity.ModelConfiguration.Conventions.TypeConvention.ApplyCore(类型memberInfo,模式配置模式配置)
位于System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(类型memberInfo、ModelConfiguration、ModelConfiguration)
位于System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(类型类型,模型配置模型配置)
位于System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(类型类型,模型配置,模型配置)
位于System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(类型类型,模型配置模型配置)
位于System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(类型类型)
位于System.Data.Entity.DbModelBuilder.MapTypes(EdmModel模型)
位于System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
位于System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
位于System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
位于System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)
在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()中
位于System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型entityType)
位于System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
位于System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
位于System.Data.Entity.Internal.Linq.InternalSet`1.Create()
在System.Data.Entity.DbSet`1.Create()处
在QalamWeb.Customers.SaveBtn_中,单击[项目路径]\QalamWeb\Customers.aspx.cs:第48行中的(对象发送者,事件参数e)
在System.Web.UI.WebControls.Button.OnClick(EventArgs e)中
位于System.Web.UI.WebControl.Button.RaisePostBackEvent(String eventArgument)
位于System.Web.UI.WebControl.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串事件参数)
位于System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)
位于System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)

谢谢各位,我明白了,你们不会相信我的问题是什么,我花了好几个小时试图解决它 问题是我有与dbset类同名的webform
我创建了客户asp页面,但我也有同名的表

如果您还不知道,在最后使用try/catch将不会捕获任何异常客户中是否存在任何未满足的不可为null的关系?客户的主键是什么?您使用什么方式生成EF模型?请发布
客户
类别代码可能重复的
     at System.Web.UI.ParseChildrenAttribute.GetHashCode()
      at  System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(Tobj)
       at System.Linq.Set`1.InternalGetHashCode(TElement value)
     at System.Linq.Set`1.Find(TElement value, Boolean add)
    at System.Linq.Enumerable.<ExceptIterator>d__1`1.MoveNext()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<GetAttributes>b__3(PropertyInfo pi)
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo propertyInfo)
      at System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention`1.<.ctor>b__0(ConventionTypeConfiguration ec)
   at System.Data.Entity.ModelConfiguration.Conventions.TypeConvention.ApplyCore(Type memberInfo, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(Type memberInfo, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
     at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
     at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
   at System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Internal.Linq.InternalSet`1.Create()
   at System.Data.Entity.DbSet`1.Create()
   at QalamWeb.Customers.SaveBtn_Click(Object sender, EventArgs e) in [Project path]\QalamWeb\Customers.aspx.cs:line 48
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)