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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:04:09+00:00 2026-06-11T08:04:09+00:00

I have a Quest and QuestTemplate class like below public class Quest{ public int

  • 0

I have a Quest and QuestTemplate class like below

public class Quest{

    public int ID {get;set;}

    public virtual QuestTemplate QuestTemplate {get;set;}

    public string Name{get;set;}
}

public class QuestTemplate{

    public int ID {get;set;}

    public QuestTemplate QuestTemplate {get;set;}

    public string Name{get;set;}

    public virtual Quest Quest{get;set;}
}

I create Quest and QuestTemplate like below in runtime

Quest q = new Quest();
q.Name = "Foo";
q.QuestTemplate = new QuestTemplate();
q.QuestTemplate.Name = "Bar";

context.Quests.Add(q);
context.SaveChanges();

It adds quest object and quest template object into related tables (Quests, QuestTemplates in this example). However the problem is that the Quest_ID column of quest template object in table is not set with quest object’s ID. Instead, it remains null. How can I resolve this problem?

P.S: I’m using EF5 with code first.

Best regards,

Kemal

  • 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-11T08:04:10+00:00Added an answer on June 11, 2026 at 8:04 am

    Nowhere in the code did you specify the QuestTemplate applies to the Quest object. The following code should work:

    Quest q = new Quest();
    q.Name = "Foo";
    
    QuestTemplate qt = new QuestTemplate();
    qt.QuestTemplate.Name = "Bar";
    qt.Quest = q;
    
    q.QuestTemplate = qt;
    
    context.Quests.Add(q);
    context.SaveChanges();
    

    Or you can map a one-to-one relationship.

    modelBuilder.Entity<Quest >()
        .HasRequred(q => q.QuestTemplate)
        .WithRequiredPrincipal()
        .WillCascadeOnDelete();
    

    Edit

    Looked at this tonight and the reason for the relation isn’t mapping is in regards to the FK name. If you specify the FK in the fluent mapping, the relationship works as expected. Also, this type of 1-to-1 relationship requires either Quest or QuestTemplate be loaded first before the other (since it’s a required field) I randomly chose QuestTemplate below:

    public class DataContext : DbContext
    {
        public DbSet<Quest> Quests { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Quest>()
                .HasOptional(q => q.QuestTemplate)
                .WithRequired(qt => qt.Quest)
                .Map(m => m.MapKey("QuestId"));
        }
    }
    
    public class Quest
    {
        public int QuestId { get; set; }
        public string Name { get; set; }
        public virtual QuestTemplate QuestTemplate { get; set; }
    }
    
    public class QuestTemplate
    {
        public int QuestTemplateId { get; set; }
        public string Name { get; set; }
        public virtual Quest Quest { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            using (DataContext db = new DataContext())
            {
                Quest q = new Quest { Name = "Bar" };
                q.QuestTemplate = new QuestTemplate { Name = "Foo" };
                db.Quests.Add(q);
                db.SaveChanges();
            }
        }
    }
    

    I believe it has something to do with the section titled Foreign Keys here, but I’m still not clear on why this is. I have other relationships configured exactly the same way without identifying the FK.

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

Sidebar

Related Questions

I have 2 classes like below. public class Quest { public int ID{ get;set;
I have 4 string variables name, quest, favorite_color, speed that might be empty. I
I have an entity Quest which contains many Task s: @Entity public class Quest
Sql Server and Oracle have commercial products like Idera (Sql Safe) and Quest (LiteSpeed)
I have two tables. In one I have column like INT and in other
I have a form that has many long-methods. My quest is: What is the
In my quest for a version-wide database filter for an application, I have written
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
In my quest for my first iPhone app I have posted about the correct

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.