I have a made an EntityFramework model based on my Demo Database.
I want to include connection strings for Staging and production and then in my Console App prompt the user to see which database they want to perform the operation on.
When I was prompted to setup the EF.edmx file I just chose to store the connection string in the app.config file. I have seen a link to change the Connection string of EntityFramework Context when initializing it here
However when I store another connection to my Staging Database, I get an error "Keyword not supported: 'metadata'"
So I stripped down the connection string not to include the EntityFramework type of parameters such as metadata=res://*/MyDBItems.csdl|res://*/MyDBItems.ssdl blah blah
and used a very simple database connection string
data source=myDB;initial catalog=myDB;user id=myUser;password=myPass;MultipleActiveResultSets=True;
now my Context does instanciate but when I get to a query I get another error about:
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.
How can I switch between different databases with Entity Framework 5, thanks!
forget it I got it....what I did...I made a 2nd constructor in the Context.cs file
public MyContext(string dbModel) : base("name="+dbModel) {
}
then in my app.config I have the various settings for the Demo,Staging & Production database connections....this took the full entityframework connection string. I think the link I provided was for Code-First but I am using Database First.
Related
As a part of the migration, I need to prefill an SQL Server table with the data where one of the fields should depend on the target database name or server name. At least it should not be the same for the Development and Production environments.
I wrote a code in OnModelCreating using modelBuilder.Entity<T>().HasData(...) but I still have no idea how to take the target database name here.
I think you can get database name from your connection string.
Put this code inside the Seed method:
var connection = context.Database.Connection.ConnectionString;
or something like that, which provide the connection string, then get the database name from that object.
I have an application using Entity Framework Core to create an SQL Server database and its tables by applying migrations. I need to be able to specify the directory location where the database files will be created.
What I want to be able to do is either:
Have my application create the database with its files in the specified location before applying migrations
Have my application tell SQL Server where to create the database files, before applying migrations
I'm creating my DbContext using the connection string:
Data Source=ServerName;AttachDbFilename=specifiedPath\databasename.mdf;Initial Catalog=databasename;Integrated Security=True
I've tried having the application create the database using a standard SQL Create query before applying migrations. This causes the migrations to fail with the following exception:
Microsoft.Data.SqlClient.SqlException (0x80131904): Database 'databasename' already exists. Choose a different database name.
I assume this is because the DbContext or migrations are trying to create the database specified in the connection string.
Could I somehow edit the migration to remove the step where it creates the database? Or the DbContext?
Just in case anybody is looking to solve a similar problem:
I solved this by simply removing my code that checked if the database already existed. There's actually no need to manually create the file at all.
By specifying the file name in the connection string, using the
AttachDbFilename=specifiedPath\databasename.mdf
parameter, SQL server just created the file in that location.
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.
seems there is no good way to encrypt database connect string for azure website (not cloud service), i already view the solution here
but, i can't store the database first entity framework connect string in the azure website setting, which will get below error, anyone knows how to make data base connect string secure in azure website using database first entity framework?
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: 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
You don't have to include the connection string in the web.config for your site. You can manage the connection string in your azure account at:
App Services>[your_app_name]>Settings>Application settings
Under Connection strings section, you can set the key/value pair which is the connection name and the db connection string.
After you set those values, in the screen, the connection string will be hidden for display.
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" />