Okay, this is a weird one. I have a solution that contains two projects. Project A references Project B. Project B is, essentially, a class library project that contains an Entity Framework Model, created database first. In the app.config of Project A, I have a few connections strings that point to different databases on one server. My software works fine. Here are my connection strings:
<add name="LRIP 1, 2, 1A" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="ECP 1" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2ECP1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="ECP2" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2ECP2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="TR-12" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2TR_12;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Now, my coworker is also working on a solution that uses Project B (the data model). He has one connection string in his app.config file:
<add name="Kreus2Context" connectionString="metadata=res://*/Kreus2Model.csdl|res://*/Kreus2Model.ssdl|res://*/Kreus2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=KREUS;initial catalog=Kreus2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
As you can see, the connection string is pointing to the same server (Kreus) that mine are. He wanted to use my connections strings (since I already had them set up) and pasted them into his app.config.
Now, when he runs, he's getting an error:
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...
We're both using the same model (the one in Project B), so I'm not sure why my connection strings aren't working for him. There is no difference between the models, we're using it as a reference for the main project. Being relatively new to EF, I'm at a loss as to where to look for solutions. I'm hoping someone can point me in the right direction.
EDIT: For clarity, I should mention that my program lets the user pick from a drop-down and chooses the data table they want to interact with. I pass the chosen name into the constructor for the datacontext:
var dataContext = new Kreus2Context(name); // name is what the user chose for the drop-down
My co-worker added that same functionality (drop box, and constructor for context that takes a string parm for connection string name).
TL;DR: My co-worker and I are both using the same database first EF model. My connections strings cut and pasted into his app.config just do not work, even though we're using the same exact model (it's in a class library project that we both are referencing in our programs).
UPDATE: As pointed out in the answer below, the ultimate source of the problem here was that my co-worker copied the connection strings into the wrong app.config file. He had two projects in his solution, one for the GUI and one for the business logic, that used the Project B (EF model). The connection strings should have been applied to the app.config file in the GUI project, his executing project. Instead, they were copied to the app.config file in his business logic project, which wasn't even being used.
Do they point to the same project? If they both do the connection strings must reside in the controlling executable. (see No connection string named 'MyEntities' could be found in the application config file).
Model first or database first calls on model creating to consume the .edmx file. Code generated by code first will throw the above exception in the on model creating method since code first does not use it.
a scan of the code will show if this method is returning the exception
if this is the case change the connection string to use the code first format
Related
I am starting with SQL Server in Visual Studio and have a problem, most probably with my connection string.
I am not installing SQL Server on my computer (there are several reasons for that) but created inside my solution a SQL project. Then, in another project I am attempting to connect to this database using some googled examples, but can't manage to connect. I frequently see that there's a user and password used to connect to the db, but in my case, thus there is no installation but an embedded SQL project, I can't work it out...
My connection string looks like this:
sqlConnectionString = #"Data Source = MyComputerName;initial catalog=DatabaseStudents";
Can anyone let me know if this kind of installation is even possible?
Thanks
This should be in your web.config if you're looking for a simple answer
<add name="YourDBContext"
connectionString="Data Source=(LocalDb)\ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\DatabaseFile.mdf"
providerName="System.Data.SqlClient" />
What is the correct way to write a connection string for a SQL Server Database Project within the same solution?
My connection string defaults as
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-PokemonDayCare-20170701121228.mdf;Initial Catalog=aspnet-PokemonDayCare-20170701121228;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
That LocalDB doesn't actually exist, so it creates it once it needs it (I think it does because I'm using single user authentication?).
So I need to make sure it doesn't create another database if I get the connection string wrong.
Here are the properties for my database project (it sits in the same solution as my web app).
How do I create a connection string for the database project?
(sorry if it's stupid or painfully obvious)
In your solution explorer double click properties
Select the Debug Tab
Select Edit Under Target Connection String Setting
I have spent the better part of the day googling for a fix to this. Every post I have found on this problem seems to have the same solution. Put the connection string in the config file for the start up project.
I have two projects. One for the EDMX and one for the website project. I have my connection string in both files, and I still receive the error. I have tried everything involving manipulating the config files to get this to work. I have tried putting the connection string in all the config files in my solution, removing or adding the Entity Framework config section to the config files, and Uninstalling and re-installing EF 5 using nuget.
I inherited this solution, so I don't know much else about how it was created, other than it was Database First.
connection string
<add name="StoreContext" connectionString="metadata=res://*/Store.csdl|res://*/Store.ssdl|res://*/Store.msl;provider=System.Data.SqlClient;provider connection string="data source=*****;initial catalog=*****;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Error:
No connection string named 'StoreContext' could be found in the application config file.
I've been there and struggled with that exact issue.
The solution is to add Entity Framework 6 to your application using NuGet. This will auto generate the correct connection string for you, and automatically set up your app.config perfectly.
The NuGet wizard that sets up app.config is quite smart: if there are some bad settings in it already, it will remove them and add good entries.
For more details, and a step-by-step guide, see my answer at: Upgrade from Entity Framework 5 to 6.
I would check to ensure that the connection string in the start up project is correct. This is usually where I get this same error, and it is almost always fixed by copying the connection string from the Data project with the EDMX to the start up project.
I would also suggest to check that you're importing the reference to the EDMX project, but I think if you're getting as far as the error you're describing, then you're already doing that.
One more thing that I've seen before is that you need to get the Entity Framework for both projects. The other day I had an issue similar to this (can't remember the exact error) where installing EF to the startup project fixed the issue.
Try this:
- Delete (or comment out) the connection string in web.config
- Delete the model.tt file nested in the edmx
- Double click the edmx file and right click in a blank space and "Update Model from Database". This will ask you for a new connection string.
If you do not delete the original, you cannot complete the wizard.
Alternatively, upgrade to 6.1 and your troubles will go away.
I have a new App that I am trying to deploy to Azure Cloud Services. Developed locally using VisualStudio 2012, Entity Framework - Code First, and SQL Server Management Studio.
I have an Azure Cloud Services Project, a Web Role Project, a service layer class (C#) library, and a data objects class library.
These all work together beautifully on my local machine. Web calls service, service calls context, context returns (or modifies) objects. Yay!
I then published the web app. Added a web.release.config transform with this statement...
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=tcp:xmeg1j7x57.database.windows.net,1433;Database=RS.Core.Objects.RSData;User ID=****#xmeg1h7x57;Password=**********;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
and no joy. It still seemed to be trying to call my (localdb).
So I ran it locally (having changed the web.config to have the azure production string) and, at a breakpoint, looked at...
WebConfigurationManager.AppSettings["DefaultConnection"]
expecting a string. Discovered an array. Second value was from the web.config, but the first was an auto-generated reference to (localdb). How do I get rid of this reference? Obviously I'm missing something, but hours of research have proved my Google-Fu to be weak.
Two things to check for that have caused this issue for me in the past:
I was calling some of the System.Security methods (specifically, User.IsInRole()) prior to the MembershipProvider being created. For some reason, this tried to check the localdb instance for user information. Once the MembershipProvider was running, I didn't have this issue. I coded around this bug due to time restrictions.
Rename your connection string from "DefaultConnection" to something else, maybe "AzureDbConnection", and change all the references where appropriate.
You might also find it useful to search the entire project for "DefaultConnection" and see if it is being dynamically set, as well as clearing any app.configs of connection string information.
Try adding a </clear> before the "add"?
<connectionStrings>
</clear>
<add name="DefaultConnection"
connectionString="Server=tcp:xmeg1h7x57.database.windows.net,1433;Database=RS.Core.Objects.RoomScopeData;User ID=RSAdmin#xmeg1h7x57;Password=**********;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
I'm struggling since this morning about this sqlite thing.after reading some good tutorials i decided to use it as my embedded database in my winform application.
i downloaded SQLite-1.0.61.0-setup that install the system.datal.sqlite for me and downloaded the firefox plugin sqlite manager and another management tool called wxSQLitePlus found here based on this tutorials found here (just being explicit)
Hoping i'm not posting any stupid stuff that will be ignored, here are my 2 challenges.
the first challenge is that i can't reference the database inside a folder let'say database
i have exception "Unable to open the database file".my app.config is as follow
<connectionStrings>
<add name="embcon" connectionString="Data Source=database\iagency.db;Version=3;"/>
but if the iagency.db is inside the root folder there is no problem at all.app.config like
<connectionStrings>
<add name="embcon" connectionString="Data Source=iagency.db;Version=3;"/>
the second chalenge is that object created via tools such as either firefox plugin or wxsqlite+
cannot be queried inside visual studio 2005 that i'm using.if it's a table when queried , i have an excetion the object doesn't exist even though both tools displays the same data.
and inversly table created by queries inside visual studio cannot be viewed by the tools.
so what's the deal?
is it a memory stuff or i'm just missing some points?
can enybody explain what is happening ?Thanks for reading.
Try using the full filepath as the data source (e.g. Data Source=c:\data\iagency.db)
See this post for more: datasource location in connection string
Try that :
<add name="embcon" connectionString="Data Source=|DataDirectory|\iagency.db;Version=3;"/>