简体   繁体   中英

asp.net mvc code first using db context object throws null reference exception

i have a strange problem with same code on a different pc. This is a mvc3 code first project.

I downloaded my project from svn. pressed build button. as usual, db initializer method has been called and started to execute. it created few tables with some data. no problem. as you can see :

Here is some screenshots, execution of the code, sorry cant post here since i need rep. img #0-#1-#2)

However to create users for the application, i decided to do it in a repository class. therefore i call my create method within initializer, Here is my usercreate function :

public MembershipUser CreateUser(string username, string firstname, string lastname, string password, string email)
    {
        using (KlmsnContext db = new KlmsnContext())
        {
            User user = new User();

            user.UserName = username;
            user.FirstName = firstname;
            user.LastName = lastname;
            user.Email = email;
            user.PasswordSalt = CreateSalt();
            user.Password = CreatePasswordHash(password, user.PasswordSalt);
            user.CreatedDate = DateTime.Now;
            user.IsActivated = false;
            user.IsLockedOut = false;
            user.LastLockedOutDate = DateTime.Now;
            user.LastLoginDate = DateTime.Now;

            db.Users.Add(user);

    db.SaveChanges();

            return GetUser(username);
        }
    }

However in "using block" (statement) when i look closely my new db object throws null reference for every entities,

screenshot img #3-#4

..and adding a user fails accordingly with the same " Object reference not set to an instance of an object " error. Strangely, the exact same code runs without error on a different computer. with almost same setup and os.

Any ideas on whats causing this? thanks!

update #1: StackTrace :

(InternalContext c)
   at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
    at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) at
    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1
    action)
    at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
    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.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) at System.Data.Entity.DbSet`1.Add(TEntity entity) at Klmsncamp.Models.UserRepository.CreateUser(String username, String firstname, String lastname, String password, String email) in C:\Users\Hp\Documents\Visual Studio 2010\Projects\Project2Klmsan\Klmsncamp\Models\UserRepository.cs:line 31 at Klmsncamp.DAL.KlmsnInitializer.Seed(KlmsnContext context) in C:\Users\Hp\Documents\Visual Studio 2010\Projects\Project2Klmsan\Klmsncamp\DAL\KlmsnInitializer.cs:line 212 at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext
    context)
    at
    System.Data.Entity.Database.c__DisplayClass2`1.b__0(DbContext
    c)
    at
    System.Data.Entity.Internal.InternalContext.c__DisplayClass5.b__3()
    at
    System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action
    action)
    at
    System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
    at
    System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext
    c)
    at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)

Can you at least show me your DbContext class , I think you probably didn't set your property to DBset Type

public class EnocDB : DbContext
  {
      public DbSet<EnocParcels> Parcels { get; set; }
      public DbSet<ReviewEnoc> Reviews { get; set; }
  }

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