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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:17:51+00:00 2026-06-13T01:17:51+00:00

I use EF 5 and Code First. Ive got a main ViewModel that exposes

  • 0

I use EF 5 and Code First.

Ive got a main ViewModel that exposes a Translation Property. A Translation consists out of 1 to n Translation Items. The Translation Property is bound to a Usercontrol. When the Translation is created and Added via _context.Translations.Add(t) in the Viewmodel also all TranslationItems inside the Translation are saved and stored correctly in the DB.

But when i try to Add a new TranslationItem (t.TranslationItems.Add) to a existing Translation it isn’t saved in the DB. context.ObjectStateManager.GetObjectStateEntries(EntityState.Added) also returns no elements so i guess the new Translation Items is still unattached. Since the TranslationItem is added inside the Child Usercontrol, i can’t access the current Datacontext To set the Entity State to Added.

What can i do?

Edit:

public class Translation : BaseEntity, IValidatableObject
{

    private List<TranslationItem> _translations;
         public virtual List<TranslationItem> Translations
    { 
        get{
            return _translations;
        }
        set
        {
            _translations = value;

        }
    }s

In ViewModel:

Translation = new Translation(); or

Translation = repo.GetTranslation(1);

Binding to custom Usercontrol:

<Views:TranslationTextInput   Translations="{Binding Translation}"/>

In code behind of UserControl:

             Trans.Translations.Add(new TranslationItem() { Text = "", Lcid = new CultureInfo("en").LCID });

Save function:

    public void Update(Translation t)
    {
        if (t.Id == 0)
            _context.Units.Add(t);
        _context.SaveChanges();
    }

I create the Context in the Viewmodel Constructor and pass it to the repository class.

  • 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-13T01:17:52+00:00Added an answer on June 13, 2026 at 1:17 am

    The ChangeTracker should automatically grab all it’s children if the Translation is properly attached. However, you could try attaching all of the TranslationItems inside the Translation if you’re sure that the Translation is attached to the context.

    I’m not sure of the DbSet name for TranslationItems, change that to whatever you named it.

    public void Update(Translation t)
    {
        if (t.Id == 0)
            _context.Units.Add(t);
    
        foreach (var ti in t.Translations)
        {
             if (_context.Entry(ti).State == EntityState.Detached)
             {
                 _context.TranslationItems.Attach(ti);
             }
        }
    
        _context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I use Entity Framework code-first. DataBaseName - I want to create a database in
I use EF 4.2 code first in my mvc3 project. miniprofiler works fine (sql
Here's sample model classes, which being use with Entity Framework Code First: public class
First off I use this code to make the navigation bar always stay fixed;
I use this code to determine checkbox width and height: var chk = $('input[type=checkbox]:first');
First the requirements: By management requirements, I can't use open source code. I need
EDIT ive got it almost, having and error message now that it can't find
I am trying to expose the linked class that EF code first automatically creates
got a few problem here and hoping that you could help me out. Let

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.