21

I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL, MSL references).

I can compile my application, but when I run I get this error:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception


My question is, where in my code does it realize the POCOs came from auto generation, and how can I get it to behave like Code First? I don't want to reference the CSDL etc in my connection string.

Karl Wenzel
  • 2,412
  • 25
  • 24
grefly
  • 1,181
  • 2
  • 12
  • 28
  • it is bad that you need to use both model first and code first ... – Serdar Dec 23 '11 at 17:05
  • 2
    @Serdar - Not necessarily. He may want to start out generating code from a known model, but then from that point forward start using code first to write new code. – Brian Rogers Dec 23 '11 at 17:10
  • @BrianRogers Now on 2018 your answer save me time and making a question on StackOverflow – fedeteka Apr 14 '18 at 15:53

3 Answers3

24

If the connection string has the metadata, EF thinks it is Model First or Database First. If it is a plain connection string, EF thinks it is Code First. However, if you want to start out doing model first but make EF think you are really doing code first (which is what you are doing), make sure you are using the DbContext code generator, not the default one. Code first POCOs are really that--"plain old c# objects"-- no special database aware or change tracking stuff in them at all. To use the DbContext code generator, right click on your model diagram and choose "Add new code generation item..." then select the ADO.NET DbContext Generator. Also, depending on how you named your primary and foreign keys and/or whether they are more complicated than just simple int IDs, you will probably need to fill in some code to map the relationships between your objects in the "OnModelCreating" method in your context. Delete the line throw new UnintendedCodeFirstException(); and replace it with your mapping code. Otherwise EF may not be able to figure out all the relationships (remember there's no metadata for it to rely on).

Hope this helps.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Your reply helped me out. And I put your reference in my question/reply @ http://stackoverflow.com/questions/18674079/ef-5-0-database-first-model-not-working-on-one-table/18675388#18675388 Thank you – HGMamaci Sep 07 '13 at 16:24
13

You need the following in your config file:

<connectionStrings>
<add name="<The name of your class>" 
     connectionString="metadata=res://*/<test>.csdl|res://*/<test>.ssdl|res://*/<test>.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=<your source>;initial catalog=<your db>;persist security info=True;user id=<your user id>;password=<your password>;multipleactiveresultsets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />
</connectionStrings>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Hi John, Thank you very much. Your answer worked well for me. – Ashok kumar Nov 01 '12 at 02:24
  • if you are switching your connectionString from a (localdb) data source to an SQL data source use this solution. i.e if you are now providing an actual server name for your data source this solution is the way to go. – Marquis Blount Jan 29 '14 at 23:16
1

I'm using Database first and resolved this by copying the EDMX generated connection string to the app.config of my startup application. One already existed but apparently they were different

m0r6aN
  • 850
  • 1
  • 11
  • 19
  • This was actually the answer to my question as well. I was trying to replace the Code First elements of a piece of software I inherited at work with Database First elements, as it is much friendlier to work with Stored Procedures that way. Since the database location isn't different, I tried to reuse the existing connection string. However, taking a working Database First version of the connection string from elsewhere, and using it in my current project's config file solved the issue. – MeanJerry Aug 30 '22 at 19:26