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 6531397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:52:24+00:00 2026-05-25T09:52:24+00:00

Person has two similar tables, difference is that each person has only one record

  • 0

Person has two similar tables, difference is that each person has only one record in PersonPreference table, but can have many in PersonRoles table. Both PersonPreference and PersonRole have the same primary and foreign key (Person_ID):

<Table("Person")> _
Public Class Person
    <Key()> _
    Public Property Person_ID As Integer
    Public Property Name As String

    Public Overridable Property PersonPreference As PersonPreference
    Public Overridable Property PersonRoles As ObservableCollection(Of PersonRole)
End Class

<Table("PersonPreference")> _
Public Class PersonPreference
    <Key()> _
    Public Property Person_ID As Integer
    Public Property Car As String
    Public Property Color As String
End Class

<Table("PersonRole")> _
Public Class PersonRole
    <Key()> _
    Public Property Person_ID As Integer
    Public Property Role As String
End Class

Public Class PersonMap
    Inherits EntityTypeConfiguration(Of Person)

    Public Sub New()
        Me.HasRequired(Function(t) t.PersonPreference).WithRequiredPrincipal()
        Me.HasRequired(Function(t) t.PersonRoles).WithRequiredPrincipal()
    End Sub

End Class

Is there a way of saving the person roles as it is the code (without adding the Roles table) or there need to be modifications (primary key on PersonRoles table – I have tried making composite key in PersonRoles table, but that did not work)?

  • 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-05-25T09:52:25+00:00Added an answer on May 25, 2026 at 9:52 am

    It is not necessary to have a Role table but you need to have a composite key in PersonRole table. I not familiar with VB.net so hope you can convert this into VB.net.

    public class Person
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    
        public virtual ICollection<PersonRole> PersonRoles { get; set; }
    
        //other properties
    }
    
    public class PersonContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Person>().HasMany(p => p.PersonRoles).
              WithRequired().HasForeignKey(r => r.PersonId);
    
            modelBuilder.Entity<PersonRole>().HasKey(r => new {r.PersonId, r.Role});
        }
    
        public DbSet<Person> Persons { get; set; }
    }
    

    Here is an example

    var ctx = new PersonContext();
    
    var person = new Person {Name = "Foo"};
    person.PersonRoles = new List<PersonRole>() 
          { new PersonRole { Role = "a" }, new PersonRole { Role = "b" } };
    
    ctx.Persons.Add(person);
    
    ctx.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As an example: Say there are two tables: Person, and Photo. Each Photo has
I have two tables: Person and Shift Person has as lookup field which is
I have two simple tables as described here... Table = Person PersonID (int, PrimaryKey)
I have the following code which has two fields that are queried. How can
I've got one class that has two text fields in it: name and surname.
I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |
I'm attempting to create a contacts application that has two main entities - person
I have a Rails simple application that has two main models. A person model
Obfuscated Scenario : A person has zero, one or many pets. Using Linq to
Am I the only person who has the need to view multiple directories at

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.