locked
Can not migrate Database RRS feed

  • Question

  • User-146938715 posted

    Hello. I don't know how to just ask the question.

    When I run update-database, a database is created. However, without the table __Migrations (or whatever its name was). The database is up-to-date.
    When I then execute update database, the above table is created. but has only the first entry. I think because the update stopped there. Who can help?

     public async Task SeedAsync()
            {
                
                if (!DBContext.Database.EnsureCreated())
                    DBContext.Database.Migrate();
                
            }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, DataService seeder)
            {
                loggerFactory.AddFile("Logs/myapp-{Date}.txt");
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                    {
                        HotModuleReplacement = true
                    });
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
                app.UseDeveloperExceptionPage();
                app.UseStaticFiles();
    
    
                app.UseAuthentication();
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
                // here you can see we make sure it doesn't start with /api, if it does, 
                // it'll 404 within .NET if it can't be found
                app.MapWhen(x => !x.Request.Path.Value.StartsWith("/api"), builder =>
                {
                    builder.UseMvc(routes =>
                    {
                        routes.MapSpaFallbackRoute(
                            name: "spa-fallback",
                            defaults: new { controller = "Home", action = "Index" });
                    });
                });
    
    
                seeder.SeedAsync().Wait();
            }

    Tuesday, January 23, 2018 5:01 PM

Answers

  • User-146938715 posted

    I found the problems.

    • First, the Initialmigration don't create the tables.
    • Second, i must used an IDesignTimeDBContextFactory
    • I must delete all migrations and the Snapshot
    • I must delete my corrupt database

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 2:49 PM

All replies

  • User753101303 posted

    Hi,

    See the doc at https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.infrastructure.databasefacade.ensurecreated?view=efcore-2.0

    EnsureCreated is basically for quick drop/create tests. If using migrations try to just call DBContext.Database.Migrate().

    You have a dedicated ASP.NET Core forum which could be better.

    Tuesday, January 23, 2018 5:15 PM
  • User-146938715 posted

    Hi,

    thanks for your answer. If i use Migrate only and don't have a database, i get an error. and i don't know how to create one. all example i can find use this code. my old project in asp core 1.1 works fine.

    do you think this is an asp.core problem?

    Tuesday, January 23, 2018 5:27 PM
  • User753101303 posted

    i get an error.

    And the error is? You deleted the database? My understanding is that Migrate (will create the database if needed) won't work on a database created previously using EnsureCreated.

    I'm new myself and never really liked migrating from code.

    Tuesday, January 23, 2018 5:49 PM
  • User-146938715 posted

    Yes. I delete the Development-Database. Sometimes it is better to roll back the database. 

    Tuesday, January 23, 2018 5:52 PM
  • User753101303 posted

    And so the error you have is?

    Tuesday, January 23, 2018 5:54 PM
  • User-146938715 posted

    If i run the code without ensurecreated, i get an errormessage like you have no persmission or your user have no permission or the the table is not found. 

    how can i create the database right? if i used update-database like written here https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db

    i get this error 

    An error occurred using the connection to database 'Portal' on server '(localdb)\mssqllocaldb'.
    System.Data.SqlClient.SqlException (0x80131904): Cannot open database "Portal" requested by the login. The login failed.
    Login failed for user 'xxx\xxx.xxxl'.


    (i changed the x)

    this is my connection string

    "Database": "Server=(localdb)\\mssqllocaldb;Database=Portal;Trusted_Connection=True;MultipleActiveResultSets=true"

    Tuesday, January 23, 2018 5:57 PM
  • User-146938715 posted

    I found the problems.

    • First, the Initialmigration don't create the tables.
    • Second, i must used an IDesignTimeDBContextFactory
    • I must delete all migrations and the Snapshot
    • I must delete my corrupt database

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 2:49 PM