【发布时间】:2016-03-05 00:48:44
【问题描述】:
我的代码优先数据模型从标准 ApplicationUser 实体开始,其中包括邮政地址和帐单属性。现在我通过添加父 Account 实体扩展了我的模型:
- 一个账号有多个ApplicationUser; ApplicationUser 现在具有相关帐户记录的不可为空的外键
- 帐单和邮政地址现在与 Account 实体而非 ApplicationUser 实体相关联,因此相关属性已从 ApplicationUser 实体移至 Account 实体
为了更新数据库模式以合并新的父表,我会将每个现有的 ApplicationUser 分配给一个新创建的 Account。所以,我需要为每个现有的 ApplicationUser 做两件事:
- 我需要创建一个新的帐户行,其帐单和邮政地址字段值取自 ApplicationUser 行。
- 我需要将 ApplicationUser.AccountId 外键字段设置为新创建的 Account 行的主键值。
请注意,我的代码托管在 Azure 上,数据在 Azure SQL Server 中。
脚手架的迁移代码看起来像这样(大大简化了)
// Create the new Accounts table
CreateTable(
"dbo.Accounts",
c => new
{
Id = c.Int(nullable: false, identity: true),
BillingInfo = c.String(),
PostalAddress = c.String(),
})
.PrimaryKey(t => t.Id);
// Add the new FK column
AddColumn("dbo.AspNetUsers", "AccountId", c => c.Int(nullable: false));
CreateIndex("dbo.AspNetUsers", "AccountId");
// Before we add the AspNetUsers.AccountId foreign key, we need to populate
// the Accounts table (one Account for each User)
Sql("some SQL command(s)")
// Make the column a foreign key
AddForeignKey("dbo.AspNetUsers", "AccountId", "dbo.Accounts", "Id", cascadeDelete: true);
// Deleted fields
DropColumn("dbo.AspNetUsers", "BillingInfo");
DropColumn("dbo.AspNetUsers", "PostalAddress");
据我所知,创建或修改数据的唯一选择是使用Sql() 方法(或SqlFile() 或SqlResource()),如上面的代码所示。
如果我仅限于使用 SQL,什么样的 SQL 命令可以完成任务?我可以在一个大的 ol' 命令中使用某种 JOIN(对尚不存在的记录)执行此操作,还是需要使用 SQL 循环 (as shown in this article)?
【问题讨论】:
-
以这种格式列出您期望的关系......
1 account has many users、1 user has 1 account...等 -
您应该使用 SQL 为您的特定规则创建手动迁移脚本,然后使用包管理器控制台在 EF 中创建空迁移,并使用
Sql(@"<your migration code>");方法在此处插入此迁移脚本 -
@raderick 这就是问题的要点。我知道我需要在迁移脚本中添加“”注释。如果我能弄清楚如何使用与我将在 Seed 方法中使用的代码等效的东西,那么我就可以回家了,因为我可以遍历现有的用户记录并创建关联的帐户记录。但如果我仅限于使用 SQL,我不知道该怎么做。
标签: c# sql sql-server entity-framework