none
Entity Framework POCOs Database First RRS feed

  • Question

  • I am new to Entity Framework, and I am trying to work with POCOs. It seems that most of the tutorials on POCOs also involve Code First, something that is not applicable to me, because I already have a DB. I’d be grateful for any pointers. 

    My problem is that I get an InvalidOperationException.“Mapping and metadata information could not be found for EntityType 'EFFTrial.LocalAccess.Product_Listing.”

    I basically got a EDMX Model to connect to a Local DB, and it worked. Next, I copied the Generated Code from the *Model.Designer.cs to another *.cs file in the Project. I then set the Code Generation Strategy to None. I then create a Context Class as below.

    public class LocalDB : ObjectContext
        {
            public const string ConnectionString = "name=LocalEntities";
            public const string ContainerName = "LocalEntities";
    
            public ObjectSet<Product_Listing> OpenList;
    
            public LocalDB() :  base(ConnectionString, ContainerName) 
            {
                OpenList = CreateObjectSet<Product_Listing>(); //InvalidOperation!!
            }
        }


    I get the above exception when it hits the constructor CreateObjectSet.

    I’m sorry I have no clue what I am doing wrong. I’d appreciate any pointers to debug this.

    Thursday, March 6, 2014 10:25 PM

Answers

  • You are confused about what POCO means, at least in the context of EF. That is "Plain Old C# Objects", also known as "Persistence-Ignorant" classes.  They are just plain .NET types without a special superclass or any database stuff embedded in the class definitions. 

    Whether you are using POCOs and whether you are using generated code are unrelated.

    You can have generated POCOs, or hand-written POCOs. You can even have hand-written POCOs with an existing database, although few people do this since you would have to modify your data model in two places.

    This link I posted uses the "5.x DbContext Generator for C#" to generate your .NET types, and it generates POCOs. 

    What you may be looking for is the workflow called "Code First to and Existing Database", which is a bit of a misnomer since it's really a database-first workflow. 

    Data Developer Center > Learn > Entity Framework > Get Started > Code First to an Existing Database

    This will generate C# code exactly as if you'd hand-written it and used it in real Code-First workflow.  The mapping metadata will be implemented using the Fluent API.  After you generate your POCOs using this workflow you can choose between regenerating them after each database change or manually making changes to your POCOs and mapping metadata when the database design changes.

    David


    David http://blogs.msdn.com/b/dbrowne/


    Tuesday, March 11, 2014 11:10 PM

