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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:58:57+00:00 2026-05-26T12:58:57+00:00

My business has a method which creates a MailMessage from an email. The method

  • 0

My business has a method which creates a MailMessage from an email. The method I use gets an Email object as a parameter, which is a simple POCO object, the foreign key properties like ToId and FromId are already set on it. The entity also have navigation properties to EmailAddress entities (FromEmailAddress and ToEmailAddress).

What I want to achive is to use these navigation properties. The way I was able to this is the following, but it looks like a hax:

    public MailMessage CreateEmail(Email email)
    {
        var tmpEmail = db.Set<Email>().Create();
        db.Emails.Add(tmpEmail);
        db.Entry<Email>(tmpEmail).CurrentValues.SetValues(email);
        db.SaveChanges(); 
        email = tmpEmail;

And then I use the email in my code. This way the entity has a proxy now so I am able to use navigation properties. Are there any simpler way to do this?

  • 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-05-26T12:58:57+00:00Added an answer on May 26, 2026 at 12:58 pm

    It’s a good solution in my opinion to enable lazy loading. An alternative would be to load the navigation properties explicitely. Then you don’t need to create a proxy:

    public MailMessage CreateEmail(Email email)
    {
        db.Emails.Add(email);
        db.SaveChanges();
    
        db.Entry(email).Reference(e => e.FromEmailAddress).Load();
        db.Entry(email).Reference(e => e.ToEmailAddress).Load();
    
        //...
    }
    

    It creates two roundtrips to the database – the same when you use lazy loading and access the navigation properties.

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

Sidebar

Related Questions

A lot of the time I will have a Business object that has a
We have redesigned the structure to a website which has several business units. Now
Has anybody gone through the process of converting a real-world business application from ASP.NET
My company has recently became a Microsoft Certified Partner and from a business perspective
A new requirement has come down from the top: implement 'proprietary business tech' with
My business has a .NET web service (not WCF) hosted in IIS on a
Our business currently has an online store and recently we've been offering free specials
I am trying to help a small business that has an application that could
I am creating a business application that has a variable width sidebar and content
I have two projects in my Solution. One implements my business logic and has

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.