0

I'm creating a web app that will allow users to log in to execute certain actions. I followed this tutorial (for simplicity, you can search for "Generate the database to store membership data" for the exact database setup I used).

Everything works fine in development, but when I deploy it and try to register a new user, it gives me the error:

An unhandled exception has occurred: SQLite Error 1: 'no such table: AspNetUsers'.

Is this because I need to do a 'dotnet ef database update' in deployment? If so, is there anyway I can avoid it, such that I can just deploy and have the database setup itself and be ready? The tutorial semi-talked about it by calling 'dbContext.Database.Migrate();', but I have no dbContext in the Configure() method... so I'm not sure how to fill in the gaps.

If there is any information about my code you'd like me to post, I'll be happy to post it for you. Thanks in advance!

7
  • Generating and applying migrations is not a function of your application. It needs to be part of your release (using something like ReadyRoll) or you or your DBA needs to manually apply any schema updates as part of your deployment procedure. Commented Jul 13, 2017 at 16:56
  • @ChrisPratt Thanks for the response! That's a bummer, I was hoping for an easier solution from within the application. So would it work if I included a script that does the dotnet commands (like the ones in the tutorial) and call it from the app to achieve what I want? This web app will be delivered to each of our customers to use (all of which have little to no experience in programming), so I need to make sure that I can just hand them the web app, and they can just use it without having to configure anything. Commented Jul 13, 2017 at 17:11
  • 1
    If you're distributing it as a package, you should have an "installer". That could be as complex as a full blown GUI installer or as simple as a powershell script. Either you assume the end user is competent enough to be able to create/modify a database on their own or you script it for them. Commented Jul 13, 2017 at 17:14
  • @ChrisPratt Yes, I will be distributing it as a package. This is great, thanks for clearing things up for me! I'll give that a shot :) Commented Jul 13, 2017 at 17:16
  • @susieloo_ manage to fix it? Commented Oct 2, 2017 at 7:03

1 Answer 1

1

I did happen to figure out how to fix the problem, but please bear with me on the details of the solution. It has been so long that I can only remember that Step 1 is definitely necessary, but I'm fairly fuzzy on if we also need to do Step 2. Some point down the line when I have time, I'll revisit this to confirm, but for now, I hope this will give you enough help to overcome the problem.

Step 1: Create your AspNetUsers table in your custom context class that inherits from IdentityDbContext<IdentityUser> with the following code. For the purposes of my example, I'm calling this class: CustomDbContext.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");
    base.OnModelCreating(modelBuilder);
}

Note: If you aren't following the guide or using this for authentication, you'd simply have your custom context class inherit from DbContext.

Step 2: In your Startup.cs, you'll need to put the following code in your Startup() method, where you are literally ensuring that the CustomDbContext database (AspNetUsers) is created and available in production.

using (var client = new CustomDbContext())
{
    client.Database.EnsureCreated();
}

Note: There is speculation that you could do client.Database.Migrate() in that using statement instead according to this (credit to Thomas Schneiter for letting me know), but I have not personally tried it to see if it works. There is another SO post relating to this particular piece of code with an answer I posted.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sharing your solution. On my case it was a different reason. I had a seed data initializer that was being called inside the startup.cs. So when i try to update the migrations it runs the initializer for some reason and can't find the tables. I just had comment it out the initializer before run the migration for the first time..

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.