Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

c# - "No such table" error after initial migration in Entity Framework Core

(Win7 x64, Visual Studio 2019, Entity Framework Core/SQLite/Tools v.5.0.2)

I follow this

https://docs.microsoft.com/ru-ru/ef/core/get-started/overview/first-app?tabs=visual-studio

tutorial on Entity Framework Core. I copy/pasted all the code just to be sure and applied initial migration with Nuget console. The console reported that the migration was applied successfully according to the log:

PM> Update-Database
Build started...
Build succeeded.
Applying migration '20210121202929_InitialCreate'.
Done.
PM> Update-Database
Build started...
Build succeeded.
No migrations were applied. The database is already up to date.
Done.
PM>  

Though the file icons of the database files created indicate that there might be a problem:

enter image description here

I tried to run the main code from tutorial, but got an error

No such table: Blogs

Code:

namespace EFCTest6
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                // Create
                Console.WriteLine("Inserting a new blog");
                // ERROR! SqliteException: SQLite Error 1: 'no such table: Blogs'.
                db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
                db.SaveChanges();

                // Read
                Console.WriteLine("Querying for a blog");
                var blog = db.Blogs
                    .OrderBy(b => b.BlogId)
                    .First();

                // Update
                Console.WriteLine("Updating the blog and adding a post");
                blog.Url = "https://devblogs.microsoft.com/dotnet";
                blog.Posts.Add(
                    new Post
                    {
                        Title = "Hello World",
                        Content = "I wrote an app using EF Core!"
                    });
                db.SaveChanges();

                // Delete
                Console.WriteLine("Delete the blog");
                db.Remove(blog);
                db.SaveChanges();
            }
        }
    }
}

I also tried to change the exception-generating line from db.Add(...) to db.Blogs.Add(...) but it generates the same exception.

question from:https://stackoverflow.com/questions/65835877/no-such-table-error-after-initial-migration-in-entity-framework-core

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure that the table exists in the database. Many people, myself included sometimes, would add the table to the Context in visual studio and then Apply the migration, but actually forget to update the database.

Type the following in the Package-Manager Console (Tools > Nuget Package Manager > Package manager Console) and type this :

Add-Migration "Blogs"

This will add the migration if it doesn't exist already. Then Type

Update-Database


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...