在Entity Framework Core和UWP中删除表 [英] Dropping table In Entity Framework Core and UWP

查看:92
本文介绍了在Entity Framework Core和UWP中删除表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UWP学习实体框架。我试图通过从DbContext中删除表来删除表。迁移后运行代码时,我收到一个错误,表明UWP不支持删除主键。
本文



此外,您还可以根据需要修改内部代码以进行迁移。


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.

这篇关于在Entity Framework Core和UWP中删除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