I have two seperate projects in the same solution. One is a windows sevice and the other is a ASP.Net application. The solution doesn't have a seperate data layer. The projects communicate to its own separate database. In the second project which is the web application when i try to do a Enable migration with the following command in the "Package Manager Console", I get an error.
The command:
enable-migrations -ContextTypeName ServicesMock.Data.MockContext -
MigrationsDirectory:MockMigrations
The error refers to the first project and says that the context type "ServicesMock.Data.MockContext" was not found in the assembly "Assembly of the first project in my solution"
How can i sought this out so that the migration command can be pointed to the relevant project ?
I was referring to this link to create the commands.
I tried changing the startup project as well.
Updated (added this section)
Corrected the issue of not selecting the proper Default Project in the package manager console.
In the first Project (Windows service) the Application.config file contains the dbcontext for that project
<connectionStrings>
<add name="ApplicationDbContextConnection" connectionString="data source=abc;initial catalog=serviceDB;user id=;password=;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
</connectionStrings>
The purpose of the second project (Web Application) is to mock some External API references used in the first project. It doesn't have a application.config, instead a web.config.
It has its own DbContext. I have added the connection string in the web.config which is
<connectionStrings>
<add name="MockContext" connectionString="Data Source=abc;Initial Catalog=mockDB;Persist Security Info=True;User ID=;Password=" providerName="System.Data.SqlClient" />
</connectionStrings>
But now when I try to EF migrate the 2nd project(Web Application) , I get an error "No connection string named 'MockContext' could be found in the application config file." But the web application does not use a app.config file. Is it referring to the 1st project ?
Related
I've a Web API project which uses EF 6.0 for database operations. I have 3 different Azure SQL databases (Dev, Test, Prod).
I have been able to create an Entity Data Model with data first approach.
I've used configuration manager of VS2017 to create a web.test config file, but transformation of connection strings isn't working.
Currently my web.config file has the connection string that points to Dev environment as follows:
<connectionStrings>
<add name="ProjectDbEntity"
connectionString="metadata=res://*/Data.ProjectEntityModel.csdl|res://*/Data.ProjectEntityModel.ssdl|res://*/Data.ProjectEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=company-ai-Projectserver.database.windows.net;initial catalog=company.DB_Dev;persist security info=True;user id=Project;password=Password1234$$;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
And, web.test.config is as follows:
<connectionStrings>
<add name="ProjectDbEntity"
connectionString="metadata=res://*/Data.ProjectEntityModel.csdl|res://*/Data.ProjectEntityModel.ssdl|res://*/Data.ProjectEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=company-ai-Projectserver.database.windows.net;initial catalog=company.DB_Test;persist security info=True;user id=Project;password=Project1234$$;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>
</connectionStrings>
I'm not able to figure out how do I make EF change connection string, when I'm using it to build a EF model using one instance of database, but during deployment I want to use other database instances with the same schema.
I've been reading up on the following post, but somehow not able to make it work.
EDIT: I found the solution. All I had to do is select Publish option, then Settings and again select Settings on the popup, and finally select the configuration from the drop down option to the desired configuration. It seems selecting the configuration on the main menu does not publishes the same environment unless explicitly selected.
I found the solution. All I had to do is select Publish option, then Settings and again select Settings on the popup, and finally select the configuration from the drop down option to the desired configuration. It seems selecting the configuration on the main menu does not publishes the same environment unless explicitly selected
I have a solution project in in which I have implemented Entity framework in a class library Project.
I have a App.config file in the same class library in which I am giving my connection string as
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=DDC5-D-4R03T72;Initial Catalog=ServiceAdaptor;User ID=sa;Password=pwd;MultipleActiveResultSets=True;Integrated Security=False"
providerName="System.Data.SqlClient"/>
</connectionStrings>
(I am using Visual Studio 2010, and I am using Code-First Approach EF)
Now the problem is when I run command "Update-Database -Verbose",
It is not creating tables in above DB , rather it creates in .\SQLExpress which I didn't mentioned in anywhere in my soln)
when I gave command like
Update-Database -Verbose -ConnectionStringName "DefaultConnection"
it gave me message
"No connection string named 'DefaultConnection' could be found in the
application config file."
What I conclude is my Update-Database command is not able to get the connection string from config file.
Not able to figure out,What is wrong.
Finally I found the solution, There Problem was here that somehow Update-database command was searching Connection string in Web.Config file not in App.Config file as in my Case I have Class library project which has App.config file.
Since It did't get the Connection string By default EF framework in VS 10 will assume .\SQLEXPRESS as default db and create the tables there.
So the solution I found is:
1) Add reference System.Configuration in the project
2) Insteed of using base("DefaultConnection") in dbContext use as below
base(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)
I have a class library called DataManip, a webform project and a winform project.
DataManip is a class that will contain all the necessary methods to manipulate the database, including handling migrations and database updates.
The .mdf database is also inside the DataManip project.
The problem that I am having right now, is that when I setup a connection string in the App.config file of DataManip, it doesn't recognize the |DataDirectory| substitution string.
Connection string :
<add name="Context" connectionString="Data Source=(LocalDB\v11.0;AttachDbFilename=|DataDirectory|DB\mydatabase.mdf;Integrated Security=True providerName="System.Data.SqlClient" />
Harcoding the path in AttachDbFilename will successfully generate the database, but working with team members make this approach not possible.
The error message that I get when I try to run update-database in DataManip is :
A file activation error occured. The physical file name '\DB\mydatabase.mdf' may be incorrect...
How can I get the DataDirectory substitution string to work in a class library?
I'm using EF and generated .EDMX from it but then I only wanted it to be used for automated generation of Class Files.
I then used the Class Files to create a Entity Model and then created a DB Context and then Repository. I'm calling a WebApi (which is in a separate project but same solution) to access the repository to GET data. While I run the WebApi, I'm getting the error,
{"No connection string named 'DBEntities' could be found in the application config file."}
But within my DAL, I have a webConfig and that has the following entry so I'm not quite sure what has gone wrong,
add name="DBEntities" connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MY-PC;initial catalog=DB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient"
In the DBContext file, remove
public RaficaDB()
: base("name=DefaultConnection"){}
to
public RaficaDB()
: base("DefaultConnection"){}
EF 4.3, EF 5 and EF 6 do not like the connection string being called name=xxxxx
Answer found here -> No connection string named 'MyApplicationEntities' could be found in the application config file
You say "within my DAL, I have a webConfig". I guess the connection string is in the configuration file of a referenced class library, but not in the main configuration file you have in your entry project (a web api project, I guess looking at the tags).
If so, just copy the connection string in the entry project configuration file.
Insert following section in the configuration section of the .config file of the same project where your .edmx file is under.
You may also create different connection string for different environment in the .config file of the main project and pass any of the connection string as parameter of the constructor of the DBContext.
<connectionStrings>
<add name="DBEntities" connectionString="metadata=res://*/Models.DBModel.csdl|res://*/Models.DBModel.ssdl|res://*/Models.DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MY-PC;initial catalog=DB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Setting the project as Startup project worked for me
I found that this worked:
1) Check if you have several "App.config" files.
2) Check if has a wrong name than the connection string it has to use.
3) Save the project and run the program
It should work now.
Copy and paste the connectionstring to your WEBAPI Project web.config file will solve the issue.
It's dumb, but I had this error that was fixed by a Rebuild All !!
Might as well have turned it off and on again....
If none of the above fixes the issue, then probably you are doing the mistake I did, here was my case:
I have multiple projects in my solution and the Startup project was different than the one having the entity framework, switching project from the package manager console seems like a buggy thing especially in entity framework commands, so here what I did:
Set your webapi(Or the project has the entity framework) as a startup project.
Rebuild the solution and try again.
Run the entity framework command again.
If the above doesn't work then try to close the solution and try from Step 2, this should fix it.
The easiest solution:
Remove current edmx file and related connection string in app.config
and add Edmx item with same name as previous again.
this worked for me.
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient Provider, not valid."
I have a working console project. However when I tried to copy the exe and the app.config (same folder) to a live server it didn't work and got the following error. Could it be a domain issue, or some setting that's baked in? I'm pretty sure it has access to databases since I have used other projects except this time is different because I chose edmx.
<connectionStrings>
<add name="AdvWorksEntities" connectionString="metadata=res://*/GroupsModel.csdl|res: //*/GroupsModel.ssdl|res://*/GroupsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=abcd;Initial Catalog=AdvWorks;Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Solution
The app.config is a file name used in a project. However, when it is compiled, the file name becomes application's exe name + .config.
For example, if the application name is "sample.exe", then the configuration name should be "sample.exe.config".