EF how pass connection string to a different project - c#

I've a solution with different projects. I'm using EF6 in my 'DALProject' structured with a repository pattern. What I need to do is to make CRUD operations from another project called 'MainProject' that references the one above, but I receive this error: `
No connection string named 'MyEntity' could be found in the application config file
To resolve this my idea is to pass directly the connection string to the DBContext constructor.
So I copied in an external file the connection string that is in the App.Config of my DALProject, and then I'm going to read this file from the MainProject. This is the connection string:
metadata=res://*/My_Model.csdl|res://*/My_Model.ssdl|res://*/My_Model.msl;provider=System.Data.SqlClient;provider connection string="data source=192.xxx.xxx.xxx\SQL2008R2EXP;initial catalog=My_Catalog;persist security info=True;user id=sa;password=xxxxxx;multipleactiveresultsets=True;application name=EntityFramework"
When this is done I try to set the DBContext in this way:
public class My_Entities : DBContext{
public My_Entities(string connStr)
: base (connStr){}
}
But what I got is this error: `
No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.`
Any helps?

Related

Azure Function unable to use connection string with no Entity provider

I have an Azure Function system that's using a DAL dll. This dll uses entity framework to connect to a SQL database and is in the UnitOfWork format for our database first code.
In azure functions there isn't the standard "app.config" file to update with my connection string to my database. So I added it here in the Application Setting's GUI:
I have copied the code from the app.config by just taking the value from the config, converting the " values, and pasting it into the GUI.
<add name="Entities" connectionString="<This is what I copied>" providerName="System.Data.EntityClient" />
However, whenever I run the code, I get this error:
2016-10-14T12:39:44.248 C# ServiceBus queue trigger function processed message: test
2016-10-14T12:39:44.265 Getting UnitOfWork
2016-10-14T12:39:44.607 Getting Repository
2016-10-14T12:39:44.639 ERROR The connection string 'Entities' in the application's configuration file does not contain the required providerName attribute."
So, typically the provider name would be the entity framework, but I don't know how to include that in the code. My question is, how do I connect with this connection string, and have an entity framework provider?
If you are using Entity Framework 6 or greater, have you tried using Code-based configuration? Some sample implementation can be found here,
http://www.entityframeworktutorial.net/entityframework6/code-based-configuration.aspx
How to set connection in Entity Framework 6 using Code-Based Configuration

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 do I stop enity framework for trying to connect to database in App_Data

I am building a web application including Entity Framework. In my solution I have 2 projects. 1 project for Data Access which contains my DbContext class:
public class MyContext : DbContext
{
public MyContext() : base("MyContext")
{
Database.SetInitializer<MyContext>(null);
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
}
}
and another project for setting up the WebApi I want to use and also contains a web.config. In my web.config I have a connection string looking like this:
<connectionStrings>
<add name="MyContext" connectionString="Data Source=ServerPcName\SqlServerName;Initial Catalog=UCP;Integrated Security=True" providerName="System.Data.EntityClient"/>
</connectionStrings>
The connection string should be correct. I retrieved my connection string by following the next steps. First I opened the Server explorer (Views -> Server explorer) and in the server explorer I set up the connection to my database. Next I did right mouse click on my connection and clicked "Modify connection...". In the dialog I clicked on the button "Advanced...". In the next dialog I selected the connection string in the lowest edit box and paste it into my web.config.
Now I try to update my database with the next command:
Update-Database -StartUpProjectName WebServices
This command returns the next error:
Directory lookup for the file [rootpath]\App_Data\UcpContext.mdf" failed with the operating system error 2(The system cannot find the file specified.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
It looks like to me that it tries to connect or create a database that suppose to be in the ASP.NET folder App_Data but I want Entity Framework to connect to my server. What am I doing wrong here or what is causing the issue that Entity Framework doesn't see or want to use my connection string properly?
Use a different string in the constructor:
base("name=MyContext")

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.

How to connect to the connection string written in the web.config of the web services

We have a web service project called 'Service' and in the web.config of the 'service' I have set the connection string as follows:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=L308;Initial Catalog=Dashboard;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I am trying to access the connection string from another project 'DBConnector' using the following code but getting null reference exception even though after adding the reference of the 'Service' into 'DBConnector'. using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
You will need to have the connection string in DBConnector project config file. Adding reference to a project doesn't bring in the config of that project in the main config of the project
If i got you
You can not do that if you want to use the Connection string of service in you library. You need to expose that as property or method
in you service.cs
public string connectionstring
{
get
{
return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
}
or
DBConnetor added as referance
If the DBconnector has added as referance in the Service layer than you can access it easily by the code you have written
If you need want to make that connection string Global so all you applications and services can use it you will need to put it in the machine.config.
The machine config if found here %WinDir%\Microsoft.NET\Framework\\CONFIG.
'Service' into 'DBConnector'.
Seems to be incorrect thing to do. It should have been other-way around!

Categories

Resources