Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:28:18+00:00 2026-06-12T11:28:18+00:00

I am trying to define a composite PK for a class where one of

  • 0

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.

Given the following classes

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

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

The equivalent SQL code would be

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.

Edit:

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

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

Still perplexed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:28:20+00:00Added an answer on June 12, 2026 at 11:28 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to define nested modules, but I couldn't find any complete explanations
I am trying to define a composite list item in an Android ListView but
I'm trying to define an infix operator but I keep getting errors. I'm using
I'm trying to define a custom formatter for my class, but I can't find
I am trying to define an abstract class that has operators to compare two
I'm trying to define a new array through a where query but I can
I am trying to define MapPageRoute on the application global.asax but my problem is
I'm trying to Order my main table(Contract) by some value in another table (via
I am trying to define a class inside a module with the Ruby C
I`m trying to define a variable of string type in a class definition in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.