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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:55:29+00:00 2026-05-26T08:55:29+00:00

I’ve found a pretty nasty bug in EF 4.1 Code First. Assume we have

  • 0

I’ve found a pretty nasty bug in EF 4.1 Code First. Assume we have this piece of code to retrieve an entity from the context and then update it with new values:

public T Update<T>(T toUpdate) where T : class
        {
            System.Data.Objects.ObjectContext objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)_context).ObjectContext;
            System.Data.Objects.ObjectSet<T> set = objectContext.CreateObjectSet<T>();
            IEnumerable<string> keyNames = set.EntitySet.ElementType
                                                        .KeyMembers
                                                        .Select(k => k.Name);
            var type = typeof(T);
            var values = keyNames.Select(c => type.GetProperty(c).GetValue(toUpdate, null)).ToArray();
            var current = _context.Set<T>().Find(values);
            if (current != null)
            {
                _context.Entry(current).CurrentValues.SetValues(toUpdate);
            }
            return current;
        }

Now assume that my entity has a single key property which is a string.

Working scenario: stored entity has key “ABCDE” and my toUpdate entity has same key “ABCDE”: everything works fine.

Bug scenario: stored entity has key “ABCDE” and my toUpdate entity has key “ABCDE ” (notice the space after the last letter).

The two keys are indeed different. But the find method “automagically” trims my key and finds the stored entity anyway. This would be good, if it didn’t break the SetValues method: since the stored key and the new key are different, I get (rightly) the following:

The property ‘Id’ is part of the object’s key information and cannot
be modified.

Because, being different, it tries to update it, and since it’s a key property it cannot be updated, so the whole thing fails and throws.

What I think is that the “Find” method shouldn’t automagically trim the key values (or whatever it is doing internally to make the two different strings appear the same). In the second scenario, the “Find” method should return null.

Now two things: how do I workaround this temporarily, and where can I report this bug, because I couldn’t find an official place to report the bug.

Thanks.

EDIT: Reported the bug here: https://connect.microsoft.com/VisualStudio/feedback/details/696352/ef-code-first-4-1-find-update-bug

  • 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-26T08:55:29+00:00Added an answer on May 26, 2026 at 8:55 am

    But the find method “automagically” trims my key and finds the stored
    entity anyway.

    I dont’ believe that trimming happens. Find uses a SingleOrDefault query internally, in other words: When you call…

    set.Find("ABCDE "); // including the trailing blank
    

    …it uses this LINQ query:

    set.SingleOrDefault(key => key == "ABCDE "); // including the trailing blank
    

    The problem is in the database and the result depends on the sort order, language, settings for capital/small letters, accents, etc. for your string key field in the database (nvarchar(10) for example).

    For example if you use a standard Latin1_General sort order in SQL Server the key “ABCDE” and “ABCDE “ (with trailing blank) are identical, you cannot create two rows which have these values as primary keys. Even “ABCDE” and “abcde” are identical (if you don’t setup to distinguish capital and small letters in SQL Server).

    At the same time this means that also queries for string columns will return all matching rows – matching with respect to the sort order of that column in the database. The query for “ABCDE “ with trailing blank will just return a record with “ABCDE” without the trailing blank.

    Up to this point this is “normal” behaviour for all LINQ to Entities queries which involve strings.

    Now, it seems – as you found – that the ObjectContext doesn’t know about the configured sort order in the database and uses the normal .NET string comparison where a string with and a string without trailing blank are different.

    I don’t know if it’s possible to tell the context to use the same comparison of strings as the database. I have some doubt that this is possible because the .NET world and the relational database world are too different. Some sort orders might be special and only available in the database and not in .NET at all and vice versa perhaps. In addition there are also other databases than SQL Server which must be supported by Entity Framework and those databases might have their own sort order system.

    For your specific case – and perhaps always when you have string keys – a possible fix of your problem would be to set the key property of the entity to update to the key of the object returned from the database:

    toUpdate.Id = current.Id;
    _context.Entry(current).CurrentValues.SetValues(toUpdate);
    

    Or more generally in the context of your code:

    //...
    var current = _context.Set<T>().Find(values);
    if (current != null)
    {
        foreach (var keyName in keyNames)
        {
            var currentValue = type.GetProperty(keyName).GetValue(current, null);
            type.GetProperty(keyName).SetValue(toUpdate, currentValue, null);
        }
        _context.Entry(current).CurrentValues.SetValues(toUpdate);
    }
    

    toUpdate must not be attached to the context to get this working.

    Is this a bug? I don’t know. At least it is a consequence of a mismatch between .NET and relational database world and a good reason to avoid string key columns/properties in the first place.

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6

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.