locked
EF4.1 CodeFirst - entry.GetDatabaseValues() throw EntitySqlException RRS feed

  • Question

  • Hello,

    Using entry.GetDatabaseValues() throw EntitySqlException when namespace of entity is different than namespace of context class. Exception message is:

    Type 'XXX' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly.

    Could you give me some advices how to avoid this exception without changing namespace on my entity classes?

    Best regards :)!!

     


    PS
    Wednesday, July 6, 2011 10:01 AM

All replies

  • Thank you for posting.

    First of all, could you provide more detailed information about how did you import namespace in your project?

    From your provided infromation, I suggest you can check this post and check if it's helpful or not. http://www.codeproject.com/Questions/208209/Problem-with-Entity-SQL?display=Print

    Hope this helps. If you have any question, please feel free to let us know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 8, 2011 8:03 AM
  • Hello, Thanks for your reply. I have two projects, one of them contains DbContext and the second one contains entity objects. That's all. If I place my entity objects in project with DbContext and change its namespaces then function GetDababaseValues() work correctly. Is there any possibility to tell DbContext that should search entity classes in other namespace? Sorry for my english, I know is very poor. Best regards.
    PS
    Friday, July 8, 2011 2:29 PM
  • Hello again,

    DbContext can access other entities located in other assemblies, but you should check if these entities are relationships or not. I suggest you provide more detailed information about the relationships with these entities.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Larcolais Gong Monday, July 11, 2011 3:11 PM entity relationship
    Monday, July 11, 2011 10:53 AM
  • Hello,

    GetDatabaseValues() is method provided by Entity Framework 4.1:

    http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.getdatabasevalues%28v=vs.103%29.aspx

    http://blogs.msdn.com/b/adonet/archive/2011/02/03/using-dbcontext-in-ef-feature-ctp5-part-9-optimistic-concurrency-patterns.aspx

    Best regards,
    Pawels


    PS

    Monday, July 11, 2011 2:23 PM
  •  

    Hi!

    I got the same Exception as Pawels did.

    My DbContext has namespace called EFSample, while the entity POCO class' namespace is EFSample.Entites.

    Here my code snippet:

                    try
    
                    {
    
                        context.SaveChanges();
    
                    }
    
                    catch (DbUpdateConcurrencyException ex)
    
                    {
    
                        var entry = ex.Entries.Single();
    
                        var dbValues = entry.GetDatabaseValues(); // the exception is thrown here
    
                        entry.OriginalValues.SetValues(dbValues);
    
                    }

    And the exception is:

    {"Type 'EFSample.Supplier' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly. Near type name, line 1, column 1282."}

     

    Regards,

    Attila



    Monday, July 11, 2011 3:01 PM
  • Hello Pawels,

    Sorry for my mistake.

    From your original post, it looks that you may use a wrong namespace in your project. In addition, my assumption is you used DbContext with a different namespace to the namespace model, I suggest you can try to use the same namespace in your project and try it again.

    Plus, here's a post like yours. I suggest you can check it and  combine with your actual condition. http://forums.oreilly.com/topic/49735-entity-sql-problem/

    Please feel free to let me know if you need any further support.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 12, 2011 8:29 AM
  • If I understand correctly, are we bound to one single namespace? All our entities AND the DbContext MUST be in the same namespace for GetDatabaseValues() to work? Will this change in the (near) future? Because this is a very severe limitation of the EF.

    Regards, Attila

    Tuesday, July 12, 2011 10:32 AM
  • Hello,

    When I use the same namespace it works correctly. But is not that I need. I want to have my POCO clsses isolated on data access infrastructure. I use some workaround to solve this problem (wrote my own GetDatabaseValues() method), but is only workaronund. As Atilla wrote - this is a big limitation of the EF. Hope it will be solved in next RTM version.

    Best Regards,

    Pawels


    PS
    Tuesday, July 12, 2011 12:33 PM
  • Hello again,

    Based on my understanding, if you just generate a DLL with a single separate and no relationship POCO class, you can just import that namespace in your project and use it. For your case, could you please tell me if these entities have some relationships each other or not.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 13, 2011 4:12 AM
  • Hello,

    My entities have relationships, but even if I removed this relationships I got exception when I try to use GetDatabaseValues() method. Many thanks for your trying to help. Maybe it is simply the bug, and in current release we can't resolve that issue.

     

    Best Regards,

    Pawels


    PS
    Wednesday, July 13, 2011 5:47 AM
  • Hello Pawels,

    I think you maybe post a simple demo to us to analyze your question more detail. v-xugong@microsoft.com

    Please feel free to let us know if you have any findings.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 8:33 AM
  • If someone interested in this, here is my workaround for handling the optimistic concurrency exception without the use of GetDatabaseValues()

     

      var currentValues = entry.CurrentValues.Clone();

      entry.Reload();

      entry.CurrentValues.SetValues(currentValues);

     

     

    Regards,

    Attila


    Thursday, July 14, 2011 2:09 PM
  • if it makes you guys feel any better, I'm having this problem too. :)  I was successfully using GetDatabaseValues with the RC of EF 4.1 when I wrote an MSDN online article about using the ChangeTracker API.

    My domain classes are in a separate namespace from the context class also.

    I've asked some folks on the team directly and will let you know what I find out.

    Julie Lerman


    Julie Lerman, Author of Programming Entity Framework, MVP
    Thursday, July 14, 2011 5:21 PM
  • If Julie got interested in this problem, I think we could sleep calm becouse it will be solved

    Best regards,

    Pawels


    PS
    Friday, July 15, 2011 9:00 AM
  • ha! Well I can't solve it myself. I'll just pester the heck out of them. ;)
    Julie Lerman, Author of Programming Entity Framework, MVP
    Friday, July 15, 2011 8:11 PM
  • response (that I got on the same day July 15, but forgot to come back here with an update) was that it looks like it might be a bug...

    So they are aware of it at least...

    Sorry not to be able to provide more. 

    Julie


    Julie Lerman, Author of Programming Entity Framework, MVP
    • Proposed as answer by jrummell Thursday, October 6, 2011 2:17 PM
    Sunday, July 31, 2011 2:32 AM
  • Thanks Julie :). At least we know that is a bug.

     

    Regards,

    Pawels


    PS
    Sunday, July 31, 2011 6:50 PM
  • Attila,

    Unfortunately, when the Entry object comes from the DbUpdateCurrencyException, this workaround fails with the following message:

    System.InvalidOperationException was unhandled
    Message=CurrentValues cannot be used for entities in the Deleted state.

     

                       catch (DbUpdateConcurrencyException ex)
                       {
                           saveFailed = true;
                           DbEntityEntry entry = ex.Entries.Single();
                           DbPropertyValues currentValues = entry.CurrentValues.Clone();
                           context.Entry(entry.Entity).Reload();
                           entry.CurrentValues.SetValues(currentValues);
    
    
                           //var entry = ex.Entries.Single();
                           //entry.OriginalValues.SetValues(entry.GetDatabaseValues());
                       }
    

    This error was thrown when the following code was called:

    DbPropertyValues currentValues = entry.CurrentValues.Clone();

    Apparently, CurrentValues are deleted from the entry when a DbUpdateCurrencyException is thrown.

    Tuesday, September 27, 2011 4:31 PM
  • The workaround at http://thefrozencoder.ca/post/2011/09/11/Type-ContosoUniversity-DAL-Department-could-not-be-found.aspx worked for me.

     

            public int SaveChanges()
            {
                // Resolve optimistic concurrency exceptions as client wins
                // http://blogs.msdn.com/b/adonet/archive/2011/02/03/using-dbcontext-in-ef-feature-ctp5-part-9-optimistic-concurrency-patterns.aspx
                int changes = 0;
                bool saveFailed;
                do
                {
                    try
                    {
                        changes = Repository.SaveChanges();
                        saveFailed = false;
                    }
                    catch (DbUpdateConcurrencyException exception)
                    {
                        saveFailed = true;
    
                        // reload database values and try again
                        foreach (DbEntityEntry entry in exception.Entries)
                        {
                            // entry.OriginalValues.SetValues(entry.GetDatabaseValues());
    
                            // workaround
                            var currentValues = entry.CurrentValues.Clone();
                            entry.Reload();
                            entry.CurrentValues.SetValues(currentValues);
                        }
                    }
                } while (saveFailed);
    
                return changes;
            }
    


     


    • Edited by jrummell Thursday, October 6, 2011 2:32 PM
    Thursday, October 6, 2011 2:31 PM
  • Yes, the configuration in this tutorial gives the DBContext the same namespace as the Models.  That approach works.  Failure occurs when they are in difference namespaces. 
    Tuesday, October 18, 2011 1:08 PM
  • This is a known bug in EF 4.1 and is documented at the link below.  I suggest you (and others) go to this page and let microsoft know that you too can reproduce this (there is a link on the page) so they can prioritize.

    https://connect.microsoft.com/data/feedback/details/694281/dbentityentry-getdatabasevalues-method-crashes-when-db-context-has-different-namespace-than-the-data-objects 


    -Aaron King
    Wednesday, November 16, 2011 4:40 PM
  • Change the namespace property in the EDMX properties window, to the namespace used by your entities.
    Thursday, December 8, 2011 4:00 PM
  • There is no EDMX in code first.
    Saturday, December 10, 2011 5:28 PM
  • I had the same issue...

    But in my case somehow the setting for Code Generation Strategy (one of the properties on the Model) had been set to "NONE" by default instead of "Default"... once I had changed the setting everything started to work.

    Sunday, October 28, 2012 10:39 PM