none
Help with Entity Framework DB first RRS feed

  • Question

  • I am using EF 5, Database first, VS2012, MVC 4, & TFS

    When I try to add a controller to my project, I get the following error for every entity after I regenerate the model:

    The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type. Entity1 previously found CLR type DB.Models.Entity1, newly found CLR type 'DB.Models.Entity1'

    I have tried deleting and recreating the model multiple times but hasn't helped. Does anyone know how to get rid of this? How do I find the ambiguous match?
    Monday, February 25, 2013 1:09 AM

All replies

  • Hi de2008;

    It sounds like you may have changed the code generation to Default which uses ObjectContext but have not deleted the old .tt files and the classes they may have created. Can you post an image of the solution Explorer windows expanding the model/s.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, February 25, 2013 2:20 AM
  • Hi Fernando,

    I think you are close to the answer. I did start my project using VS2010 and began having problems after moving to 2012. When I right click on the edmx design surface and look at the properties, the Code Generation Strategy property is None. I will try changing this to Default. I have tried deleting the edmx and recreating it but it doesn't seem to matter. The old .tt files appear (in the solution) to be gone. Here is my image, does it show something useful?

    Tuesday, February 26, 2013 2:12 AM
  • Hi de2008;

    You stated that this project was started with VS 2010 and the issue came about from the upgrade from VS 2010 to VS 2012. I suspect the VS 2010 project was using ObjectContext as the Entity Framework context. DbContext was not available until .Net Framework 4.1. 

    Try this first, open the edmx designer and right click on any empty area and select properties from the context menu. Change the Code Generation Strategy property is Default then go to the Solution Explorer window and delete the two nodes in the tree that end with the extension .tt. then re-build the project and if you get errors on compile post errors here.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 26, 2013 4:31 AM
  • I get a bunch like this:

    Error 5 'Lms2012.Models.LmsEntities' does not contain a definition for 'Entry' and no extension method 'Entry' accepting a first argument of type 'Lms2012.Models.LmsEntities' could be found (are you missing a using directive or an assembly reference?) C:\projects\Lms\Lms2012\Lms\Controllers\CurriculumController.cs 82 20 Lms2012

    and a bunch like this:

    Error 12 'System.Data.Objects.ObjectSet<Lms2012.Models.Course>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Data.Objects.ObjectSet<Lms2012.Models.Course>' could be found (are you missing a using directive or an assembly reference?)

    C:\projects\Lms\Lms2012\Lms\Controllers\CourseController.cs 118 40 Lms2012



    • Edited by de2008 Wednesday, February 27, 2013 1:24 AM
    Wednesday, February 27, 2013 1:24 AM
  • Hi de2008;

    From the error message it seems that you wrote some code using the Find method connected to the object collection. This was implemented in the new DbContext. So open the EDMX designer and right click on any empty area and select properties from the context menu. Change the Code Generation Strategy property to None. Now recompile and run if you get any errors as before please post here.

       


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, February 27, 2013 4:25 AM
  • then it complains that it can't find any of my entities.

    Error 24 The type or namespace name 'Prerequisite' could not be found (are you missing a using directive or an assembly reference?) C:\projects\Lms\Lms2012\Lms\ViewModels\CourseIndexData.cs 16 28 Lms2012
    Error 22 The type or namespace name 'Group' could not be found (are you missing a using directive or an assembly reference?) C:\projects\Lms\Lms2012\Lms\ViewModels\StudentGroupsIndexData.cs 13 28 Lms2012

    etc

    Thursday, February 28, 2013 1:17 AM
  • Hi de2008;

    OK it sounds like you have multiple projects in your solution. The projects that reference the Entity Framework entity classes need to add a reference to the Entity Framework project and add a using statement at the top of the code file.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 28, 2013 5:46 AM
  • Fernando, I only have 1 project. I have the edmx in my Models folder. Since we removed the .tt files, don't we somehow need to add them back?
    Thursday, February 28, 2013 12:06 PM
  • Hi de2008;

    To your statement, "Since we removed the .tt files, don't we somehow need to add them back?", they get automatically added back when you change the Code Generation Strategy property to None in the EDMX.

    I do not understand why you are getting this error. I would like if you can to zip up your project and upload your project to your SkyDrive in a public folder or to some other place on the web where I can download it to have a look at it.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 28, 2013 3:50 PM
  • Hi de2008;

    Giving you issue a little more thought I think what may correct the issue is to add a using statement to the top of those code files that are throwing the exceptions. Go to your EDMX designer and right click on an area of the design surface where there no objects and select Properties. In the Properties windows copy the Namespace that it has in the text box to its right. So that the complete statement will become :

    using TheNamespaceYouJustCopied;

    Now all the Entity classes should be found by the code.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 28, 2013 7:50 PM
  • Just to be clear...

    The entities do not get regenerated when I change the Code Generation Strategy. I have to edit web.config, remove the connection string, save it, go to the model folder, add a new ADO.NET Entity Framework Data Model, select the tables I want and then I get the error, "Check Out The item c:\projects\Lms\Lms2012\Lms\Models\LmsModel.Designer.cs could not be found in your workspace, or you do not have permission to access it" (I see the file in TFS, anyways, I click OK) and the .tt files reappear both on disk and in my Solution Explorer and everything compiles. I will try to upload my project in the next few days.
    Saturday, March 2, 2013 1:22 PM
  • My controller files do all include:

    using Lms2012.Models;
    using Lms2012.ViewModels;

    Saturday, March 2, 2013 1:28 PM
  • Just to be clear...

    The entities do not get regenerated when I change the Code Generation Strategy. I have to edit web.config, remove the connection string, save it, go to the model folder, add a new ADO.NET Entity Framework Data Model, select the tables I want and then I get the error, "Check Out The item c:\projects\Lms\Lms2012\Lms\Models\LmsModel.Designer.cs could not be found in your workspace, or you do not have permission to access it" (I see the file in TFS, anyways, I click OK) and the .tt files reappear both on disk and in my Solution Explorer and everything compiles. I will try to upload my project in the next few days.

    Is this because the LmModel.Designer.cs file is still on the file system, but excluded from the project? So it doesn't appear in the solution but is still on disk and write protected because it isn't checked out.

    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Tuesday, March 5, 2013 7:46 PM
    Moderator
  • Glenn: I did try to check it out also, but I still get the errors when I try to add the controller. 

    Fernando: Do you want my project even w/o the DB?

    Friday, March 8, 2013 12:43 AM