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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:48:55+00:00 2026-06-10T09:48:55+00:00

I have two models in my ASP.NET MVC project. When seeding to include test

  • 0

I have two models in my ASP.NET MVC project. When seeding to include test data I do the following:

context.Dialogs.Add(new Dialog
{
    Id = 1,
    Chapter = 1,
    Index = 0,
    DialogColor = "default-color",
    DialogText = "blah blah!",
    Character = "none",
    Transition = false,
    Fade = true,
    Timer = 0,
    Actions = new List<Action>
    {
        new Action { ActionText = "--continue--", ActionLink = 1, Id=1 }
    }
});

This saves the record in the Dialog table but the Actions are not saved, I know I can probably save the dialog first and then append an Action to it but I want to be able to add it all inline like above?

Dialog model:

public class Dialog
{
    [Key]
    public int Id { get; set; }
    public int Chapter { get; set; }
    public int Index { get; set; }
    public string DialogColor { get; set; }
    public string DialogText { get; set; }
    public string Character { get; set; }
    public bool Transition { get; set; }
    public bool Fade { get; set; }
    public int Timer { get; set; }

    public virtual IEnumerable<Action> Actions { get; set; }
}

Action Model:

public class Action
{
    [Key]
    public int Id { get; set; }
    public string ActionText { get; set; }
    public int ActionLink { get; set; }

    public Dialog Dialog { get; set; }
}
  • 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-10T09:48:57+00:00Added an answer on June 10, 2026 at 9:48 am

    You need to update your model to use an ICollection so entity framework will make the proper associations. From this post:

    The Entity Framework doesn’t support collection properties that are
    exposed as IEnumerable with Add and Remove methods.

    In your dialog class update your property to:

     public virtual ICollection<Action> Actions { get; set; }
    

    EDIT:

    Couple other things to look for, are the navigation properties mapped?

    You could try adding a fluent mapping:

      protected override void OnModelCreating( DbModelBuilder modelBuilder )
      {
         modelBuilder.Entity<Dialog>()
            .HasMany(d => d.Actions)
            .WithOptional(a => a.Dialog);
    
      }
    

    Is the FK actually getting mapped in the database? Note: The Dialog_Id on the Actions table. If you don’t have this, then EF isn’t actually mapping your relationship

    enter image description here

    If none of this work, you could always explicitly add the entity to your context when creating it in the list:

         context.Dialogs.Add( new Dialog
         {
            Id = 1,
            Chapter = 1,
            Index = 0,
            DialogColor = "default-color",
            DialogText = "blah blah!",
            Character = "none",
            Transition = false,
            Fade = true,
            Timer = 0,
            Actions = new List<Action>
                {
                    context.Actions.Add(new Action()
                                           {
                                              ActionText = "--continue--",
                                              ActionLink = 1, Id=1 
                                           })
                }
         } );
    

    The DbSet.Add method will return the entity once it’s been added to the context.

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

Sidebar

Related Questions

I'm working in an ASP.NET MVC project where I have created a two LinqToSQL
I've got an ASP.NET MVC (VB) project with two models that represent two different
I have a .NET solution containing two projects: An ASP.NET MVC project, 'Website' A
Comparing the two data models in an asp.net MVC app, which provides better performance,
My ASP.NET MVC 2 application uses Entity Framework 4.0 for the data model. Following
I have a ASP.NET MVC 3 view with esentially two forms, but which reside
I'm building an ASP.Net MVC website. Rather than have everything in one project, I've
I have two models: User and Car with the following associations: User has_many Car
So I have a simple voting feature on my asp.net mvc page. My index
I've been trying to use a preexisting database in my ASP.NET MVC project. I

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.