当我调用任何dbset类时,EF DBContext抛出空引用

时间:2015-12-23 21:40:44

标签: c# asp.net entity-framework

晚安我是Enitity框架的新手我创建新的asp.net Web应用程序并首先从现有数据库添加新项目模型代码当我尝试调用任何dbset类时,我得到异常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)
    {}}

当我尝试使用像这样的dbset

时,我尝试创建新客户或任何东西
 using (var dbctx = new Model1())
       {
        var customer = new Customers();
        customer.Name = NameTxt.Text;
        customer.CustomerCompany = CompanyName.Text;
        customer.Customer_Type = TypeTxt.Text;
        customer.Nationality = NationalityTxt.Text;
        customer.Mobile = MobileTxt.Text;
        customer.Phone = phoneTxt.Text;
        customer.Address1 = addressTxt.Text;
        customer.City = cityTxt.Text;
        customer.Street = StreetTxt.Text;
        customer.Fax = faxTxt.Text;
        customer.Notes = NoteTxt.Text;

        customer.Address2 = "";
        customer.DeletedDate = System.DateTime.Now;
        customer.User_ID = 4;
        customer.User_Name = "";
        customer.Photo = "";
        customer.Website = "";
        customer.Email = "";
        customer.Location = "";
        customer.IsDeleted = false;


        dbctx.Customers.Add(customer); // exception here 
        dbctx.SaveChanges(); try
        {
        }
        catch { }

异常错误

 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)

1 个答案:

答案 0 :(得分:1)

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