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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:25:47+00:00 2026-05-24T08:25:47+00:00

I am using Entity Framework & LINQ to retrieve data. I am having a

  • 0

I am using Entity Framework & LINQ to retrieve data. I am having a problem with the following:

var customer= db.customers.where(c=>c.id==1);
customer.name=santhosh;
customer.city=hyd;

The fields are changing in the database before I call:

db.SaveChanges();

How do I avoid 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-24T08:25:47+00:00Added an answer on May 24, 2026 at 8:25 am

    The fields are changing in the database before I call

    If you mean changing as in changing outside of application, changes in SQL Management Studio for example. Entity Framework cannot detect those changes, so as a result you might get stale objects that was cached by Entity Framework. To prevent receiving cached object and get the up-to-date values from database, use AsNoTracking.

    Try putting AsNoTracking():

    var customer= db.customers.AsNoTracking().where(c=>c.id==1);
    customer.name=santhosh;
    customer.city=hyd;
    db.SaveChanges();
    

    Or if your problem is to detect concurrent updates(unfortunate terminology, it doesn’t apply to UPDATE only) to same row, use rowversion(aka timestamp) field type; then on your .NET code add Timestamp attribute on the property. Example: http://www.ienablemuch.com/2011/07/entity-framework-concurrency-checking.html

    public class Song
    {
        [Key]
        public int SongId { get; set; }
        public string SongName { get; set; }
        public string AlbumName { get; set; }
    
        [Timestamp]
        public virtual byte[] Version { get; set; }
    }
    

    UPDATE (after your comment):

    If you really has no intent to persist your object changes to database. Try detaching the object.

    Try this:

    var customer= db.customers.where(c=>c.id==1);
    db.Entry(customer).State = System.Data.EntityState.Detached; // add this
    customer.name=santhosh;
    customer.city=hyd;    
    db.SaveChanges();
    

    That won’t save your changes on name and city to database.

    If you want something more robust(the above will fail an exception if the object was not yet attached), create a helper:

    private static void Evict(DbContext ctx, Type t, 
        string primaryKeyName, object id)
    {            
        var cachedEnt =
            ctx.ChangeTracker.Entries().Where(x =>   
                ObjectContext.GetObjectType(x.Entity.GetType()) == t)
                .SingleOrDefault(x =>
            {
                Type entType = x.Entity.GetType();
                object value = entType.InvokeMember(primaryKeyName, 
                                    System.Reflection.BindingFlags.GetProperty, 
                                    null, x.Entity, new object[] { });
     
                return value.Equals(id);
            });
     
        if (cachedEnt != null)
            ctx.Entry(cachedEnt.Entity).State = EntityState.Detached;
    }
    

    To use: Evict(yourDbContextHere, typeof(Product), "ProductId", 1);

    http://www.ienablemuch.com/2011/08/entity-frameworks-nhibernate.html

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

Sidebar

Related Questions

I have a problem with the following Linq query using Entity Framework: from o
I have just started Entity Framework & linq and write this query var query
Is there any way to validate using DataAnnotations in WPF & Entity Framework?
I'm using Entity Framework in my project, and I have the problem that, once
I'm using Entity Framework for creation of my Data Access Layer and I want
I am using the Entity Framework, ASP.NET and C#3.5 I borrowed the following code
I am using Entity Framework POCO to generate some self-tracked data objects, and I
So, I am using the Linq entity framework. I have 2 entities: Content and
This question pertains to query optimization using Linq with the Entity Framework. Is there
I'm using Entity Framework 4 along with MSSQL to store and access data on

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.