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

  • Home
  • SEARCH
  • 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 8593871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:05:07+00:00 2026-06-12T00:05:07+00:00

I am looking for a more better way to update a record using Entity

  • 0

I am looking for a more better way to update a record using Entity Framework.

Below is what I am using now, where playerFromModel is data fetched from View.

public bool UpdatePlayer(PlayerEntity playerFromModel)
{
    bool updateSuccessfull = false;

    using (var context = new PlayerEntities())
    {
        var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();

        if (player != null)
        {
            player.emailAddress = playerFromModel.EmailAddress;
            player.first_name = playerFromModel.FirstName;
            player.last_name = playerFromModel.LastName;
            player.alt_email_id1 = playerFromModel.AlternateEmailAddress;
            player.street = playerFromModel.Street;
            player.city = playerFromModel.City;
            player.state = playerFromModel.State;
            player.zip = playerFromModel.Zip;
            player.country_code = playerFromModel.CountryCode;
            player.phone1 = playerFromModel.Phone;

            try
            {
                context.SaveChanges();
                updateSuccessfull = true;
            }
            catch
            {
                updateSuccessfull = false;
            }
        }
    }

    return updateSuccessfull;
}

As you can see I have to manually map each of the fields and the same approach I have followed everywhere.

I was thinking that there definitely has to be a better way to do this.

Please guide me on this. Thanks.

  • 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-12T00:05:08+00:00Added an answer on June 12, 2026 at 12:05 am

    You can just use TryUpdateModel. e.g.

        bool updateSuccessfull = false;
        using (var context = new PlayerEntities())
        {
            var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();
    
            if (player != null)
            {
                 //Beware this will try to map anything it can. This can be dangerous
                if(TryUpdateModel(player)){
                try
                {
                    context.SaveChanges();
                    updateSuccessfull = true;
                }
                catch
                {
                    updateSuccessfull = false;
                }
             }
            }
        }
        return updateSuccessfull;
    }
    

    I wouldn’t though see here Real example of TryUpdateModel, ASP .NET MVC 3.

    You could use Automapper or something similar as MiBu said. Create a ViewModel link is here of the Player and then map from that to the entity

    //Get a PlayerUpdate ViewModel     
    
           using (var context = new PlayerEntities())
        {
       // fetch the domain model that we want to update - BTW I'd use a repository pattern but that is another debate
            var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault();
    
            if (player != null)
            {
    
            // Use AutoMapper to update only the properties of this domain model
            // that are also part of the view model and leave the other properties unchanged
            AutoMapper.Map<UpdatePlayerViewModel , PlayerEntity>(viewModel, player);
    
               try
                {
                    context.SaveChanges();
                    updateSuccessfull = true;
                }
                catch
                {
                    updateSuccessfull = false;
                }
        }
    

    However I am of the opinion that updating entity properties is a significant thing and shouldn’t be done automatically. Others feel the same. Jimmy Bogard who created AutoMapper doesn’t seem to believe it needs two way mapping (However he says that is partly as they built AutoMapper for their requirement) Similar answer here on Stackoverflow saying do it yourself.

    It depends on the complexity of your application but I would look at using the command pattern to send a message to an appropriate handler containing your properties and have the handler update them. If it was successful we do one thing if it wasn’t we do another. Same pattern is described in MVC in Action. See similar (here is the link)[http://www.paulstovell.com/clean-aspnet-mvc-controllers%5D and here is the second link

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

Sidebar

Related Questions

I am looking for a more versatile way to get from a data.frame to
I am looking for a more efficient way to do comparisons between all elements
I am looking for a more jquery/cleaner way to be able to select all
I'm looking for ideas on the best way to refactor this scenario (better design,
I'm looking for a more efficient way to reprioritize items in a priority queue.
I am looking for a way to resize terminal when using telnetlib . I
I've been looking for a way to move a bunch of tables, more than
I am looking for more advanced alternatives to xsd.exe . I am just about
I am making my first text field, and I am looking for more of
I've found a similar question here , but I'm looking for more general solutions.

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.