Model First with DbContext, Fails to initialize new DataBase
Asked Answered
S

4

23

I give up. I found this: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx And thought, that's cool. So I quickly redesigned my model to take advantage of best of two worlds.

But now my model fails at creating new database (or adding tabls to existing one). 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.

At:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

And that's my connection string:

    <add name="ForumContextContainer"
 providerName="System.Data.SqlClient"
 connectionString="Data Source=.\SQLExpress; Initial Catalog=iForum; Integrated Security=True"/>

Note. I added Initial Catalog later, to try if it going to work, but It was exactly the same.

Stapleton answered 9/5, 2011 at 17:55 Comment(0)
M
33

This is wrong connection string. Once you are using model-first / database-first (EDMX) you must use Entity connection string with referencing .ssdl, .msl and .csdl metadata files. Also be aware that you must create your database in design time when creating model from EDMX = you must generate SQL script and execute it to create the database.

Mcdermott answered 9/5, 2011 at 21:56 Comment(8)
Actually I found a way around by chaning T4 Templates. But thanks anyway,Caribbean
@Łukasz Baran care to share you solution?Anstus
@JoelBeckham My problem was throw new UnintentionalCodeFirstException(); in Entities class.Nu
Great answer--too bad the error message couldn't be a little more helpful.Dirty
Why does EF need a special kind of connection string?Dreeda
@IanWarburton: It needs special kind of connection string only when EDMX is used. The additional info in that connection string holds reference to mapping resources generated from EDMX file and stored in assembly.Mcdermott
@IanWarburton: Possibly yes, but there are classes independent on the generated code which works just fine with only connection string.Mcdermott
Fair enough. It just seems like a cheeky place to be adding framework configuration.Dreeda
N
2

I guess this error usually appears when someone adds the EDMX/db first to a class library in the solution. If you do so make sure that the connection string added in App.config file in the class library project is available in the web.config or the exe project config file (so just copy/paste it there).

Neonate answered 3/1, 2013 at 23:39 Comment(1)
This simple solution worked for me when all others had failed. Just had to copy the connection string section from app.config in model dll project into the MVC app web.config.Hanyang
F
0
Add this connection string to web config and make changes:

<add name="Entities" 
connectionString="
metadata=res://*/EFmodel.csdl|res://*/EFmodel.ssdl|res://*/EFmodel.msl;
provider=System.Data.SqlClient;provider 
connection string=&quot;
data source=SAI-PC;
initial catalog=OrderDB;
user id=sa;
password=Pass$123;
MultipleActiveResultSets=True;
App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />

EFmodel is my .edmx file name.

OrderDB is database name.
Formwork answered 15/2, 2015 at 7:15 Comment(0)
C
-1

Delete or comment this :

//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
//    throw new UnintentionalCodeFirstException();
//}

And change your connection string to valid one.

Cryptonym answered 2/7, 2015 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.