I am using Entity Framework and recently came to realize the benefits of having your EF model in another project within the same solution so that I can build multiple UIs from it.
I moved it over to a new class library project and updated all the references to the entities in the web project to use the new dll generated by the project. Everything has gone smoothly, except for one small snag. When I moved EF over to the new project, somehow it was still reading its connection string from the web.config in the web project (don't ask me how because I have no clue).
I used "Update Model from Database" in the EF designer and it did not find a connection string (as I expected after moving it over to the new project) so I used the wizard to generate a new connection string, which it did just fine. The new connection string now resides in App.config within the class library project. The connection string in the properties window is correct now, and the designer is reading it from the App.Config. I went ahead and deleted the connection string from Web.Config in the web project.
Now when running the application I get the following error:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
If I paste the connection string back into the Web.Config it all works just fine. I do not want to create a new EF model from scratch because it is a fairly complicated model and I did a lot of restructuring after pulling in from the DB. I have poured over the generated CS file as well as the XML in the edmx file and cannot find anything useful. Any help is much appreciated. Obviously for now, until I figure this out, I'm just leaving the connection string in web.config since, for whatever reason, that seems to work.
This is by design; while the config file in the class library is what the designer will use, the configuration file of the actual application is what will get used at runtime. Whether that's Web.config for an ASP.NET project or App.config for a Winforms or WPF project, it's the application configuration file (or something higher up, like Machine.config) that will be used; the file in the class library is not part of the application.
If you're trying to provide an EF model that will work without having to specify the connection string in the application or web configuration file, then you'll have to store the connection string some other way (you could always hard-code it) and pass it into the appropriate overload of your context's constructor.
My solution is generally to provide a static parameterless function on the context itself that calls this overload with the appropriate connection string.
Related
I am having a bit of trouble with Entity Framework and class libraries. We already have databases, so I chose to use database first approach to get type safety.
The resulting dll file is supposed to be consumed by another application, which allows creation of custom "agents" in the form of a dll file.
For testing I use PowerShell to instantiate the classes and call their methods.
Now, you might already see the problem. There is no such thing as a startup project. All I have are class libraries to be consumed.
At runtime, EF cannot find app.config file, nor use the connection string therein. Which of course produces following error:
Exception calling "GetPersonStaff" with "0" argument(s): "No connection string named 'StaffEntities' could be found in the application config file."
Then I tried to explicitly give the class inheriting DbContext a connection string. But that led to following error:
Exception calling "GetPersonStaff" with "0" argument(s): "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. This
will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection st
ring is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and t
hat you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.micros
oft.com/fwlink/?LinkId=394715"
I am kinda stumped now. I tried to google for a solution, but they all either tell me to change startup project, copy app.config/web.config to startup project, or use explicit connection string. None of which are usable in my use case, I believe.
How should I tackle this problem? Thank you in advance.
Well, I figured this out. The correct approach is "Code First from Database", which allows explicit connection string to be used.
I am currently developing a solution with both an ASP.NET project, and a WPF application. I have a common project too, with an ADO.NET entity model, where entities are generated from a database.
If i try to call anything from my database (MySQL) from my WPF, ASP or a Test project i get a InvalidOperationException where it says that no connection string named "DataModel" could be found in application config file.
I then have to add entity framework connectionstrings and other stuff to each project, in order to be able to fetch data from my common project or database. It also means if i want to change the db connection i have to do it in every single project. Isn't there a smarter way to do this?
Thanks..
Isn't there a smarter way to do this You're doing what most people do, at least for small and medium environments, by putting the connection string in each project.
Most projects need different connection strings for different environments (DEV, QA, PRODUCTION). I use and highly recommend the free add-in Slow Cheetah. That tool allows you to define XSLT transforms to modify XML files in your project. In this case, I use it to drop in the correct connection string depending on the build settings.
When you are ready to create a build for the PRODUCTION environment, you just change the Visual Studio solution configuration to Release, and the generated web.config/app.config contains the PRODUCTION connection string.
You can pass the connectionstring that has to be used to the constructor of your DbContext. You have 3 options to pass a connectionstring:
Pass nothing, EF will look for defaultconnectionstring in configfile, if none is found it will throw an error.
Pass the name of the connectionstring you want to use, if it's not found in the config file EF will use the default connectionstring.
public partial class MyDb : DbContext
{
public MyDb(string connectionStringName) : base(connectionStringName)
}
Pas a connectionstring to the constructor. EF won't look in any config files and just use that one, this is probably what you're looking for:
public partial class MyDb : DbContext
{
public MyDb(string connectionString) : base(connectionString)
//Or hardcode it in:
public MyDb() : base("datasource=...")
}
Edit: It is indeed not good practice to do the above, I'm just saying it's possible.
I have used EF5 for my project and I encrypted my connection string in project's web.config file.
And I replaced the constructor of Entities like this:
public PEntities()
// : base("name=PaypalEntities")
: base(Cryptography.DecryptConnectionString())
{
}
But when I want to update my database model with EF wizard, it asks to me for a new connection string and credentials and replaces my connection string in my config file with this. So my project doesn't run properly.
How can I solve this problem?
I'd strongly encourage you to have the Entity framework model in a separate project. If you do that, the project can have its own connection string that points to your reference database (the one you use for making changes as and when necessary). Thus, the running assembly, whether it be a web project or anything else, will just refer to the model project and can have its own connection string.
Yes...
The Solution is, Create a Partial class with same namespace and write the method into it.
Now whenever you update database or edmx file you will find it's default constructor. just delete it.
refer this
http://forums.asp.net/post/4722699.aspx
Try to uncheck the "Save entity connection settings" checkbox:
I'm working on a WinForm application. I've implemented data-access logic into a "library project" that I set as reference in my WinForm project. I'm using LINQ to SQL to connect to my project database, mapping the tables I use into a dbml file. Now I have to publish my project and change the connection string to point to the production DB.
Is it possible to change the connection string without re-compile the project?
It'll be very useful at debug-time and for maintenance...
I've tried to change it in app.config and also in the Settings file, but it seems to still point to the development DB.
Where am I doing wrong?
The solution suggested in this article is very good: http://goneale.com/2009/03/26/untie-linq-to-sql-connection-string-from-application-settings/
But I decided to solve my issue in a different way.
Without modifying anyting in the dbml file I added in my DAO class a constructor that takes a parameter:
public MyDataAccessClass(string connectionString)
{
_connString = connectionString;
}
then instead of using DataClasses() constructor to instantiate the LINQ-TO-SQL class, I replaced it with DataClasses(_connString).
Now I can use the data access library where I need. The connection string will be set in app.config of the referencig application (or anywhere else).
I have used Ruby on Rails with ActiveRecord, so I am quite used to switching to Production/Development database.
I am wondering, how do people implement Development and Production Database difference in ASP.NET MVC (preferably with ado.net entity).
I tried to have it by creating 2 entity datasets with the same name in a different namespace, so I would only need to switch the used namespace in my database code. Unfortunately, this didn't work because the entity datasets do not allow equal names...
The way I do it (with a web app) is to have separate versions of the Web.config file for each of Development (web.config), QA (web-qa.config), and Production (web-prod.config) installs. The config files have different connection strings pointing to the corresponding instances of the database. The default connection string points to the SQL server instance on my local machine which isn't available to the network.
As part of the publishing step, I manually delete the web.config file and rename the appropriate QA/Prod configuration to web.config. This hooks up that instance of the app to the correct instance of the database.
The easiest way (though it's still not as easy as in Rails) will be to include connection strings into App.config.
You should have one entity dataset, and instatiate the entities object using the constructor that takes a ConnectionString, which you can read in from:
WebConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;
The ConnectionStrings element also supports the general configSource attribute.
You can therefore have something like:
<connectionStrings configSource="PathToConnectionStrings.config" />
This allows you to have all the connection strings held in a seperate file, that can then have different values on each environment.
Note that unlike the appSettings file attribute, this doesn't do a merge, it completely replaces the section.
Note that in VS2010, you will have the facility to modify the .config based on the build configuration, so could change the values just by setting your build to "Production" say.