Using Linq to SQL and dbml to create _and_ upgrade database? - c#

Im using dbml (c# vs2010) to model my database and DataContext.CreateDatabase() to create it. Some time later I add a new property to one entity-type and now I want to upgrade the SQL-database to fit the new version of the dbml-schema. How do I accomplish this? Do I have to delete and re-create the database or upgrade it manually? Is ADO.NET EF better in this respect?
Update:
When searching for methods of updating the database according to changes in dbml I only get results for the inverse; updating dbml from changes in the database. But what is then the visual designer in dbml for? I want some master-design-view where I can do my changes and then generate sql-upgrade-script from that. Isnt that what everyone wants?

Neither Linq2Sql nor EF are made for this kind of scenario.
You asked if this is possible so in response to that I'll say that in theory I guess you could hack around this in EF by altering the schema of your db and using some script to alter the DBML file but I would highly recommend against it. Use the right tool for the job.
Sounds like ADO.NET might be a better solution for you.

Related

EF DB first update model from DB programmatically

I'm learning EF DB first, I have updated one table column and I found a solution from the official document to refresh the model.
Right-click anywhere on the design surface, and select Update Model
from Database.
Is there a way to do this programmatically.
Is there any programmatic solution to that?
No. There is no (common) scenario where updating the model from the database is useful without making manual changes to your code. So there would be little point in in automating that.

C# Is there an easier way to create a database, empty tables, tables with data in them by default, stored procs and views?

Before I posted this question, I did some Googling first on how a database was created through C# and mostly it points to either SMO or SQL query files and it was the time of SQL Server 2005 and 2008.
So at this day in age, is there an easier way to create a database with empty tables, tables with data in them by default, stored procedures and views?
I need a suggestion.
I think the answer is probably Entity Framework. You can do 'code first' and use database migrations, allowing you to write your C# code and use that to generate a lot of the database for you.
Ultimately though, 'easier' is subjective. I personally find EF great for the 'normal' stuff, but at the end of the day, if you need a stored procedure to do some custom logic; you need to write the custom logic, in some fashion.
Maybe have a look and see if you think it fits your needs.
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Looked at the database projects in studio 2013. You create a database as a series of scripts using a familiar GUI. However, changes are published - this process creates a unique change script targeting the connection you define. For new databases the whole thing gets created, but publish against a partial or out dated version and the script created in a change script to bring it up to date.
You can even writ unit tests against your database using specialist tools, although I do find them lacking a bit.
More on msdn - here
Depends. right out of gates. Sp and views. Best shot is directly from database through a workbench. I can then capture definitions and store in a file to be replayed through c#
As for tables there are many orms that can generate tables via c#. Look at entity frameworks. Code first examples
I have generated tables using EF Works fine. I then went into database and created views and sps.
The trick is to migrate new views and sps into your EF model U can google entity Frameworks code first ... Adding views and SPs.
Worst case is u create database all through database workbench. Create a script that an be played to recreate eveything. By running. Then use EF DATABASE first approach
In either case u end up with a good set of autogenerated code to manage CRUD and object management and an abstracted data model

Versioning support for Entity Framework auto generated T-SQL?

I have a solution which uses Entity Framework model first approach.
The problem I am facing is that whenever I change something on a table, add a column or change a relationship,I right click and go for "Generate Database from Model", which re-generates ALL the code for the solution even if I just changed one table..and that generated code is useless for a production database since it drops every table and then re creates them..
I am wondering, isnt there be an option just to generate the T-SQL with the changes I made ? Otherwise model first would be useless after your app goes into prod.
I am using entity framework 5.0
Personally, I would suggest you to use Red-Gate SQL Compare when you need to sync your databases at Production environment.
This tool helps you to compare and synchronize databases using sync scripts without losing data (it will alert about if so) and its UI is just awesome.

About model first EF4 development

I just tried that out, and I also found that If I changed EF model and want to regenerate the database from model , all existing data in database will lost.
Is there any way to fix that, as this always happens during the development.
Check out Entity Designer Database generation powerpack
it's a visual studio add on from microsoft, which let's you modify your db schema without wiping your data.
James Gaunt basically nailed it - generate the schema in a new database and use schema update tools. You don't necessarily need to buy anything there are schema and data compare and migrate tools in Visual Studio under the "Data" Menu.
Try Devart Entity Developer. We offer the database synchronization functionality for Entity Framework and LINQ to SQL models, see this article for details.

How to use LINQ for CRUD with a simple SQL table?

Every LINQ blog I found there seemed around 2 years old, I understand the syntax but need more direction on creating the SQL mapping and context classes.
I just need to use LINQ for 2 SQL tables I have, nothing complicated. Do folks write the SQL mapping classes by hand for such cases or is there a decent tool for this?
Can someone point me in the right direction?
In your project right-click to open the context menu
Add new item
Linq-to-Sql data classes
Open the created dbml file in the design view
Open the servers view
Connect to your database
Drag-and-drop your tables to the design view of the dbml
and you are ready to go!
If you want to avoid using or generating a dbml file (with the editor or not), I believe you can use SqlMetal to generate a set of code files from a database.
More info here: http://msdn.microsoft.com/en-us/library/bb386987.aspx
Example:
Generate source code from SQL metadata directly:
sqlmetal /server:myserver /database:northwind /namespace:nwind /code:nwind.cs /language:csharp
You just add a "Linq to Sql data classes" project item to your project. Then you open server explorer, choose your database and drag the tables in question on to the design surface and you are done.
If you are using a compact sql database look at SqlMetal Builder. This is a gui driven program to generate the dbml file. This is then added to the project. I have found this tool to give me the best results.

Categories