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

2

5

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 :)

Shadwell answered 24/7, 2014 at 8:20 Comment(0)
S
3

While configuring your website and database on the azure server you have to define a connection string and the name of the connection string should not be the same as connection string name in your web.config file. I just did rename the connection string name in my web.config and problem solved. :)

Shadwell answered 19/8, 2014 at 11:6 Comment(0)
T
4

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!

Trillby answered 3/3, 2015 at 18:29 Comment(3)
appreciate your research Jed, In my case renaming the connection string does the trick.Shadwell
@RKSharmaSharma your question and solution lead me down the path to solving my particular situation of the same error. So thank you for having them on here. I wanted to share my findings and solution since this post was in the top 5 for my googling of the error just incase my time could help save someone else from the same troubleshooting. :DTrillby
top result for me on google. fixed my problem straight away, i didn't realize that ugly meta stuff was important.Unleavened
S
3

While configuring your website and database on the azure server you have to define a connection string and the name of the connection string should not be the same as connection string name in your web.config file. I just did rename the connection string name in my web.config and problem solved. :)

Shadwell answered 19/8, 2014 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.