我在迁移数据库时遇到问题。只是说 - 我是新手所以请不要非常认真地评判我。 这是我的两个表(模型),它们应该是一对一或零 关系:
<Table("User")>
Partial Public Class User
<Key>
Public Property UserId As Integer
<StringLength(50)>
Public Property FirstName As String
<StringLength(50)>
Public Property LastName As String
<StringLength(50)>
Public Property Username As String
<StringLength(50)>
Public Property Password As String
Public Property Email As String
Public Property Age As Integer
Public Overridable Property User_Wallet As User_Wallet
End Class
<Table("User_Wallet")>
Public Class User_Wallet
<Key, ForeignKey("User")>
Public Property UserId As Integer
<DataType(DataType.Currency)>
Public Property WalletBalance As Decimal
Public Overridable Property User As User
End Class
当我尝试应用以下迁移时:
Namespace Migrations
Public Partial Class _4
Inherits DbMigration
Public Overrides Sub Up()
DropForeignKey("dbo.User_WalletUser", "User_Wallet_WalletId", "dbo.User_Wallet")
DropForeignKey("dbo.User_WalletUser", "User_UserId", "dbo.User")
DropIndex("dbo.User_WalletUser", New String() { "User_Wallet_WalletId" })
DropIndex("dbo.User_WalletUser", New String() { "User_UserId" })
DropPrimaryKey("dbo.User_Wallet")
DropColumn("dbo.User_Wallet", "WalletId")
DropTable("dbo.User_WalletUser")
AddPrimaryKey("dbo.User_Wallet", "UserId")
CreateIndex("dbo.User_Wallet", "UserId")
AddForeignKey("dbo.User_Wallet", "UserId", "dbo.User", "UserId")
'DropColumn("dbo.User_Wallet", "WalletId")
'DropTable("dbo.User_WalletUser")
End Sub
Public Overrides Sub Down()
CreateTable(
"dbo.User_WalletUser",
Function(c) New With
{
.User_Wallet_WalletId = c.Int(nullable := False),
.User_UserId = c.Int(nullable := False)
}) _
.PrimaryKey(Function(t) New With { t.User_Wallet_WalletId, t.User_UserId })
AddColumn("dbo.User_Wallet", "WalletId", Function(c) c.Int(nullable := False, identity := True))
DropForeignKey("dbo.User_Wallet", "UserId", "dbo.User")
DropIndex("dbo.User_Wallet", New String() { "UserId" })
DropPrimaryKey("dbo.User_Wallet")
AddPrimaryKey("dbo.User_Wallet", "WalletId")
CreateIndex("dbo.User_WalletUser", "User_UserId")
CreateIndex("dbo.User_WalletUser", "User_Wallet_WalletId")
AddForeignKey("dbo.User_WalletUser", "User_UserId", "dbo.User", "UserId", cascadeDelete := True)
AddForeignKey("dbo.User_WalletUser", "User_Wallet_WalletId", "dbo.User_Wallet", "WalletId", cascadeDelete := True)
End Sub
End Class
End Namespace
我在PackageManager控制台中收到此错误:
**指CREATE UNIQUE INDEX语句因重复键
而终止找到对象名称'dbo.User_Wallet'和索引名称的'PK_dbo.User_Wallet'。重复键值为(0)。无法创建 约束或索引。查看以前的错误。声明一直如此 终止。**
我的表格首先是我尝试应用迁移的一些数据,当谷歌搜索一下并在这里阅读一些帖子时,我删除了所有数据 - 现在表格都是空的,但我仍然得到同样的错误。 任何人都可以给我一个方向,我的错误是什么?