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



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

这是我的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]QalamWebCustomers.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)

谢谢伙计们,我收到了,你们不会相信我的问题是什么,我花了几个小时试图弄清楚问题是我有一个与dbset类同名的webform我创建了客户asp页面,但我也有一个同名的表

最新更新