本文介绍了无法在VB.NET中使用Fluent API定义复合PK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一类定义复合PK,其中一列是另一张表的FK.代码编译时没有任何错误,但是当我尝试迁移更改时,出现以下错误

I am trying to define a composite PK for a class where one of the columns is a FK to another table. The code compiles without any errors but when I try to migrate the changes I get the following error

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 }'.

但是,可能导致异常的似乎确实在所请求的语法中.

However, the that is supposedly causing the exception does seem to be in the syntax that is requested.

提供以下课程

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

以及以下Fluent API代码

And the following Fluent API code

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

感谢您提供的任何帮助.

Thank you for any help you can provide.

经过更多研究(即进行实验),该问题似乎源于PK定义中导航属性的使用.因此,将代码更改为

After more research (i.e. experimenting) the problem seems to stem from the use of the navigation property in the PK definition. So changing the code to look like

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导航属性.

clears the Exception but, of course, I now lost my ParentClass navigation property in ChildClass.

仍然感到困惑.

推荐答案

似乎您必须在子类中具有标量属性.只能使用显式标量属性来定义键.

It seems that you have to have the scalar property in the child class. Keys can only be defined using explicit scalar properties.

这篇关于无法在VB.NET中使用Fluent API定义复合PK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:09