I am working on a web application project where the DAL is generated using LinqToSql designer in Visual Studio. All data access logic is in a separate project.
I created a copy of the database with same structure and same names for tables, views etc. I tried to change the connection string to the new source, but it is not working. Somehow, it still accesses the old database.
All of the following changes failed:
Changed the connection string in config files for all the projects in the solution which is now pointing to new data source.
Create a partial class for the DataContext and passed new connection string to the constructor after changing the connection string to "none" in Visual Studio LinqToSql designer properties.
Passed the connection string explicitly whenever I instantiated the DataContext.
Deleted the contents of bin and obj folders for the LinqToSql project and rebuilt everything.
My project is saving new data to the new datasource, but when reading data it is getting it from the old database, and also throwing null exceptions when it connects to the old DB for data.
What am I doing wrong? Is there any fix for this? What is the best way to make a project with LinqToSql DataContext point to a new datasource with the same data (for example: dev, staging, release, etc.)? Thanks.
If you are reading data from the old database, there must be a connect string or similar in your project that hasn't been updated.
There are three places where the LINQ to SQL designer might be storing the old connection information:
In the app.config file.
In the Settings.settings file.
In the default constructor for the DataContext.
Check all of them.
You could also use 'Find' to search your entire solution for any other references to the old database hard-coded into your application.
Related
We have an existing Db with about 100 odd tables - These were created the old way - use sql queries to generate the Db and use the SQL reader/writer to access and update the DB's.( ADO.net). A developer prior to me added few new tables to this using the EntityFramework (EDMX) approach. They just named the tables generated newly "DBEntities" and used this as a data source for the new pages that were then written in ASP. net webforms(apsx).
I am now tasked with developing some of the newer pages on this existing webforms app to be built in Angular 2.0. I started off with consolidating the one DB that this app reads ( which were referencing both the EDMX file and ado.net - having two config file entries - add name ="DbConn" connection string "".. and add name="DBEntities" the edmx conn string".) I removed the edmx files , used the reverse poco generator and created first class POCO's and generated my tt file. Updated my web config to include only one connection string "DBConn". Tested the app - all the old web form pages were working well. Only those aspx pages(controls that use DataBind..) that were referencing the EDMX files gave me this error - The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
I have tried everything(regenrated POCO's.. checked all the table names against the aspx data sources everything looks good..) and nothing seems to work. Using EF6. - Any help is appreciated..
Thanks much.
I've created a Db and created a .edmx file, using Database first approach. In development, I've used "Update model from Database", if any new field added/ size modified. How can i update the model using code like in production environment.
Any Code alternative for updating model, when database changes.
Please suggest me.
In database first approach, you want to create a separate database project (so that the changes won't affect the other projects).
Once database is changed, you just use 'Update model from Database' as you stated.
Then compile the application and copy the database project dll file to production.
Basically, you are just replacing the old dll file with new one.
Note: if your database and web application are in a same project, you'll need to copy the entire dll file.
I'm working with MSSQL Server 2008 and made a database in it.
Now i made a ASP.Net (MVC 4) project and connected it to the database. I let visual studio generate the Entity Data Model and everything is working fine so far.
Now i have a table for a many to many relation with ofcourse a PK, and two FK. When i manualy insert a row to the database with Microsoft SQL Server Management Studio and i refresh my web page this new row doesn't show up.
I'm 100% sure there is nothing wrong with the C# code (ASP.Net) but after X minutes the row show up :s.
Do i need to update the datasource somewhere or what do i do wrong? (it's my first ASP.Net project :))
Thnx!
Code edit:
private dbEntities db = new dbEntities();
This dbEntities is generated by visual studio from the SQL Server and contains models for all tables.
It sounds like your data context is stale (it's retrieving the data from memory and not going to the DB).
See this question or this one for a possible solution.
Edit: Looking at your sample code, I'm going to guess that this has been declared as a page (or class) member. You should wrap this in a using statement so the object context can be disposed:
e.g.
using (var db = new dbEntities())
{
//perform work
}
LINQ to SQL Connection Strings with class library projects
By default, creating a new LINQ to SQL model (.dbml) will put the
connection strings in both the application settings file and also
web.config / app.config. This is not so much of a problem for web
projects, but what about class library projects? i have a connection
class where I can use it to check connection in all pages but I have an
error where it cant read DataContext at all.
This is a photo that shows my problem.
Generally speaking, class libraries don't support config files. There are ways to make it work, but it's not considered a good practice since different applications may use the same library to interact with different instances of the database. I would recommend looking at a dependency injection or inversion of control solution like Ninject to pass the connection string to the constructor from the app that references the library.
UPDATE:
If you absolutely must read a config file from an assembly instead of the calling application, it can be done with ConfigurationManager.OpenExeConfiguration(). There are several answers here on SO that provide code samples for doing so, but I'm not going to link to them because I strongly encourage you not to go down that road.
By the looks of it you're not using LINQ to SQL - all I can see is an EntityFramework edmx. Check your code generation strategy, and make sure you're trying to instantiate the correct context name (think it's whatever the Entity Container Name is set to).
Also you need to make sure System.Configuration is referenced.
You need to put the connection strings in the main application app.config
Just put a copy of the connection strings in there and you can access them or in this case web.config
I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box.
I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message:
The server encountered an error
processing the request. The exception
message is "Model compatibility cannot
be checked because the database does
not contain model metadata. Ensure
that IncludeMetadataConvention has
been added to the DbModelBuilder
conventions.
After some googling, I discovered that I had to delete the database and let EF create the database. That's fine but I lost all my .NET membership tables. I can go back in and add the membership tables again but if my model changes and EF needs to recreate the database then I have to add the membership tables in again.
How do I get around this?
This is how code-first work. Main idea of code first is that you do not touch your database because it is responsibility of the model to create the database. If you want to customize your database you must create custom IDatabaseInitializer and add your custom SQL.
public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
protected override void Seed(MyContext context)
{
// Here run your custom SQL commands
context.Database.ExecuteSqlCommand("CREATE TABLE ....");
}
}
Now you only need setup your cutom intializer on the startup of your application:
Database.SetInitializer<MyContext>(new MyDbInitializer());
If you don't want to do it this way you must manually maintain your database and set initializer to null.
Found a easier workaround here. I hope this helps.
http://www.paragm.com/ef-v4-1-code-first-and-asp-net-membership-service/
Another option could be to use the System.Web.Management namespace. I've had great success with the code below:
string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseContext"].ConnectionString;
string database = "MyDatabaseName";
SqlServices.Install(database, SqlFeatures.All, connectionString);
It will just create the database and after that you can add users with the standard membership API.
Here's another possibility.
If you look at the MvcMusicStore sample - there's a SampleData class that is responsible for seeding the database on a rebuild. The SampleData class inherits from DropCreateDatabaseIfModelChanges, and overrides the Seed method. This class is passed to the Database.SetInitializer in the Application_Start method in global.asax.
I was getting the same error as you until I changed the parent class of SampleData to CreateDatabaseIfNotExist.
Then you can override the Seed method to insert any data you desire at startup, without it blowing away the database.
While you are developing, create 2 databases and two connection strings. One for SqlMembership (using aspnet_regsql) and one for your EF Application. If you would like to merge them into a single DB in production, just change the connection string in web.config.release to be the same. Then, EF model changes will just drop your apps db and not your membership DB.
By treating your authentication component separately, you will naturally decouple your authentication system from your application system. Then, if you wish to change membership providers, you will be better setup.
As the system grows, you will likely need to support non-pure models without EF code first, so this is a good template for going down that path.
I found the easiest way without playing with anything else was the following.
I ran the application first time with DropAndRecreatedatabase always in the Initilizer.
This created my database for the first time.
Following this I changed this to DropCreateDatabaseIfModelChanges.