LINQtoSQL Not saving to database,but changes show in app - linq-to-sql

Hey all, I have a linq app using C# express2008 and sqlserver express 2005 (mdf file connection)
I followed the regular dml generation and vanilla datacontext. However i created a repository class to manage the Linq stuff.
In using the functions, selecting data works fine, updating data works in the app.But when i check the data in the tables, nothing has changed.Needless to say, when i close the app, also no change.
I used SQL profiler to see what was being sent to sqlserver express, nothing showed up.
What could be my issues?

Are you calling SubmitChanges() on the DataContext?

No primary key, or no column(s) in the L2S model marked as primary key member(s)..?

Ok... found 'a' soln.
1. changed (forced) default connection string from looking at a file to an instance of sql serverĀ (using database instead of file)
2. Attached file to sql server express..and renamed the db.
Ran app again and everything works. Only thing is..the dml still uses the old connection string..so any mods to tables have to be done in sqlserver.
This is just my quicky patch, anyone care to provide a more elaborate view?

Related

Migrate Table Data Only from MSSQL to MySQL

Been tasked with moving a code first database from MSSQL to MySQL. After a few hours of kung fu, I was able to get the asp.net core project to properly deploy all migrations to mysql. Now I need to migrate the data inside of the existing mssql tables. I saw posts mentioning MySQL Migration Toolkit but that appears to be old. Also attempted to do so with MySQL Workbench and DBLoad's Data Loader but haven't had any luck.
Table structure is pretty simple with incremental integer keys + the usual crap with asp.net core identify framework (GUID). Just need to keep that consistent during the migration. What is the best way to migrate the data now that the table structure is setup in MySQL? Any recommendations would be greatly appreciated!
Update: Some more details...
I attempted a direct migration of the database from MSSQL to MySQL using MySQL WorkBench and DBLoader. But failed on the ASP.net Identity tables big time plus other issues. .net core api took a huge dump in multiple places so that idea is out.
From that point, I migrated the api controller over to mysql and then had to fix a myriad of issues related to mssql fks being too long and a few other issues.
So at this point, the controller works on mysql. I just need to dump all of the data into MySQL and keep the FKs consistent.
I have had a few thoughts with it such as CSV export>import and/or trying a few other things. Any recommendations?
You can use Data Export wizard built-in with SqlServer Management Studio (task -> Export Data)
or use SSIS package to migrate data.
Tried MySQL Workbench, DBLoad from DBLoad.com etc to migrate the data directly and they all failed.
So ended up finding a "solution"...
First off, I modified the ASP.net Core project with:
//services.AddDbContext<ApplicationDbContext>(options =>
// options.UseSqlServer(
// Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
Then went to Package Manager Console and ran: update-database
This created all of the tables in MySQL.
Then opened up Microsoft SQL Management Studio. Right clicked on the database Tasks > Generate Scripts. Saved everything to one file with Advanced option Schema and Data selected.
Then opened the db script in Notepad++ and applied the following edits using Replace with Extended Search Mode enabled:
GO -> blank
[dbo]. -> blank
[ -> blank
] -> blank
)\r\n -> );\r\n
\r\n' -> '
DateTime2 -> DATETIME
After edits were made in Notepad++, I removed all of the SET, ALTER and CREATE related stuff from the text file and then copied all of the insert lines into MySQL Workbench in order to ensure foreign keys were already populated before that table's data was inserted. Thank goodness there were no Stored Procedures to deal with! What a pain in the tucus!
Also on a side note, the app is hosted on Azure. Spent a couple of hours fighting the API not connecting to the database. This was not apparent at first as the app was fake throwing a 404 error to Postman. When I first attempted to wire up the API controllers to the MySQL DB, I entered the database connection string into Azure's App Service Configuration. It didn't work at all even though running the app locally worked fine. Ended up finding another post on this here site mentioning to get rid of the database connection string out of the App Service > Configuration window. Worked like a champ after that. All of the data with its auto incremented keys linked up without issue.
I am very pleased with the results and hope I never have to go through this process again! It is always a nice feeling to know an app now runs on a completely open source infrastructure. Hope this helps someone. Good luck.

Entity Framework 4.1: Code first error: "the model backing the context has changed since the database was created"

