Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure entity framwork (Model First ) on microsoft azure?

I was using EntityFramwok (code First) in my application, but for some cause i have to change entityframwork approch to Database First. i have configured the project on local successfully, when published the code to microsoft azure server and tried to login in my application it throws an exception: " The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715 " I have searched about it on google but can't found any clue, seems i am the only person who got this exception :( does anybody knows about this. I am new to azure so don't know how to change the entityframwork approach there. any help or suggestions will be appreciated :)

like image 378
R K Sharma Avatar asked Jul 24 '14 08:07

R K Sharma


People also ask

How do I configure Entity Framework?

Configuration enables you to override Entity Framework Core's default behaviour (conventions) in respect of mapping entities and their properties and relationships to a relational database. Configuration can be applied in two ways; through decorating classes and properties with attributes, or by using the Fluent API.


1 Answers

I just deployed a database first project to Azure and ran into the identical error. Seems like there is a lot more information when googling now than there was when you asked this question. Regardless, none of it was entirely on point for me though and I had to experiment to come up with the solution.

Here's my scenario: My database first DB is hosted on Azure as well and the connection string that Azure recommends for ADO.NET is what I put in my Web.Release.config transformation entries. I have two connection strings, one is the default connection and the other is the one that is used for the db first project. The solution was to use the same (functioning) content from the Web.config connection string and only replace the inner portion with Azure db connection string. I was cut & past happy at first and just wiped out the full content of the connection string leading to the error.

Lot's of the info within the connection string is particular to your project and the db first entities you have created in the project. Here are a couple examples from what I have:

You're Web.config should have connection strings for you local dev environment (chances are these were created for you by the DB first wizard):

    <configuration>
    ...
      <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebRole1-20150218073037.mdf;Initial Catalog=aspnet-WebRole1-20150218073037;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="DbFirstModelEntities" connectionString="metadata=res://*/Models.DbFirstModel.csdl|res://*/Models.DbFirstModel.ssdl|res://*/Models.DbFirstModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\ProjectsV12;initial catalog=DbFirstData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
...
    </configuration>

Notice how the dbfirst connection string starts out with a bunch of meta data about the db models. This stuff is important!!

Next comes the correct Web.Release.config connection string transformation (just posting the DB first one, but you'd need to do something similar for any others that you have):

<connectionStrings>
  <add name="DbFirstModelEntities"
       connectionString="metadata=res://*/Models.DbFirstModel.csdl|res://*/Models.DbFirstModel.ssdl|res://*/Models.DbFirstModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=tcp:<yourdatabesserver>.database.windows.net,1433;Database=DbFirstData;User ID=<user>;Password=<password>;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;"
       providerName="System.Data.EntityClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

So long story, if you're encountering this error after publishing to azure: go check your transformations and make sure you're not removing all that important meta data!

like image 169
Jed Avatar answered Nov 15 '22 06:11

Jed