我想爲一個類定義一個組合PK,其中一列是FK到另一個表。代碼編譯沒有任何錯誤,但是當我嘗試遷移改變我碰到下面的錯誤無法在VB.NET中使用Fluent API定義複合PK
PM> Update-Database -Force -TargetMigration:0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.InvalidOperationException: The properties expression 'x => new VB$AnonymousType_0`2(RtepNumber = x.RtepNumber, ContractId = x.Contract.ContractId)' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.
The properties expression 'x => new VB$AnonymousType_0`2(RtepNumber = x.RtepNumber, ContractId = x.Contract.ContractId)' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.
然而,這是理應引起例外似乎是在被請求的語法。
考慮以下類
Public Class ParentClass
Public Property ParentClassId as String
Public Property Title As String
Public Property ChildClasses As ICollection(Of ChildClass)
End Class
Public Class ChildClass
Public Property ParentClass As ParentClass
Public Property ChildClassId As String
Public Property Title As String
End Class
而下面流利的API代碼
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As System.Data.Entity.DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Entity(Of Models.ChildClass).HasRequired(Function(x) x.ParentClass).WithMany(Function(x) x.ChildClasses).Map(Function(x) x.MapKey("ContractId"))
modelBuilder.Entity(Of Models.ChildClass).HasKey(Function(x) New With {x.ParentClass.ParentClassId, x.ChildClassId})
End Sub
相應的SQL代碼將
CREATE TABLE [dbo].[ParentClass](
[ParentClassId] [nvarchar](10) NOT NULL,
[Title] [nvarchar](25) NOT NULL,
CONSTRAINT [PK_ParentClass] PRIMARY KEY CLUSTERED
(
[ParentClassId] ASC
)
)
GO
CREATE TABLE [dbo].[ChildClass](
[ParentClassId] [nvarchar](10) NOT NULL,
[ChildClassId] [nvarchar](10) NOT NULL,
[Title] [nvarchar](25) NOT NULL,
CONSTRAINT [PK_ChildClass] PRIMARY KEY CLUSTERED
(
[ParentClassId] ASC,
[ChildClassId] ASC
)
)
GO
ALTER TABLE [dbo].[ChildClass] WITH CHECK ADD CONSTRAINT [FK_ChildClass_ParentClass] FOREIGN KEY([ParentClassId])
REFERENCES [dbo].[ParentClass] ([ParentClassId])
GO
ALTER TABLE [dbo].[ChildClass] CHECK CONSTRAINT [FK_ChildClass_ParentClass]
GO
感謝您的幫助,您可以提供。
編輯:
更多的研究(即試驗)後,問題似乎從PK定義使用的導航屬性的遏制。所以改變代碼看起來像
Public Class ChildClass
Public Property ParentClassId As String
Public Property ChildClassId As String
Public Property Title As String
End Class
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As System.Data.Entity.DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Entity(Of Models.ChildClass).HasKey(Function(x) New With {x.ParentClassId, x.ChildClassId})
End Sub
清除異常但當然,我現在在ChildClass中失去了我的ParentClass導航屬性。
仍然困惑。
謝謝。但問題源於只有在兒童課堂中具有導航屬性。 – Jason
@Jason:當然可以。我的答案是在你的問題中引用你的最後一句話「*我現在在ChildClass *中失去了我的ParentClass導航屬性」,並顯示了你可以同時擁有 - 標量屬性和導航屬性。 – Slauma
哎呀。我明白。謝謝 – Jason