Im creating a database via a "code first" application, the sql server contains no databases.
The application runs fine, creates the database and seeds the data i have defined in my initializer.
a service i have running tries to add some data to the database for the first time. i get the error:
The model backing the 'yyyContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
There should be no changes since the database was created and when then service runs.
I'm running EF4.1, and the database doesn't exist so unlike questions with similar titles:
Database.SetInitializer<YourContext>(null);
Isn't the solution for me.
Any ideas about what could be wrong are welcome.
Doh! moment, turns out the service wasnt using the same connectionstring as the other app.
the reason the databases didnt look the same must be because earlier in development i started the main app with no connectionstring aswell, so it provisioned a local instance database for itself to use.
Then later when i was trying to use the service, it was trying to access the same database from earlier, and the model changed significantly since then.
I pointed the connectionstring to the correct database and everything worked from there.

Entity Framework 4.0 is not Importing my Associations from Sql Server 2008 Express R2 database

I'm trying to use Entity Framework 4.0 in my VS2010 project. I've always used Linq to SQL for my projects with great success, and wanted to try using EF4 for a change. Unfortunately when I go through the steps to add EF4 model to my project, none of my associations are created.
Here are my steps:
Right click on my project in VS2010 solution explorer
Click Add => New Item...
Select 'ADO.NET Entity Data Model'
Select 'Generate from database'
Connect to my SQL 2008 Express R2 database
Check the 'Tables' item
Pluralize or singularize generated object names is CHECKED
Include foreign key columns in the model is CHECKED
Click Finish!
I see all of my tables, but there are no associations between any of them. When using the 'Linq to SQL Classes' item, all of my associations are generated as I would expect. Even when I dumb it down to only include two (associating) tables during the EF4 setup process, the associations still don't show up.
I've tried manually adding the assoications through the EF data model designer, but it generates other errors when debugging the project. The errors indicate that my named database associations are not configured correctly in the database.
UPDATE: I thought it was a bug with EF4, but it appears as though I'm missing something in the constraints / associations. I'm going to rebuild some of my tables from scratch and see if that helps. My db was converted from SQL CE 3.5, so I may have missed something along the way.
Thanks
I thought it was a bug with EF4, but it appears as though I'm missing something in the constraints / associations. I rebuilt some of my tables from scratch and it fixed my problem.

Getting MySQL code from an existing database

I have a database (mdb file) that I am currently busy with. I would like to know if it is possible to generate MySQL code that would be used to create this database?
There are a couple of tools you can look at to try to do the conversion.
DataPump
Microsoft DTS (Nos Called SQL Server Integration Services)
Other option might be generate MySQL code from Access' DB MetaData you can access from JDBC, ODBC, ADO.NET or any other database access technology with metadata support. For this option you need to generate a piece of code (script). So it will only make sense if your access DataBase has a lot of table with a lot of columns or if you are planning to do this task several times.
Of course, using one of the mentioned tools will be faster if it works.
You can certainly write DDL to create and populate a MySQL database from the work that you've already done on Microsoft Access. Just put it in a text file that you execute using MySQL batch and you're all set.
If you intend to keep going with developing both, you'll want to think about how you'll keep the two in synch.

Linqtosql sync with the database

I am about to use linqtosql in my first asp.net mvc application.
I have come up with a database schema. But the problem is that I may change few of the tables in future. So keeping the model classes in sync with database will be a issue.
I got this link which states the similar situation,
keep LinqToSQL sync with the database
My question is, has any body used the third party tools given in the above post,
do they work properly
www.huagati.com/dbmltools/
www.perpetuumsoft.com/Product.aspx?lang=en&pid=55&tid=linqtosqlsynchronization
Or is there any better approach for this problem.
The "official" approach is to simply delete any out of date tables from the designer then drag the updated table from your Server Navigator back on again. I've been using this method for well over a year now and so long as you make your data context changes at the same time you're updating the database you should be OK. It also gives you extra incentive to make sure you have your database structure in order before continuing.
There is also SQLMetal.
http://msdn.microsoft.com/en-us/library/bb386987.aspx
This is whats in our CreateDBML.bat file
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
sqlmetal /server:{server-name} /user:{username} /password:{password} /database:{databasename}
/dbml:..\..\Codebase\Domain\CompanyName.ProjectName.Domain\Entities\ProjectName.dbml
/namespace:CompanyName.ProjectName.Domain.Entities /pluralize /views
sqlmetal /code:..\..\Codebase\Domain\CompanyName.ProjectName.Domain\Entities\ProjectName.designer.cs ..\..\Codebase\Domain\CompanyName.ProjectName.Domain\Entities\ProjectName.dbml
pause