1

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) 
5
  • What's the stack trace? Commented Feb 11, 2013 at 18:15
  • Do you have different databases for the computers? Perhaps db.Roles.Find(3) is the problem Commented Feb 11, 2013 at 18:17
  • I'm home now. i can paste stack trace tomorrow tho :S. @pleun: nope it s same, sqlexpress. <connectionStrings> <add name="KlmsnContext" connectionString="Data Source=.\sqlexpress;Initial Catalog=klmsncamp;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings> Commented Feb 11, 2013 at 19:12
  • Can you please post the User class, and the full stack trace? Commented Feb 11, 2013 at 19:24
  • @Slaks stacktrace: wklej.org/id/953999 Commented Feb 12, 2013 at 6:26

1 Answer 1

0

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; }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.