Goodnight I am new to Enitity framwork I create new asp.net web application and add new item model code first from exist database When I try to call any dbset class I got exception null reference
Here is my DbContext Class :
public partial class Model1 : DbContext
{
public Model1()
: base("name=Model1")
{}
public virtual DbSet<Customers> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{}}
I try to create new customer or any thing when ever I try to use dbset like this
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 { }
exception error
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.
Stack trace
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)