不能与VB.NET流利的API定义复合PK(Cannot define composite PK w

2019-10-17 07:25发布

我试图定义组合PK的一类,其中一列是一个FK到另一个表。 代码编译没有任何错误,但是当我尝试迁移的变化,我得到以下错误

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。

仍然感到困惑。

Answer 1:

你可以保持导航属性在您的类...

Public Class ChildClass      
    Public Property ParentClassId As String

    Public Property ChildClassId As String

    Public Property Title As String

    Public Property ParentClass As ParentClass
End Class

...然后使用HasForeignKey而不是MapKey在映射:

modelBuilder.Entity(Of Models.ChildClass). _
    HasKey(Function(x) New With {x.ParentClassId, x.ChildClassId})

modelBuilder.Entity(Of Models.ChildClass). _
    HasRequired(Function(x) x.ParentClass). _
    WithMany(Function(x) x.ChildClasses). _
    HasForeignKey(Function(x) x.ParentClassId)

如果外键在数据库表中的另一名添加一列名称映射:

modelBuilder.Entity(Of Models.ChildClass). _
    Property(Function(x) x.ParentClassId). _
    HasColumnName("ContractId")


Answer 2:

看来,你必须有在子类的标量属性。 钥匙只能用明确的标量属性进行定义。



文章来源: Cannot define composite PK with Fluent API in VB.NET