我有 EF 5,我添加了下表,它添加成功,如代码片段 #1 所示。但是,我希望UserID是引用Users表的外键,如果我这样做了,那么我的代码将看起来像代码片段#2。在不删除这个新表的情况下,你能告诉我我现在该怎么做UserID 是引用用户表的外键。太感谢了。我正在使用包管理器控制台来实现这一点。
代码片段#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");
}
}
代码片段#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);
}
牧羊人nacy
相关分类