Mapping existing tables to Domain classes in C# - c#

I don't even know if I'm using the correct terms but here goes:
Is there a way to map the tables and their relations in a SQL Server to domain (C# code) automatically, by means of a tool or something?
I've used the nhibernate plugin, but it creates a file in .cs and another in xml, that has the mapping, but I want that mapping to be present as "property" in the .cs file.
Sorry if this is a bit confusing.

You could always use the Entity Framework or maybe Linq2SQL, but I'm not familiar with how that works.
Tho, out of EF and NHibernate, I prefer NHibernate.

Use LLBLgen of Linq 2 Sql (available in VS 2008). Or use Entity Framework (in VS 2008 SP1).
Linq 2 sql allows you to drag and drop a table from sql server to the canvas and it creates a domain class for you with properties mapped to columns.

Take a look at Fluent NHibernate and see if that is what you are looking for:
http://blog.jagregory.com/2008/08/08/introducing-fluent-nhibernate/

You can use Castle ActiveRecord which uses attributes.

I think you should read a bit about Linq to Sql
http://searchwindevelopment.techtarget.com/tip/0,289483,sid8_gci1269859,00.html#

Check out SubSonic. It will generate your DAL and can also generates code off of custom templates.

Related

Map SQL Query results into entities (C#)

Is there a way to automap a query into a class in C# in order to strong type the query results? I don't want to access to query result with a DataTable, specifying manually the Column Name anymore.
There is couple of options:
EntityFramework - powerful and heavyweight. https://msdn.microsoft.com/en-us/library/aa937723(v=vs.113).aspx
Dapper - lightweight - mostly mapper https://www.codeproject.com/Articles/212274/A-Look-at-Dapper-NET
If you need only mapping of stored procedures, queries etc, Dapper is good way to go, as it's basically adding extension methods to connection object. It cannot create database etc as Entity Framework but it all really depends on needs.
My favourite question. Use QueryFirst. Write your SQL in the SQL editor window, syntax validated as you type, then QueryFirst will generate the C# wrapper to execute it, with strongly typed inputs and outputs. And there are numerous other benefits :-) SqlServer, MySql and Postgres support built in. Others easy to add. Disclaimer : I wrote QueryFirst.
You should take a look at AutoMapper. It's an easy and user-friendly way to map a DataTable to a List<object> of your specification.
You can find an article which talks about it (including sample code) here.
If you want to automize the process, you can use dynamic objects which don't require you to create specific classes for every DataTable that you're using.Hope this helped!
There is a helpful library on LINQ to DB
"LINQ to DB is the fastest LINQ database access library offering a simple, light, fast, and type-safe layer between your POCO objects and your database."
https://github.com/linq2db/linq2db

What's Different Between LINQ and Entity Framework?

I know the benefit of the LINQ and I know use of it in .Net Application. I fill same thing there are providing as a Entity Framework.
So What's Major Difference between LINQ and Entity Framework?
LINQ could be applied to any data source: in-memory objects, XML, SQL, ...
Entity Framework could use LINQ to perform queries against a relational database.
LINQ to SQL is the predecessor of Entity Framework and is obsolete now.
Comparing EF and LINQ is incorrect. Both are different things and they often work together to give better developer experience (and productivity benefit).
LINQ is querying syntax/model that can be applied to any data source. EF provides one such data source.
They are somewhat similar, and can be used in a very similar way, code-wise, but they have some important differences. Note that "LINQ" is not the same thing as "LINQ to SQL"; the EF also uses LINQ. Some notable differences are:
LINQ to SQL is largely SQL Server only, not so much by design as by implementation. The EF is designed to support, and does support, multiple DBs, if you have a compatible ADO.NET provider.
Out of the box, LINQ to SQL has a very poor story for DB metadata changes. You have to regenerate parts of your model from scratch, and you lose customizations.
The EF supports model features like many-to-many relationships and inheritance. LINQ to SQL does not directly support these.
In .NET 3.5, LINQ to SQL had much better support for SQL-Server-specific functionality than the EF. This is mostly not true in .NET 4; they're fairly similar in that respect.
The EF lets you choose Model First, DB First, or Code First modeling. LINQ to SQL, out of the box, really only supports DB First.
SOURCE : Here
I totally agree with VinayC. You cannot really compare.
With Entity Framework, you will be able to have a whole representation of your database in your program. It will help you create classes corresponding to the database elements, connected together like they are in the database. You can after interact with elements of theses classes directly, and like this impact the database. You will have some representation of these classes diagram in visual studio. It's basically often simpler than working directly with the database elements, even if setting it up requires some effort.
The use of Linq is to perform queries on the data sources.

Software that generates C# objects from database tables

Is there a software to generate C# objects from SQL tables?
Most popular from the Microsoft:
Linq to Sql
Entity
Framework
Or subsonic
See a list of available frameworks. I would add that NHibernate have generated lots of good community feedback.
Also take a look at Castle Active Record.
I'm the creator of SqlSharpener which is a light-weight tool that will parse your *.sql files (like in an SSDT project) and create DTO objects and stored procedure wrappers. It is faster than any ORM out there because it generates code via T4 templates to output pure ADO.NET code at design-time, so it's crazy fast.

Generating db schema from c# class

Is there any other method than nHibernate by wich we can generate db schema from class definition? My classes arn't that complex etc (few one-to-many relations). However I would like to just be able to save my objects in db and recreate schema if needed.
I am stuck with .NET 2.0. I am not that particular about performance for this project, I am just lazy to create tables and write save/load code and deel with nHibernate xml.
Thanks
.NET classes are not compatible with a relational data model. That's why ORMs such as NHibernate exist. Without a mapping which describes the conversion of the model to relational tables you cannot create a DB schema. You could take a look at FluentNhibernate automapping feature. If you stick to the conventions it can create the database schema from .NET classes.
NHibernate can do this. The relevant classes -- SchemaExport and SchemaUpdate -- are in the NHibernate.Tool.hbm2ddl namespace. Here's one example.
Yes, ther are other ways. THis is called "programming" - you may have heard of that. Basically, it is possible to write your own database generator. Been there, done that, long before nhibernate had that functionality (or, actually, even existed).
Sit down, write your own database generator.

How do you generate classes in C# from a SQL Server Schema?

I have a sql database and I want to create a class for each table. I think a tool exists that allows me to extract information from a sql database and transform it into classes like "DataTable" or "DataRow". Afterwards, I could use those object in dataset.
Instead of Data Table and Data sets you can use your own objects to represent data in your applications and to do so you can use some persistence frameworks and OR mappers (object relational mappers).For example you can use "Linq to Sql","Microsoft Entity framework" or NHibernate.
There are some code generation tools that let you generate code for these frameworks.
MyGeneration and CodeSmith as two of them that I know.
Maybe T4 (Text Template Transformation Toolkit) ist your friend...
There is a whole world of tools out there that do things like this. It's called ORM. Josh mentioned Subsonic, which is a great free tool. There is also the Entity Framework which is part of Visual Studio 2008 SP1. If you would like an even better tool, the one I suggest you use is LLBLGen.
Hope this helps!
I'm the creator of SqlSharpener which is a light-weight tool that will parse your *.sql files (like in an SSDT project) and create DTO objects and stored procedure wrappers at design-time using T4 templates.
How about good old Linq? Start here.
We are moving in the direction of Entity Framework, but we are taking our time because we dont find EF ready for prime time and Linq2Sql is more or less a poor mans/hobbyists ORM tool. At present we use a combination of a custom homegrown framework (The Kinetic Framework) and Fluent NHibernate.
An option if you want to write your own code generator would be to use SMO objects if you are using MS Sql Server 2005/2008 and pull the information out of the Table/Stored Procedure objects.

Categories

Resources