Everything works great when I run my project locally, but when I deploy to Azure I get the following 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 connection string is:
add name="LifeEntities" connectionString="Server=tcp:abc000000ab.database.windows.net,1433;Database=Life;User ID=myid#abc000000ab;Password=mypw;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"
I'm getting this error when my code tries to open the context of my entity framework database. Note, that I'm using a pretty basic MVC project template that does ASP.NET Identity as well.
I've read that this is because of code first vs data but not sure how to solve this issue.
That's a pure SQL connection string. You need to use a EF connection string, including the model metadata etc.
Here's an example:
<add name="Context" connectionString="metadata=res://*/Context.csdl|res://*/Context.ssdl|res://*/Context.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Context;integrated security=True;MultipleActiveResultSets=True;"" providerName="System.Data.EntityClient" />
Related
I'm developing an ASP.NET Core 3 Web API with a database first generated db context.
I have the connection string in the appsettings.json file.
Everything is fine when I run it locally on IIS Express.
The problem is that when I publish it on Azure, it gives me the error:
System.ArgumentException: Keyword not supported: 'data source'.
[...]
I noticed that the connection string, when published changed from:
metadata=res://*/DTOs.csdl|res://*/DTOs.ssdl|res://*/DTOs.msl;provider=System.Data.SqlClient;provider connection string='data source=*****;initial catalog=*****;persist security info=True;user id=*****;password=*****;MultipleActiveResultSets=True;App=EntityFramework'",
to:
metadata=res://*/DTOs.csdl|res://*/DTOs.ssdl|res://*/DTOs.msl;provider=System.Data.SqlClient;provider connection string="data source=*****;initial catalog=*****;persist security info=True;user id=*****;password=*****;MultipleActiveResultSets=True;App=EntityFramework""
As a work around I changed the line
services.AddScoped<palmdtos>(_ => new MyDbContext(Configuration.GetConnectionString("myConnectionString")));
to
services.AddScoped<palmdtos>(_ => new MyDbContext(Configuration.GetConnectionString("myConnectionString").Replace(""","'").Replace("&", "&")));
Is there a better way to do it?
Models created with the EF Designer are different from Code First in that your model already exists and is not generated from code when the application runs. The model typically exists as an EDMX file in your project.
The designer will add an EF connection string to your app.config or web.config file. This connection string is special in that it contains information about how to find the information in your EDMX file.
Refer to this article.
The cause was the connection string for one of the EDMX files we were using. Since the EDMX has to be read-only, we had to use a different connection string in Azure.
When replacing "e; by a single quote ', it will work fine again. So go to azure website > Configuration > Connection strings > add your conn string with custom type.
Note: Make sure you also select Custom instead of SQLAzure for your Entity Framework connection string, even though the database runs on Azure.
Can you try updating the settings?
In Azure Panel:
Select App -> Application Settings -> Enter new Connection String -> Save
I have a azure V1 function using a project dll that handles entity framework.
First I set connect string like
metadata=res://*/Dev.csdl|res://*/Dev.ssdl|res://*/Dev.msl;
provider=System.Data.SqlClient;
provider connection string='
data source={IP};initial catalog={DBName};
persist security info=True;
user id={User};password={PW};
MultipleActiveResultSets=True;
App=EntityFramework'
and I got
Keyword not supported: 'metadata'.
then I changed my connect string to
data source={IP};initial catalog={DBName};persist security info=True;user id={User};password={PW};
and I got
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
And here's my code
DevEntities db = new DevEntities();
var lstAcAccount = db.AcAccounts.ToList();
return req.CreateResponse(HttpStatusCode.OK, lstAcAccount);
DevEntities is from other dll project that using the connect string above.
So, what should I do to make this work?
You shouldn't use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:
"data source=localhost\sqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"
If it is Database first:
Open the .edmx[Diagram] -> right click -> "Update Model from database"
And see if the will appear the "Add", "Refresh" and "Delete" tabs.
If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead.
Hope it helps.
I've created project to read from MSSQL DB and display some data. VS's wizards created connection string like the following:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
When I tried to deploy application to the real web server I changed connection string to use faceless DB account:
<add name="SomeContext" connectionString="data source=xxx.xxx.xxx.xxx\_name,port;User Id=userid;Password=password; MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
It looks like EF thinks that there some migration needed and tries to execute a lot of meaningless operations including attempts to execute SQL expressions with schema creating.
I have no write permissions on this DB server and I don't see any reasons to allow EF change DB structure.
So I'm completely lost there. What is the proper way to change authorization methods without changing on DB side?
Try to change connection string to production in VS project, then use Update-Database via Package Manager Console.
Pretty strange for me but the root of the issue was in default "initial catalog". It was stored somewhere for connection with integrated security but when I changed authorization method default was broken. As soon I defined initial catalog it was fixed.
I have gone through the steps to publish my web app using database first on the azure portal.
However, when I publish I get this error message:
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 connection string in the web.config after it has been modified by publish:
<add name="MySiteEntities" connectionString="metadata=res://*/MySite.csdl|res://*/MySite.ssdl|res://*/MySite.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:**********.database.windows.net,****;initial catalog=MySite;user id=username#**********;password=*******;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
My context (generated by edmx):
public partial class MySiteEntities : DbContext
{
public MySiteEntities()
: base("name=MySiteEntities")
{
}
...
I am very confused becuase it seems like entity framework is trying to use code first rather than database first.
UPDATE:
I just tried using the same connection string locally and the web app seems to run fine. The web app does connect to the remote database fine. It is only when I publish to azure it fails.
Read my answer to a similar question at Entity framework work locally but not on azure.
If you made the same "mistake" I did, this is what's happening ... the Azure-deployed app isn't finding your connection string "MySiteEntities" inside your web.config. Instead, at the time your created your Azure Web Site (or Cloud Service or whatever), you created an associated Azure SQL Database and gave its connection string the exact same name, "MySiteEntities". This latter connection string is a "plain" connection string without Model/Database-first metadata references, and so is being treated as a Code-First connection by EF, which then complains about the conflict. See Code First vs. Database First for an explanation of this distinction.
It should be:
<connectionStrings>
<add name="MyDatabaseModelEntities"
connectionString="metadata=res://*/MyDBModel.csdl|res://*/MyDBModel.ssdl|res://*/MyDBModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=<provideServerName>.database.windows.net;
Initial Catalog=MyDatabase;
Integrated Security=False;
User ID=<provideUserID>;
Password=providePassword>;
MultipleActiveResultSets=True;
Encrypt=True;
TrustServerCertificate=False""
providerName="System.Data.EntityClient"/>
</connectionStrings>
I changed connection string to remote (Azure) on my local web.config, then remove all set connection strings during publishing and publish web.config. It rewrites remove web.config. Then return connection string on local web.config to local connection. It works fine now.
Hello Entity Frameworks Gurus!!
I've been following the official tutorial and have started a small project with it. I've started using SQL Server Compact Edition and have decided to change it to a SQL Server Express database.
After changing my connectionstring
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|Registration.sdf" providerName="System.Data.SqlServerCe.4.0"/>
<!-- <add name="SchoolContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;Initial Catalog=SchoolRegistration;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>-->
it started throwing this error:
Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention
has been added to the DbModelBuilder conventions.
What I don't comprehend is that I have an Initializer which implements the DropCreateDatabaseIfModelChanges
public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
and added the Initializer to the Global.asax.cs application_start()
Database.SetInitializer<SchoolContext>(new SchoolInitializer());
So after my error I deleted the SQL Server Compact .sdf file and switched back to its connection string and all the changes and all fixtures where put in it.
Why not SQL Server Express?
Something I am missing? Or should I have created another context for the new connection string and refactor my Initializer? Thanks for reading this
EF checks whether the model and the database are in sync by checking the values in the EdmMetadata table. In your case that table is missing in the database. But you are using the DropCreateDatabaseIfModelChanges initializer. Hence EF can not determine whether the model has been changed to execute the initializer.
If you want to use that initializer you need to let EF create the database with the EdmMetadata table (meaning dropping your express database so that EF can recreate it for you from your connectionString). Otherwise remove the initializer and manually do the changes to the database or use EF migrations.