Unable to load the specified metadata resource. On code first approach - c#

Hi i am trying to create the tables using code first approach on EF5. I have created a database called "DEV_Database" on a server called "SERVER01".
After I enter "enable-migrations -contexttypename Entities -force" on package manager console I get this error "Unable to load the specified metadata resource."
I had this connection String auto generated for me by VS12, which words fine with other approaches.
Can anyone help me with this. My connection string is below.
<add name="Entities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVER01;initial catalog=DEV_Database;persist security info=True;user id=USER1;password=pass;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>

Related

Entity Framework: Update-Database, when I'm not authorized to write to master

I have created a SQL Server database on Amazons Web Services. This means that I'm not authorized to edit the master database (or I believe so).
I followed the tutorial code-first migrations with an existing database
And I have been able to use the commands
Enable-Migrations
Add-Migration InitialCreate –IgnoreChanges
But when I use this command
Update-Databse
I get this error
This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.
And my connection string looks like this
<connectionStrings>
<add name="DBContext"
connectionString="data source=DBName.ctekr2i3aabo.eu-central-1.rds.amazonaws.com,1433;initial catalog=WeeklyReview;persist security info=True;user id=theodor349;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>
I needed to supply the password for the connection.

ServerVersion = 'conn.ServerVersion' threw an exception of type 'System.InvalidOperationException'

I've seen this error answered for other people, but I can't seem to get it to work. I'm trying to connect to a SQL Server Database project locally.
I'm getting this error thrown on Open() when I do the follow:
SqlConnection conn = null;
conn = new SqlConnection(connection)
conn.Open()
Where connection is a connection string passed from my webconfig. I've found examples online and tried these three connection strings and I get the same error every time:
<add name="TICKETING_CONNECTION" connectionString="Server=(local); DataBase=Ticketing_DB; Integrated Security=SSPI"/>
<add name="CONN"
connectionString="Data Source=localhost; Integrated Security=SSPI;Initial Catalog=Ticketing_DB"
providerName="System.Data.SqlClient" />
<add name="CONN2"
connectionString="Data Source=(local);Initial Catalog=Ticketing_DB;"
providerName="System.Data.SqlClient"
/>
Could the problem be stemming from the path of where the database is located on my machine? I've tried copying the c:\ path but didn't have any luck. I've seen some examples online use a .mdf file, but my project does not have this file either.
This is my first time trying to connect to a database project, so any help or advice would be greatly appreciated
If you are using Visual Studio try going to the Server Explorer and Right click on Data Connections and Add new connection by selecting DataSource as Microsoft SQL Server and provider as .net framework provider for SQL Server.
Under the server name check for the SQL Server if it is listed with your machine name.
Then try to provide the database details and user name and password for SQL Authentication or select windows for windows authentication. Then do a test connection. If it is fine on click of OK it will add the data connection.
Right click the properties of data connection which is newly added and navigate to properties to extract the connection string. Compare it with yours. This can help you troubleshoot the issue.

Entity Framework not using connection string in app.config

My error message states
The underlying provider failed on Open
and the InnerException states that the database cannot open by the requested login.
The strange thing is that my connectionString is showing a different login than what's mentioned in the error. Why is the application using an incorrect connectionString than the one that's explicitly defined in my App.Config?
Few common reasons why:
1) You aren't using that project as your StartUp Project
2) You didn't match the name in the config
<connectionStrings>
<add name="YourContext"... >
</connectionStrings>
3) You are hard coding it in the YourContext.cs

How to use localDb with entity framework migrations?

I have Persistence class library, which contains my DbContext class. It also contains app.config file, where I have predefined connection string like this:
<connectionStrings>
<add name="<Namespace1>.Persistence.AssessmentContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AssessmentContext.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Assuming that AssesmentContext has Namespace1.Persistence namespace.
When I try to enable-migrations using PM console, it gives me such an error:
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.
When I tried to debug it and put debug output into AssessmentContext ctor I found that the connection string is still using .\SQLEXPRESS data source.
Data Source=.\SQLEXPRESS;Initial Catalog=<Namespace1>.Persistence.AssessmentContext;Integrated Security=True;MultipleActiveResultSets=True
So what I am doing wrong? And why EF doesn't take my connection string from app.config?
The above connection string entry must be copied to the app.config of the executable program if you are using desktop app or copied to web.config if you are using web app.

The underlying provider failed on Open in entity framework connection

i use Entity framework in .net 4 and use this connection in project:
<add name="Database1Entities" connectionString="metadata=res://*/Model.Model1.csdl|res://*/Model.Model1.ssdl|res://*/Model.Model1.msl;provider=System.Data.SqlClient;pr ovider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\Database1.mdf;initial catalog=Database1;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
and show this error:
The underlying provider failed on Open.
I don't know why you get this error but I use this connection string and I don't have any problem.
remember the name of the DbContext class and connection string should be same
<add name="MyDB"
connectionString="Server=127.0.0.1;User ID=sa;Password=password;Database=Database"
providerName="System.Data.SqlClient"/>
The problem is not in your connection string. This is mostly happen when you are rebuild and clean the project again and again. there is a simple way to solve this problem is that close your local host and wait 2 to 3 minutes. If you face this problem again then close your project and open your project again. Your problem will sort out.

Categories

Resources