简体   繁体   English

实体框架包管理器控制台问题

[英]Entity Framework Package Manager Console question

I have EF 5 and I added the following table and it added successfully as shown in code snippet #1. 我有EF 5,我添加了下表,并且添加成功,如代码片段1所示。 However, I want the UserID to be a foreign key that references the Users table and if I did that, then my code would have looked like code snippet# 2. Without deleting this new table, can you please tell me how I can now make the UserID to be a foreign key that references Users table. 但是,我希望UserID是引用Users表的外键,如果这样做,那么我的代码将看起来像代码片段2。如果不删除此新表,您能否告诉我现在该如何做? UserID是引用用户表的外键。 Thank you so much. 非常感谢。 I'm using Package Manager Console to achieve this. 我正在使用Package Manager Console来实现此目的。

Code snippet #1 代码段#1

public partial class Initialignorechanges : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Favorite",
            c => new
            {
                ID = c.Int(nullable: false, identity: true),
                UserID = c.Int(nullable: false),
                UserName = c.String(nullable: true, maxLength: 25, unicode: false),
                FavoritedUserID = c.Int(nullable: false),
                FavoritedUserName = c.String(nullable: true, maxLength: 25, unicode: false),
                FavoritedDate = c.DateTime(),
                ShowToUser = c.Boolean(nullable: false),
                ShowToFavoritedUser = c.Boolean(nullable: false),
            })
            .PrimaryKey(t => t.ID);                
    }

    public override void Down()
    {            
        DropTable("dbo.Favorite");
    }
}

Code snippet #2: 代码片段2:

public partial class Initialignorechanges : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Favorite",
            c => new
            {
                ID = c.Int(nullable: false, identity: true),
                UserID = c.Int(nullable: false),
                UserName = c.String(nullable: true, maxLength: 25, unicode: false),
                FavoritedUserID = c.Int(nullable: false),
                FavoritedUserName = c.String(nullable: true, maxLength: 25, unicode: false),
                FavoritedDate = c.DateTime(),
                ShowToUser = c.Boolean(nullable: false),
                ShowToFavoritedUser = c.Boolean(nullable: false),
            })
            .PrimaryKey(t => t.ID)
            .ForeignKey("dbo.Users", t => t.UserID, cascadeDelete: true)
            .Index(t => t.UserID);
    }

    public override void Down()
    {
        DropIndex("dbo.Favorite", new[] { "UserID" });
        DropForeignKey("dbo.Favorite", "UserID", "dbo.Users");
        DropTable("dbo.Favorite");
    }
}

Take a look at Code-Based Migration in Entity Framework 6 . 看看Entity Framework 6中基于代码的迁移

Now, you have to create a migration class using the Add-Migration command with the name of your migration class, as shown below. 现在,您必须使用Add-Migration命令以及您的迁移类的名称来创建一个迁移类,如下所示。

在此处输入图片说明

After creating a migration file using the add-migration command, you have to update the database. 使用add-migration命令创建迁移文件后,您必须更新数据库。 Execute the Update-Database command to create or modify a database schema. 执行Update-Database命令以创建或修改数据库模式。 Use the –verbose option to view the SQL statements being applied to the target database. 使用–verbose选项可以查看应用于目标数据库的SQL语句。

在此处输入图片说明

At this point, the database will be created or updated. 此时,将创建或更新数据库。 Now, whenever you change the domain classes, execute Add-Migration with the name parameter to create a new migration file and then execute the Update-Database command to apply the changes to the database schema. 现在,每当更改域类时,请使用name参数执行Add-Migration以创建新的迁移文件,然后执行Update-Database命令以将更改应用于数据库架构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在项目中管理Entity Framework的程序包管理器控制台命令 - Managing Package Manager Console commands for Entity Framework in project 实体框架程序包管理器控制台命令错误-生成失败 - Entity Framework Package Manager Console Commands Error - failed to build Project X 以框架“.NETStandard”为目标。 实体框架 Package 管理器控制台工具不支持此框架 - Project X targets framework '.NETStandard'. The Entity Framework Package Manager Console Tools don't support this framework 使用包管理器控制台“ Update-Database”等在实体框架中以编程方式连接字符串 - Programmatically connection string in entity framework with package manager console “Update-Database” etc 实体框架:在包管理器控制台VS 2012中添加迁移时抛出Null Reference Exception - Entity framework: Null Reference Exception thrown when adding migration in package manager console VS 2012 当我在 package 管理器控制台中的 Visual Studio 2013 中安装实体框架时 - When I am installing Entity Framework in Visual Studio 2013 in the package manager console 实体框架:package 管理控制台中的启用迁移不起作用 - Entity Framework: enable-migrations in package management console does not work 复杂的顺序问题(实体框架) - Complex orderby question (entity framework) 关于实体框架和交易的问题 - Question about Entity Framework and Transactions LINQ with Entity Framework-基本问题 - LINQ with Entity Framework - basic question
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM