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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:06:59+00:00 2026-06-03T01:06:59+00:00

I am working on an application that uses EF 4.2 and database-first development, using

  • 0

I am working on an application that uses EF 4.2 and database-first development, using the standard T4 template to generate a DbContext and POCOs. The T4 templates generate entities something like this:

public class Address
{
    public int AddressId { get;set; }
    public string Address1 { get;set; }
    public string City { get;set; }
}

public class Account
{
    public int AccountId { get;set; }
    public string Name { get;set; }
    public int AddressId { get;set; }
    public Address BillingAddress { get;set; }
}

When I create a billing address for an existing account, my code is something like this:

public void Save(Account updated)
{
    var existing = DbContext.Find(updated.AccountId);

    MyContext.Entry(existing).CurrentValues.SetEntry(updated);
    existing.Address = updated.Address;

    MyContext.SaveChanges();
}

Watching SQL Server Profiler, I can see the Address entry being inserted into the database, but unfortunately, it is occurring after the Account entry is updated, so the address is detached from its parent account, and when I next load the account, the billing address is empty again.

A workaround is to add the following code after the call to SaveChanges():

if (existing.AddressId == null && existing.Address != null)
{
    existing.AddressId = existing.Address.AddressId;
    MyContext.SaveChanges();
}

which, while it may work, requires a second SQL UPDATE to the database, and as the entity grows and adds more associations, requires more and more hacks. Is there something obvious that I’m missing?

** UPDATE **
Following Ladislav’s answer below, I added a call to the following method in the WriteNavigationProperty to my T4 template:

void WriteKeyAttribute(CodeGenerationTools code, NavigationProperty navigationProperty, MetadataTools ef)
{
    var dependentProperties = navigationProperty.GetDependentProperties();
    if (dependentProperties.Any())
    {
        var keys = new List<string>();
        foreach (var key in dependentProperties)
        {
            keys.Add(String.Format("\"{0}\"", key.Name));
        }
#>
    [ForeignKey(<#= String.Join(", ", keys) #>)]
<#+
    }
}

Hope that helps!

  • 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-03T01:07:01+00:00Added an answer on June 3, 2026 at 1:07 am

    It sounds like your BillingAddress is incorrectly mapped because AddressId is not handled as the FK of the relation.

    Try to add this attribute to your navigation property:

    [ForeignKey("AddressId")]
    public Address BillingAddress { get;set; }
    

    If you are using EDMX with database first make sure that the there is correctly configured relation between those classes. EF uses this information in its store mapping and store mapping defines sequence of the operations. If you don’t have correctly configured relation entities are processed in alphabetical order of their type names => Account is processed prior to Address.

    Btw. are you sure that your Account is not duplicated during your SaveChanges call?

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

Sidebar

Related Questions

I'm currently working on a PHP application that uses a MySQL database for its
I am working on a new application that uses a jet (MS Access) database.
I am currently working on a VB.NET desktop application that uses .mdb (Access) database
I am working on a multithreaded application that uses DB2 for its primary database.
I'm working on an AIR application that uses a local SQLite database and was
I am working on a .NET web application that uses an SQL Server database
I'm working on a Windows Mobile 6.5 application that uses a SQL compact database
I am working on an application that uses Oracle's built in authentication mechanisms to
I'm working on an application that uses multiple threads to process its data. The
I am currently working on an application that uses a TableViewer in several places

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.