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

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.

Related

Dataset Designer functionality in Asp.Net

I am developing an Asp.Net WebForms website. I am used from my prior project to the "dataset designer", it was my main worksite, I had there all my tables and also hundreds of queries which I could easily call from code like this: "this.InventoryTableAdapter.MyQuery('parameters'), it was great, but I can't find such a thing in Asp.Net, am I missing something. how do you access your special queries in code. do you have to write cumbersome ADO.Net code for each small thing, can someone enlighten me please.
You can still add a database designer to your .NET application. Today's standard ORM for .NET is Entity Framework, although many other good options exist. One thing to research is Entity Framework Database First. Here's how to get started...
In your project, choose to Add New Item. In the Add New Item dialog choose the Data items and then select ADO.NET Entity Data Model. Click Add.
In the Entity Data Model Wizard, select EF Designer from database.
If you have database connections defined within your development environment, you may see one of these connections pre-selected. Or, you can create a new connection to your database.
After setting up your connection, select the tables you want to generate models for. These will be the tables available in your database designer.
This is a very high level how to. You can read more about this approach here: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/creating-the-web-application
Another ORM option is LinqToSql. You can read more about creating a database designer ".dbml" file using LinqToSql here: https://msdn.microsoft.com/en-us/library/bb384428.aspx
I hope this helps.

The DataContext object (LINQ to SQL)

I've read about ways to synch my SQL database with my C# code, and it looks like the DataContext object is the way I'm going to go.
After having read up on what it can do, I find myself still slightly confused on its exact capabilities. For instance, once set up, does the DataContext linked to my SQL database contain a property for each table? And does that table object then contain a list of items, each of which holds the data for each table entry?
In addition, I'm not sure how to instruct it to generate me the code (since I've read that it does generate the necessary objects). How is this achieved?
The Gu (Scott Guthrie) has an excellent article series about Linq to Sql that introduces this concept: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
Good luck!
"does the DataContext linked to my SQL database contain a property for each table"
YES you will be able to accesss all the table and properties,
that you have used in your datacontext (.dbml) file
Anyway, please read this article about LINQ TO SQL. Also there are some good video tutorial about ASP.NET 3.5 LINQ TO SQL
Hope this help
What have you tried so far?
There are two ways to generate the code:
In Visual Studio, on your project, add a new item of type "LINQ to SQL classes". Then drag-and-drop the tables you want in your project onto the drawing space. This is the most simple, but there is no support for refreshing the model if you change the database.
Use the commandline tool SqlMetal.exe to generate the classes.

How to map a database?

I've created a *.dbml file through sqlmetal tool, which contains a diagram of my database (MSSQL). But, my database has 266 tables and if I open that file in VS2008 and export it via PDF, I've got a terrible document. VS2010 will hung up when opening that file. I need to map this database because I need to create an external tool which will work with it.
Is there some conventional approach to learn how this database is created ? I need to know how are the tables connected together (relations) in order to create a tool which will extract data from them.
Or, Is there some other tool which can read dbml files and visualize relations between those tables ?
Going over dbml is probably a bad idea. Suggestions:
Reverse engineer using Visio.
Use the Sql Server diagramming tool, available in Sql Management Studio.
Run dbdesc against the database.

Using Linq to SQL and dbml to create _and_ upgrade database?

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.

Best way to update LINQ to SQL classes after database schema change

I'm using LINQ to SQL classes in a project where the database design is still in a bit of flux.
Is there an easy way of synchronising the classes with the schema, or do I need to manually update the classes if a table design changes?
You can use SQLMetal.exe to generate your dbml and or cs/vb file. Use a pre-build script to start it and target the directory where your datacontext project belongs.
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sqlmetal.exe
/server:<SERVER>
/database:<database>
/code:"path\Solution\DataContextProject\dbContext.cs"
/language:csharp
/namespace:<your namespace>
I haven't tried it myself, but Huagati DBML/EDMX Tools is recommended by other people.
Huagati DBML/EDMX Tools is an add-in
for Visual Studio that adds
functionality to the Linq2SQL/DBML
diagram designer in Visual Studio
2008, and to the ADO.NET Entity
Framework designer in Visual Studio
2008 SP1. The add-in adds new menu
options for updating Linq2SQL designer
diagrams with database changes, for
renaming Linq-to-SQL (DBML) and EF
(EDMX) classes and properties to use
.net naming conventions, and for
adding documentation/descriptions to
Linq-to-SQL generated classes from the
database properties.
Here is an easy fix without any additional software, that just works for simple changes (like added fields, few tables, etc).
Instructions:
You pull a copy of the changed table into the designer (will be removed later)
Now select all the new (or changed) fields and (right-click ->) copy
In your original table right click and insert them (delete changed fields first)
Now delete the table you copied them from
I know it is kinda obvious, but somehow non-intuitive, and it helped me a lot, since all the right attributes and types will be copied, and all links stay intact. Hope it helps.
When to use:
Of course it is - as said - for small changes, but surely better than manually replacing tables with many links, or when you don't want your whole database structure generated by SQLMetal. For example when you have a big amount of tables (e.g. SAP), or when using cross-linked tables from different databases.
DamienG has written some t4 templates which can replace some of what VS generates for you. These can be rerun whenever you like via a command line tool.
T4 templates have the added benefit of being editable. This allows you to tweak what is generated to you hearts content.
I think Jeff complained about this recently. One common technique is to drag all the objects into the designer again...
I hope someone else chimes in with a better approach!
I wrote a tool to do script changes to Dbml scripts see http://code.google.com/p/linqtodbmlrunner/ and my blog http://www.adverseconditionals.com
How about modifying the Properties of the entity/table within the DataContext design surface within Visual Studio?
For instance if I added a column to an SQL Server table:
Open the *.dbml file.
Right click the entity and select Add > Property.
Fill out the values in the Properties window for the new column.
Build your solution.
The auto generated model classes should reflect the new column that was added.

Categories