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

  • SEARCH
  • Home
  • 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 8578433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:23:16+00:00 2026-06-11T20:23:16+00:00

So I got a class called ObjectA it has many ObjectsB, the objects got

  • 0

So I got a class called ObjectA it has many ObjectsB, the objects got a Id, to identify them but ALSO a language (enum, but it is actually saved as and INT and doesn t matter either for this case).

public class ObjectA
{
       public int Id {get;set;}
       public Language Language {get;set;}
       ... // and a list of objectA properties

       public virtual ICollection<ObjectB> ObjectBs {get;set;}
}

public class ObjectB
{
       public int Id {get;set;}
       public Language Language {get;set;}
       ... // and a list of objectB properties

       public ObjectA TheObjectA {get;set;}
}

Now I learned how to map them both as a primary key, I use fluent API for this. I know you can also use [Key] and [Column] attributes (which I don t use):

modelBuilder.Entity<ObjectA>().HasKey(a => new {a.Id, a.Languague})
modelBuilder.Entity<ObjectB>().HasKey(a => new {a.Id, a.Languague})

Now I tried a lot of stuff but I can’t seem to be able to connect them with each other. Anyone has an idea how I can fix this?

  • 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-11T20:23:17+00:00Added an answer on June 11, 2026 at 8:23 pm

    If the principal (ObjectA) in a one-to-many relationship has a composite primary key the dependent (ObjectB) must have a foreign key that is composed of the same number of columns as well.

    I assume that the related objects A and objects B must have the same language. In that case you can make ObjectB.Language the first part of the foreign key. (It’s part of the primary and foreign key in ObjectB at the same time.) If you expose the second part of the FK as property in your model, it would look like this:

    (I believe you must swap Id and Language for the following to work.)

    public class ObjectA
    {
        public Languague Languague {get;set;}
        public int Id {get;set;}
        ... // and a list of objectA properties
    
        public virtual ICollection<ObjectB> ObjectBs {get;set;}
    }
    
    public class ObjectB
    {
        public Languague Languague {get;set;}
        public int Id {get;set;}
        ... // and a list of objectB properties
    
        public int TheObjectAId {get;set;}
        public ObjectA TheObjectA {get;set;}
    }
    

    And the mapping with Fluent API:

    modelBuilder.Entity<ObjectA>().HasKey(a => new { a.Languague, a.Id })
    modelBuilder.Entity<ObjectB>().HasKey(b => new { b.Languague, b.Id })
    
    modelBuilder.Entity<ObjectA>()
        .HasMany(a => a.ObjectBs)
        .WithRequired(b => b.ObjectA)
        .HasForeignKey(b => new { b.Language, b.TheObjectAId });
    

    If the languages can be different introduce a separate FK property for the language:

        public Language TheObjectALanguage {get;set;}
        public int TheObjectAId {get;set;}
        public ObjectA TheObjectA {get;set;}
    

    …and map:

    modelBuilder.Entity<ObjectA>()
        .HasMany(a => a.ObjectBs)
        .WithRequired(b => b.ObjectA)
        .HasForeignKey(b => new { b.TheObjectALanguage, b.TheObjectAId });
    

    If the relationship is optional use WithOptional instead of WithRequired and make the FK properties nullable:

    public Language? TheObjectALanguage {get;set;}
    public int? TheObjectAId {get;set;}
    

    I you don’t want to have FK properties in your model you can use MapKey with Fluent API:

    modelBuilder.Entity<ObjectA>()
        .HasMany(a => a.ObjectBs)
        .WithRequired(b => b.ObjectA)
        .Map(m => m.MapKey("TheObjectALanguage, TheObjectAId")); // FK columns
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two vector<MyType*> objects called A and B . The MyType class has
I've got a class called Admin , and it has exactly the same fields
I've got a class called PhoneContact with 3 properties: Title , Description and PhoneNumber
I've got a class called Task. The header file goes as such: class Task
I've got a JPanel class called Board with a static subclass, MouseHanlder, which tracks
Okay so i got this clas called event. It has a property called eventDate
I've got this class, let's call it Refund (because that's what it's called). I
I've got a template class called w32file which works with both wchar_t and char.
ive got this class it has one 2dArray and when im trying to fill
I've got a class called Request . At some point in that class I

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.