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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:56:18+00:00 2026-05-23T07:56:18+00:00

This is my first foray in either SQL CE or EF, so I may

  • 0

This is my first foray in either SQL CE or EF, so I may have a lot of misunderstandings. I’ve searched a lot of blog entries but still can’t seem to get this right.

I have an MVC3 web site for registrations for a race we’re running. I have a RaceEvents table, and a Runners table, where each RaceEvent will have many runners registered it for it, i.e., Many-to-One. Here are the POCO’s with extraneous data stripped out:

  public class RaceEvent
{
    [Required]
    public int Id { get; set; }

    public virtual ICollection<Runner> Runners { get; set; }

}

    public class Runner
{
    [Required]
    public int Id { get; set; }
    [Required]
    public int RaceEventId { get; set;}
    [ForeignKey("RaceEventId")]
    public RaceEvent RaceEvent { get; set; }
}

Which, as much as I can figure out, ought to work. As I understand it, it should figure out by convention that RaceEventId is a foreign key to RaceEvents. And if that’s not good enough, I’m telling it with the ForeignKey attribute.

However, it doesn’t seem to be working. Whenever I insert a new runner, it is also inserting a new entry in the RaceEvents table. And when I look at the table diagram in ServerExplorer, it shows two gold keys at the top of the Runners table diagram, one for Id, identified in the properties as a PrimaryKey, and the other for RaceEventId, not identified as a PrimaryKey, but indicated in the properties to be for table Runners, rather than for table RaceEvents. I would expect a gold key for Id, but a silver ForeignKey for RaceEventId.

FWIW, I don’t really care about the ICollection in the RaceEvent, but the blog entries all seemed to imply that it was necessary.

Can anybody help me get this right?

Thanks.

  • 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-23T07:56:19+00:00Added an answer on May 23, 2026 at 7:56 am

    Ok,

    Sorry I did not read your question in enough detail. In our project this is how we would represent what your doing. I looked in SSMS and it is not showing said grey key, but it does not create a race event every time you add a runner. Although you do need to make sure when you create a runner that you set the race event property.

    public class DB : DbContext
    {
        public DB()
            : base("Data Source=(local);Initial Catalog=DB;Integrated Security=True")
        {
        }
    
        public IDbSet<Runner> Runners { get; set; }
        public IDbSet<RaceEvent> RaceEvents { get; set; }
    }
    
    public class RaceEvent
    {
        [Key]
        public int RaceEventID { get; set; }
    
    
    }
    
    public class Runner
    {
        [Key]
        public int RunnerID { get; set; }
    
        [Required]
        public virtual RaceEvent RaceEvent { get; set; }
    
    }
    

    Any question let me know.

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

Sidebar

Related Questions

My first foray into writing jQuery functions. I have this function, but I'm not
I've worked with CakePHP 1.3, but this is my first foray into CakePHP 2.0
I'm not a newb to JavaScript but this is my first foray into Acrobat
problem euler #5 i found the solution but i don't know why this first
Using Dozer to map two objects, I have: /** /* This first class uses
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
This is my first foray into designing two versions of images for retina or
This is my first foray into using an MVC construct (CodeIgniter). I'm hoping someone
This is my first foray into shell scripting so if i'm asking a very
I'm a (near complete) beginner, and this is my first foray into encryption -

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.