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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:08:21+00:00 2026-06-08T07:08:21+00:00

I am using Entity Framework code first for a simple database containing multiple choice

  • 0

I am using Entity Framework code first for a simple database containing multiple choice questions. Each question has multiple possible answers and specifies one as the correct answer.

public class Question
{
    public int QuestionId { get; set; }

    [ForeignKey("CorrectAnswer")]
    public int CorrectAnswerId { get; set; }
    public string Text { get; set; }

    public virtual ICollection<Answer> Answers { get; set; }

    public virtual Answer CorrectAnswer { get; set; }
}

public class Answer
{
    public int AnswerId { get; set; }
    [ForeignKey("Question")]
    public int QuestionId { get; set; }
    public string Text { get; set; }

    public virtual Question Question { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Answer>()
        .HasRequired(a => a.Question)
        .WithMany(q => q.Answers)
        .HasForeignKey(a => a.QuestionId)
        .WillCascadeOnDelete(false);
}

This all works fine, but I need to avoid the catch 22 situation where you can’t create a question because it needs a correct answer, but you can’t create an answer because it needs a question. So I want to make the CorrectAnswerId field nullable, allowing you to create the question, then the answers, and then specify the correct answer.

I’ve tried variations on the following, but always get a “conflicting multiplicities” exception:

modelBuilder.Entity<Question>()
            .HasOptional(q => q.CorrectAnswer)
            .WithRequired(a => a.Question)
            .Map(p => p.MapKey("CorrectAnswerId"));
  • 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-08T07:08:22+00:00Added an answer on June 8, 2026 at 7:08 am

    Change

     int CorrectAnswerId {get;set;}
    

    to

    int? CorrectAnswerId {get;set;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone my problem is very simple. Using entity framework code first I want
When using Entity Framework Code First 4.3.1 it is possible to create relationships with
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I'm creating a database using entity framework code first and I'm having some issues
I am developing a rather large database schema using Entity Framework Code First. I
We are using Entity Framework Code First with Foreign Key relationships. We investigating on
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I need to map a many-to-many relationship using Entity Framework Code First. Its a
(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually

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.