0

In asp.net webform application, trying to save user to AspNetUsers after UPDATE-DATABASE command. the following code doesnt do that. solution ?

        public Configuration()
        {
           AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(MyApp.Models.ApplicationDbContext context)
        {
            if (!context.Users.Any(u => u.Email == "some@mail"))
            {
                var store = new UserStore<ApplicationUser>(context);
                var manager = new UserManager<ApplicationUser>(store);
                var user = new ApplicationUser { Email = "some@mail" };

                manager.Create(user, "password");
            }
        }
Nadav
  • 220
  • 4
  • 12
  • AFAIK, Seed is only called on a database creation, not for an upgrade. For this you need to enable [migration](http://stackoverflow.com/questions/8448087/how-to-seed-data-using-entityframework-code-first-migrations). – tschmit007 Aug 26 '14 at 13:59
  • @tschmit007 i have enabled migrations. and seed is called whenever u call UPDATE-DATABASE command on the Package Manager Console. – Nadav Aug 26 '14 at 14:09
  • sorry, I was on the Seed of the database initializer. indeed, the Seed method of configuration is called. Have you considered the solution of [this post](http://stackoverflow.com/questions/9342459/best-way-to-incrementally-seed-data-in-entity-framework-4-3) ? – tschmit007 Aug 26 '14 at 14:17
  • @tschmit007 10x, but i wanna know y my solution doesnt work – Nadav Aug 26 '14 at 14:22
  • imho, that is because the only utpdate is the seed, so there is not real update, so seed is not called. – tschmit007 Aug 26 '14 at 14:34
  • @tschmit007 `PM> update-database Specify the '-Verbose' flag to view the SQL statements being applied to the target database. No pending explicit migrations. Applying automatic migration: 201408261438410_AutomaticMigration. Running Seed method.` – Nadav Aug 26 '14 at 14:41
  • well, try adding a context.SaveChanges(), but that is weird... what is the value of .Succeeded of IdentityResult returned by manager.Create ? – tschmit007 Aug 26 '14 at 14:47
  • @tschmit007 context.SaveChanges() doesnt help and is not needed here – Nadav Aug 26 '14 at 14:53
  • I know but it was my last idea with checking the .Succeeded valuer of the Create return in case of incorrect password: not long enough, not complicated enough.... – tschmit007 Aug 26 '14 at 14:54

1 Answers1

1

In order to add ApplicationUser you must have the property Username initialized.

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
nadav
  • 552
  • 5
  • 11