如何删除表中的实体框架代码优先?

编程入门 行业动态 更新时间:2024-10-24 00:28:56
本文介绍了如何删除表中的实体框架代码优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用实体框架与自动迁移。

I am using Entity Framework with Auto Migrations.

所以,当我添加一个新的模式,我的背景下,我的数据库进行更新,并创建新表。

So when I add a new model to my Context, my database is updated and new table is created.

我想要做的是相反的,从数据库中完全滴速表。然而,从Context类删除定义不起作用

What I want to do is the opposite, droping the table completely from database. However, removing the definition from Context class does not work.

public class CompanyContext : DbContext { public DbSet<Permission> Permissions { get; set; } public DbSet<Company> Companies { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } }

比如说,我想删除从数据库公司表。从 CompanyContext 类要做到这一点,我删除公司属性。然而,这是行不通的。

For instance, I want to remove Company table from database. To do that, I remove Companies property from CompanyContext class. However, it is not working.

什么是砸在EF表的正确方法,并使用自动迁移如果可能的话?

What is the proper way to drop tables in EF and using auto migrations if possible ?

推荐答案

外接迁移命令创建一个文件夹迁移。你可以看到一个包含两个方法即; [日期戳] _InitialCreate.cs文件上和下。在InitialCreate类的最多的方法创建一个对应的数据模型实体集的数据库表,而向下的方法将它们删除。通常情况下,当你输入一个命令,回退数据库,唐氏方法被调用。你可以看到像DropIndex,DropForeignKey,唐氏方法DROPTABLE陈述。

The add-migrations command creates a Migrations folder. You can see [DateStamp]_InitialCreate.cs file containing two methods viz.; Up and Down. The Up method of the InitialCreate class creates the database tables that corresponds to the data model entity sets, and the Down method delete them. Typically when you enter a command to rollback a database, Down method is called. You can see statements like DropIndex, DropForeignKey, DropTable statements in Down method.

在问题被埃姆雷要求的情况下,写DROPTABLE声明的向下方法[日期戳] _InitialCreate.cs类和表将被丢弃。

In case of question asked by Emre, write the DropTable statement in the Down method of [DateStamp]_InitialCreate.cs class and the table will be dropped.

希望这会有所帮助。

更多推荐

如何删除表中的实体框架代码优先?

本文发布于:2023-11-05 09:54:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!