I am learning Entity Framework with UWP. I tried dropping a table by deleting it from DbContext. When running the code after migration I received an error that dropping primary key is not supported with UWP. This article, https://docs.efproject.net/en/latest/providers/sqlite/limitations.html, recommends using sql(string) method. This article, https://msdn.microsoft.com/en-us/data/jj592907.aspx, has an example I am trying to follow.
using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
}
I cant find the reference. I like the idea of using sql statements. In another stackoverflow article, How to drop a table in Entity Framework Code First?, I don't understand: " write the DropTable statement in the Down method of [DateStamp]_InitialCreate.cs class and the table will be dropped". Isit relevant to my problem. If so how do I implement it. Thank you.
I tried dropping a table by deleting it from DbContext.
You can use db.Database.ExecuteSqlCommand
to drop tables:
//you have to use this namespace
using Microsoft.EntityFrameworkCore;
using (var db = new BloggingContext())
{
db.Database.ExecuteSqlCommand("DROP TABLE [Blogs]");
}
I don't understand: " write the DropTable statement in the Down method of [DateStamp]_InitialCreate.cs class
<DateStamp>_<MigrationName>.cs
is generated through your migration(Add-Migration MigrationName). You can find it under Migrations folder. And inside this class file you can find Down Method.
Everytime you call db.Database.Migrate()
(normally in App.xaml.cs
file). SQLite will check the table __EFMigrationsHistory
to see if the <DateStamp>_<MigrationName>.cs
file has been executed. You can modify the timestamp of [Migration("<TimeStamp>_<MigrationName>")]
in following file to manually execute the migration again:
And also, you can modify the codes inside to do a migration according to your requirement.