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

The Archive Base Latest Questions

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

First of all, I’m new to the EF-code first and .NET in general. Im

  • 0

First of all, I’m new to the EF-code first and .NET in general.

Im working on a mobile web app thats being built with MVC4, EF, JQuery, AutoMapper & other technologies and libraries.

This might be confusing, but I will do my best to explain it right.

I have the following pocos:

  public class Test
{
    [Key]
    public int TestId { get; set; }
    .
    .
    Other properties
    .
    .
    public virtual ICollection<Question> Questions { get; set; }
    public virtual ICollection<UserStatus> UserStatuses { get; set; }
}

public class UserStatus
{
    [Key]
    public int UserStatusId { get; set; }
    public int TestId { get; set; }
    public string Customer { get; set; }
    .
    .
    Other properties
    .
    .
}

public class Question
{
    [Key]
    public int QuestionId { get; set; }
    public int TestId { get; set; }
    .
    .
    Other properties
    .
    .
}

and in my context class i have:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Database.SetInitializer<CmeContext>(null);

    modelBuilder.Entity<Test>()
        .HasKey(a => new { a.TestId })
        .HasMany(s => s.Questions);
    modelBuilder.Entity<Test>()
        .HasKey(a => new { a.TestId })
        .HasMany(s => s.UserStatuses);          
}

The above Test object has a one-to-many relationship to Question and UserStatus objects through TestId. Each Test has it’s own set of questions which are 4 questions per test, and 20 userStatuses 5 per question).
As the name implies the UserStatus table has user statuses; it let me know which question(s) from what test was answered by a user.

The code in the controller:

var filtered = (from test in _ctxCmeContext.Test.Include("UserStatus").Include("Question")
                where test.Program == "SomeProgram" && test.IssueDate.Value.Year == year && test.IssueDate.Value.Month == monthNumber 
                orderby test.IssueDate descending 
                select new { test,
                                                 Questions = test.Questions, 
                         UserStatuses =     test.UserStatuses.Where(x => x.Customer == currentUserId) });

As you see I have an anonymous class so I can get the UserStatuses collection filtered by currentUserId to get the statuses for the current user only. Everything works fine at this point.

Here’s what I need to do:

Each question needs to know its own status, so it’s a one-to-one relationship between the Question and UserStatus table

In my context class I added the following:

      modelBuilder.Entity<Question>()
     .HasRequired(x => x.UStatus)
     .WithOptional();

and in the Question Object I had:

                        public virtual UserStatus UStatus { get; set; }

and I also modified my linq statement as follows:

          var filtered = (from test in         _ctxCmeContext.Test.Include("UserStatus").Include("Question").Include("UStatus")
                where test.Program == "SomeProgram" &&   test.IssueDate.Value.Year == year && test.IssueDate.Value.Month == monthNumber 
                orderby test.IssueDate descending 
                select new { test, 
                             QuestionStatus =    test.Questions.Select(u=>u.UStatus), 
                             Questions = test.Questions, 
                             UserStatuses = test.UserStatuses.Where(x => x.Customer == currentUserId) }); 

The result:

the UStatus property had data for each question but it wasn’t for the current user nor for the right test; it seemed to me that it just grabbed the first record it ecountered that had the same QuestionId from the Question table. No good.

so I changed the relationship to one-to-many:

In my context class I replaced:

    modelBuilder.Entity<Question>()
       .HasRequired(x => x.UStatus)
       .WithOptional();

with:

        modelBuilder.Entity<Question>()
           .HasKey(a => new { a.QuestionId })
           .HasMany(s => s.UStatus);

changed the UStatus property in the Question Object to:

   public virtual ICollection<UserStatus> UStatus { get; set; }

and removed the following from the linq statement

   QuestionStatus = test.Questions.Select(u=>u.UStatus), 

This time I was able to get some correct data but only for the first 4 questions, and the rest were null

I don’t know what to do anymore. Help me please.

  • 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-18T11:03:44+00:00Added an answer on June 18, 2026 at 11:03 am

    In the Question object I created a navigation property back to the Test object, and from there I was able to know the status of each question because the UserStatus is a child collection in the Test object.

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

Sidebar

Related Questions

First of all, let me show you a bit of code: index.php <a href=new-directory-dialog.php
first of all some details: I configured security as below in web.xml view plaincopy
First of all, I'm quite new to the Android and JAVA world (coming from
First of all I want to mention two things, One: My code isn't perfect
First of all, this code , as ugly as it is, works, for all
i have this code which i use in order to extract first all users
I have this code below, with which I select first all id's from table
First of all, pardon my hacky code, I'm just trying to try this out
when i built my first all html/css website i used clickable images as a
First of all, this relates to a desktop application rather than an ASP .Net

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.