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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:00:29+00:00 2026-06-10T06:00:29+00:00

I have this generated entity: public partial class Player { public int Id {

  • 0

I have this generated entity:

    public partial class Player
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public System.DateTime Birthdate { get; set; }
        public PlayerPosition Position { get; set; }
        public int IdTeam { get; set; }

        public virtual Team Team { get; set; }
    }

I want to make a method to update the position of a player.

I am doing this:

        Player playerToUpdate = new Player
        {
            Id = 34,
            Position=PlayerPosition.Defender
        };
        playersRepository.Attach(playerToUpdate);
        playersRepository.UpdatePosition(playerToUpdate);

        public void Attach(T entity)
        {
            DbSet.Attach(entity);
        }

        public void UpdatePosition(Player playerToUpdate)
        {
            Context.Entry(playerToUpdate).Property(p => p.Position).IsModified = true;
        }

I get a validation exception (The name field is required)

What is the way to fix it?

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-10T06:00:31+00:00Added an answer on June 10, 2026 at 6:00 am

    Why aren’t you first loading the existing player, updating the position, and then saving back??

    It’s an existing player – right? You obviously also have the player’s ID …

    Something like:

    Player existingPlayer = playersRepository.GetByID(34);
    
    existingPlayer.Position = PlayerPosition.Defender;
    playersRepository.Save(existingPlayer);
    

    And of course you can wrap this into a method on the playersRepository of your own:

    public void UpdatePosition(int playerID, PlayerPosition newPosition)
    {
        Player existingPlayer = playersRepository.GetByID(playerID);
    
        existingPlayer.Position = newPosition;
        this.Save(existingPlayer);  // assuming you have a Save method on the repository
    }
    

    and then just call that:

    playersRepository.UpdatePosition(34, PlayerPosition.Defender);
    

    Entity Framework is smart enough to figure out that only the Position on that player has changed, so it will generate SQL something along the lines of:

    UPDATE dbo.Player
    SET Postion = 'Defender' 
    WHERE PlayerID = 34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple entity: public class Something{ [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int ID {
I have 2 Hibernate classes in a Spring-driven Application like these: @Entity public class
Assume I have the following two classes: public class User : Entity { public
I have an Entity Framework POCO class that is generated by a T4 Template.
The default constructor in a generated Entity Framework Entities file is like this: public
I have entity class generated by E-R designer that I have modified a little.
I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
I have two checkboxes generated like this: <table> <?php foreach ($invited_members as $mem) :
I have an insert query that gets generated like this INSERT INTO InvoiceDetail (LegacyId,InvoiceId,DetailTypeId,Fee,FeeTax,Investigatorid,SalespersonId,CreateDate,CreatedById,IsChargeBack,Expense,RepoAgentId,PayeeName,ExpensePaymentId,AdjustDetailId)
I have a simple app with a full screen UIWebView. This contains HTML generated

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.