[英]How to retrieve inserted identity in migration of FluentMigrator
在迁移过程中,如何插入到我的表中,然后检索ID,然后使用它在另一个表中插入相关数据。
我现在拥有的是一个要插入的硬拷贝ID,但我不知道当我运行迁移时它会是什么。
var contactId = 2;
var phoneNumber = 2;
Insert.IntoTable("Contacts")
.WithIdentityInsert()
.Row(new
{
Id = contactId,
TimeZoneId = contact.TimeZoneId,
contact.CultureId,
Type = (byte)(int)contact.Type,
Email = contact.Email.ToString(),
EntityId = entityId
});
Insert.IntoTable("PhoneNumbers")
.WithIdentityInsert()
.Row(new
{
Id = phoneNumberId,
phone.Number,
Type = (byte)(int)phone.Type,
ContactId = contactId
});
我希望能够检索插入的ID并将其用于第二次插入而不是对其进行编码。
我正在使用SQL Server,如果它有任何帮助...
我认为这将是微不足道的,但似乎不是,谷歌搜索后,并没有在这里找到任何答案。
您可以在.Row()
调用后通过链接.WithInsertIdentity()
手动插入Id。
这将允许您将其保留在内存中,以便在其他对象中用作外键。 不幸的是,在Up()
或Down()
方法中的所有代码完成执行之前,FluentMigrator实际上并不执行任何SQL。
对于相同的情况,我在sql-query中使用带有@@IDENTITY
Execute.Sql()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.