I have an existing database and also some existing code that I'd like to push to that database using the Entity Framework (v6.0.0.0).
I am having no problem connecting to the DB, and I have examined the context and migrations files, all look good to me.
When I run the Update-Database -Verbose command, I'm getting the following error:
CREATE DATABASE permission denied in database 'master'.
I'm not clear why it's even trying to create a database, b/c one already exists, and I'm specifying the DB in the settings.
Here is my app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="DataModel" connectionString="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]"/>
</connectionStrings>
</configuration>
Any reason it would be ignoring my DB name and trying to create a new one from scratch?
So I figured it out...
The solution (in my case at least) was that I had two connection strings in my app.config file.
I had one in the <connectionsStrings> section and another in the <parameters> section (see above).
At one point I even commented out the one in <connectionStrings> because the other one seemed to look more like an Entity Framework version.
As it turns out, I followed the advice I found in a few other answers regarding setting up an empty class in the DbContext file and hard-code it to the name listed in the <connectionStrings> section.
So like this:
public class YourContext : DbContext
{
public YourContext() : base("name=DefaultConnection")
{
}
public DbSet<aaaa> Aaaas { get; set; }
}
After I did that, I got the following error:
The connection string 'DataModel' in the application's configuration file does not contain the required providerName attribute."
So, I changed my connectionStrings to look like this:
<connectionStrings>
<add name="DataModel" connectionString="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]" providerName="System.Data.SqlClient" />
</connectionStrings>
And after that, I got the following error:
CREATE TABLE permission denied in database '[xxxxxxxxx]'.
So I went back to the DB and added the db_ddladmin Database Role for the DB in question.
After that, I ran Update-Database and it worked!
Of course, I went back in and removed the db__ddladmin permission and also the View any database server permission.
Related
I'm trying to use Entity Framework to update-database.
It's running fine with no errors. When I use -Verbose it's showing
Target database is: 'PokemonAppCore.PkmnContext' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention)
But in the app.config I've pointed it to a local db, like so. Nothing is showing up in SQL Server Explorer under LocalDb.
Why is it using .\SQLEXPRESS instead of localdb like it's specified in app.config?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</configSections>
<connectionStrings>
<add name="PkmnConnectionString"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=PkmnDatabase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
At first and as mentionned by Mohammad Akbari, it's important to pass your connectionstring name on DbContext constructor.
I'd like to add one more thing, the startup project (in VS solution) should be the one where EF is used, the update-database command is seeking for web.config (or app.config) of startup project.
Regards,
I' am getting this:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
error in my Entity Framework.
I read this Question and the possible answers, however non of them worked for me. Here is my App.config file:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="SalesReportEntities"
connectionString="metadata=res://*/SalesReportModel.csdl
|res://*/SalesReportModel.ssdl
|res://*/SalesReportModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=.;
initial catalog=Training;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
What other possible things can cause this error?
I can offer a few suggestions and some are similar to the thread you already looked at:
Can you use SQL Management Studio and verify this database exists and has tables and is currently in a responsive state?
Put your 'connectionStrings' section after the < providers > node.
Your parameter value is 'v11.0' which seems to be targeting a version of SQL Server 2012. Mine that I am looking at now is similar to yours but just states: "mssqllocaldb". Which I believe is just the default instance of sql server from the connection you specify.
Uninstall Entity Framework completely and get it again from NuGet. I assume you are using Visual Studio so in 2015 it is Tools>NuGet Package Manager>Manage NuGet Packages. Uninstall, check your app config and 'References' that 'EntityFramework' is gone. Reinstall and check your settings again.
Are you trying to reference a project that entity lives in in another project or is it self contained? If so you need to target EF just like you were on the source project with adding EF to that project and a config the same.
You may also want to post the entire exception for better answers too.
I have a new ASP.NET web application I am calling "Smartifyer" that was empty to start with. I added EntityFramework with Nuget and have created a model called WordModel and the following context:
public class SmartifyerContext : DbContext
{
public SmartifyerContext()
: base("SmartifyerDatabase")
{
}
public DbSet<WordModel> Words { get; set; }
}
Within Web.config in the root directory I have the following configurations:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="SmartifyerDatabase"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SmartifyerDb.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
When running a test on the context, the Find method takes about 30 seconds until it times out with the following error:
System.Data.SqlClient.SqlException: A network-related or
instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL Server is configured to
allow remote connections. (provider: SQL Network Interfaces, error: 26
- Error Locating Server/Instance Specified)
I have a pretty poor understanding of setting up SQL connections and what all the configurations within Web.config do. I would like the .mdf database file to be auto-generated if it is not present but I am unsure how to modify my configurations to do that.
EDIT WITH SOLUTION:
All of my connection config and database stuff was setup correctly. My problem was that I was running a unit test which was using the test project's app.config file rather than the main project's web.config. Copy and pasting my web.config into the test project's app.config fixed the issue!
The error you're getting is because the EF is not able to locate the database instance service. The EF have default connection string that connects to developer database known as LocalDB\V11.0. Here your application is trying to connect to LocalDB\V11.0 as specified in ConnectionString.
<add name="SmartifyerDatabase" connectionString="Data Source=(LocalDB)\v11.0; ......
You must ensure that LocalDB instance is installed on you machine. see this answer. If it's not there install it and try to connect manually to the LocalDB instance then try with the application. Hope this will resolve your issue.
For information about the SQL LocalDB see this MSDN article.
You can also use your SqlExpress or other Sql database if it's installed on your machine. Just change the DataSource property in the connectionstring and you will be good to go.
How do i get the EF model designer at design time see the connection string in a separate config file without prompting for 'Choose you data connection' when i try and update model from database.
I have a separate config file for connection strings to run against different environments. In app.config i use <connectionStrings configSource="connections.config">.
I do not want to save the connection in app.config or web.config. Run time works fine just seems to be a big limitation on the designer.
To reproduce the problem simply create an new ADO.net Entity Data Model. Store the connection string in the app.config. you will get an app fonfig like below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Now copy the connectionstring and put it in a new file 'myconnections.config'
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Edit the app.config and change the connection string settings to
<connectionStrings configSource="myconnections.config">
The model designer will now not know about the connection. If you click 'Update Model from Database' it will prompt you for the connection and want to save a connection back to the app.config. Really frustrating.
Well I know this question is 5 years old, but I actually found the solution. I am using VS2019 and when I add a new data connection in Server Explorer, EF will see my new connection. While this is not actually the perfect solution because we have to add connection string separately, it still works.
I have written dll in c# which is used by Excel (the dll is COM registered). I have no problem connecting with Excel. The dll retrieves data from a SQL Server database using Entity Framework 5. If I run the dll through a console app, the dll works fine. But when I run it through Excel (through VBA) I get a an InvalidOperationException. The error message is "No connection string named "MegaDailyEntities' could be found in the application config file. This occurs the first time I try to retrieve data from the database.
I ran into this problem with the console app, but then I include the following in my App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="MegaDailyEntities" connectionString="metadata=res://*/MegaDaily.csdl|res://*/MegaDaily.ssdl|res://*/MegaDaily.msl;provider=System.Data.SqlClient;provider connection string="data source=CQI-Laptop1;initial catalog=MegaDaily;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
and it worked fine.
So my question is, how do I get Excel to use this connection? Is there a config file for Excel?
Thanks
The reason why connection string is not found is because .config files exist only for applications (.exe) but not for additional libraries (.dll)
Excel process tries to load connection string from Excel.exe.config, instead of loading from yourlibrary.dll.config
Working solution for this is to edit your *.context.tt file (EntityFramework classes generator) and update contructor for you DbContext.
You will find something like:
public <#=code.Escape(container)#>()
: base("name=<#=container.Name#>")
{
...
}
It should be possible to change it into
public <#=code.Escape(container)#>()
: base("your EF connection string")
{
...
}
Or take a look how to set connection string from code