All replies

  • Sounds like you downloaded a pre-made demo project.  If I were you I'd start over with a new Mapping using the Wizard.  First delete the EDMX stuff.  Then create a new one, and use the Wizard to point you to the DB and tables you want, then reimport and try again.  Also it looks like you are using a much older version of EF because they now use DB Context types.  So make sure you have latest EF loaded and start from scratch.

    https://www.youtube.com/results?search_query=entity%20framework%20database%20first&sm=3


    JP Cowboy Coders Unite!

    Thursday, March 6, 2014 10:42 PM
  • Thank you Mr. Javaman II.

    In my original post, I failed to mention that I am using Visual Studio 2010, and running .NET 4.0. So in this sense I am a bit old. Unfortunately my organization has not yet moved to the latest version of VS and .NET. Would this be a problem?

    By the way, I used Object Context because my generated code used it. I thought the easiest way to get POCOs to work with Database First, was to copy the code. But I think I am missing something here.

    No this is not a Demo project. But I should also mention that I did not go to the Mapping Wizard. I first added an Empty Model, and then I updated it with a single table. Would this change the behavior?

    My project has four files: The Model, the Copied Generated Code, the Context (code shown in the original post) and finally, the main Program to instantiate the Context. The Model has a single table, nothing else. This is not too complicated, so I have no clue what I am doing wrong.

    I cannot see the text in most of those videos. My problem is that I am looking for Database First, but ALSO with POCOs. Generally the POCOs Tutorials/Videos are related to Code First, NOT Database First

    Thursday, March 6, 2014 11:08 PM
  • But I should also mention that I did not go to the Mapping Wizard. I first added an Empty Model, and then I updated it with a single table. Would this change the behavior?

    Don't do that because it will force you to manually do all the mapping.  Delete the entire EDMX stuff and let the wizard do it all for you!  That way all the mapping is done automatically.


    JP Cowboy Coders Unite!

    Friday, March 7, 2014 4:34 AM
  • Thank you Mr. Javaman II.

                Unfortunately, this did not work. As you suggested, I deleted the model. (I also had to delete the connection string from the App.config.)  Next, I recreated the Model with Generate from Database, instead of the Empty Model I was using before. This complied and ran.

                Then as before I copied the generated code to a separate file. I set the Code Generation Stratergy of the Model to None. And finally as in my original post, I attempted to use the context class mentioned in my original post and I get the same exception as before. So this does not change my situation.

    I’d appreciate any ideas. 

    Friday, March 7, 2014 3:43 PM
  • Then as before I copied the generated code to a separate file. I set the Code Generation Stratergy of the Model to None. And finally as in my original post, I attempted to use the context class mentioned in my original post and I get the same exception as before. So this does not change my situation.

    I don't understand why you are copying the generated code. I personally have never done this in the 4 years I've been using EF and have never had an issue with EF.  Please explain...

    And can you explain why you feel you must set CODE GENERATION STATEGY to none?  Again I've never mucked nor had the need to change how the T4 templates... Actually I did try it once when EF 4.0 came out and changed the template to only use DBContext, I tried to get it to bring back ObjectContext but failed.  I had to retool everything in project to DBContext (pain)...

    I suspect it's this step (above) that's fouling you up.


    JP Cowboy Coders Unite!

    Friday, March 7, 2014 10:09 PM
  • I don't understand why you are copying the generated code. I personally have never done this in the 4 years I've been using EF and have never had an issue with EF.  Please explain...


    Thank you Mr. Javaman II.

                As explained in the Title and my original post, my objective is to see if I can get POCOs to work with an Existing Database. I have attempted this, and I could not get it to work. In order to post it to these forums, I decided to create the SIMPLEST example I could that still had the problems I was facing, and it is what I presented in my original Post i.e.

                I created a simple Model containing a single table with Generated Code. I copied the Generated Code to a separate file, so that we can pretend that these are my POCOs. I then tried the context presented in my original post and I cannot get it to work.

    I hope this clarifies my situation.

    Now the problem I am facing is that all tutorials are either

    Database First & Generated Code

    OR

    POCOs & Code First

    So is there really no way out? 


    Sunday, March 9, 2014 2:59 AM
  • Please try to help me understand how mapping an existing Table using EF is NOT POCO?  It gives you Create, Read, Update and Delete just by the mapping...  I still don't understand the need to copy what already gives you that function..

    JP Cowboy Coders Unite!

    Monday, March 10, 2014 1:43 AM
  • Please try to help me understand how mapping an existing Table using EF is NOT POCO?  It gives you Create, Read, Update and Delete just by the mapping...  I still don't understand the need to copy what already gives you that function..

    JP Cowboy Coders Unite!

    Because I want to use my own code. The benefit of EF is that you can use POCOs, but I cannot see how it can be done.
     I am copying the code in my example in this thread is to simulate POCOs. This was done so that other's can replicate my problem. If I cannot get this to work, my own POCOs will not work. Hence my question, i.e. why?

    Monday, March 10, 2014 5:34 PM
  • So if you insist on copying the code you also have to copy the mapping.  There's no way around it as the mapping is how the code converts SQL Schema to .NET equivalents.

    JP Cowboy Coders Unite!

    Monday, March 10, 2014 7:37 PM
  • Follow the instructions here:

    Data Developer Center > Learn > Entity Framework > Get Started > Database First  

    to use a DbContext/POCO code generation for Visual Studio 2010.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, March 10, 2014 7:43 PM
  • So if you insist on copying the code you also have to copy the mapping.  There's no way around it as the mapping is how the code converts SQL Schema to .NET equivalents.

    JP Cowboy Coders Unite!


    So How do I copy the Mapping? i.e. is there some file I can edit?
    Tuesday, March 11, 2014 9:31 PM
  • Thank you David, but this link/url does not deal with POCOs, it works using generated code. As mentioned previously, I can only find tutorials that are either

    Database First & Generated Code

    OR

    POCOs & Code First

    This is an example of a former. What I am looking for is Database First & POCOs
    Tuesday, March 11, 2014 9:35 PM
  • You are confused about what POCO means, at least in the context of EF. That is "Plain Old C# Objects", also known as "Persistence-Ignorant" classes.  They are just plain .NET types without a special superclass or any database stuff embedded in the class definitions. 

    Whether you are using POCOs and whether you are using generated code are unrelated.

    You can have generated POCOs, or hand-written POCOs. You can even have hand-written POCOs with an existing database, although few people do this since you would have to modify your data model in two places.

    This link I posted uses the "5.x DbContext Generator for C#" to generate your .NET types, and it generates POCOs. 

    What you may be looking for is the workflow called "Code First to and Existing Database", which is a bit of a misnomer since it's really a database-first workflow. 

    Data Developer Center > Learn > Entity Framework > Get Started > Code First to an Existing Database

    This will generate C# code exactly as if you'd hand-written it and used it in real Code-First workflow.  The mapping metadata will be implemented using the Fluent API.  After you generate your POCOs using this workflow you can choose between regenerating them after each database change or manually making changes to your POCOs and mapping metadata when the database design changes.

    David


    David http://blogs.msdn.com/b/dbrowne/


    Tuesday, March 11, 2014 11:10 PM
  • Thank you very much David.

     As I mentioned in my original post, I am new to using the Entity Framework. The only material I have read so far is Lerman’s book, but this only covers EFv4. I had not heard about the Fluent API before, and I would need to look into it.

     Looking at the link you suggested, I am curious if that is really Entity Framework?

    (I know that the page is obviously listed under Entity Framework.) I ask this question because if you observe the provider name in the connection string, you would see it to use SqlClient, instead of EntityClient. So if I try non-MS database servers like Oracle, this would not work. This seems to be closer to LINQ than Entity Framework.

     

     

    Wednesday, March 12, 2014 8:16 PM