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.
Related
We have a project driven by database design. And when someone makes a db modification, I would like to have my EF code updated. So, I was wondering if there is a way to programmatically generate EF source code given a database connection string. I then plan on attaching this generated EF source code to my solution. I don't need an exact solution right now, but if anyone can point me in the right direction, that would be great.
Thanks!
You could query the SQL Server system tables and generate code that way, perhaps using XML and XSLT or T4 templates.
http://msdn.microsoft.com/en-us/library/ms189082(v=sql.105).aspx
There are three approaches to Entity Framework Development: Database First, Model First, and Code First. You are using Database first.
Entity Framework Development Approaches
I recomend Julia Lerman's book Programming Entity Framework 2nd Edition.
The tutorial I've linked to is for Code First but it has a nice introduction to Repository and Unit Of Work Patterns which you will want to know when using any of the three approaches.
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.
What is the best approach to build a small (but scalable) application that works with Sql Server or Oracle?
I'm interested in build apps that supports multiple databases, in the process behind the feature.
Using an ORM that supports multiple databases is the first step here. You could look at either NHibernate or Entity framework for example - both have oracle and sql server support. That way you should just have to swap out the database mappings to get the application to work on either DBMS.
Edit - thanks to tvanfosson, added the 'new' link for nhibernate.
In addition to the ORM comments; sometimes life is not that simple.
You must keep separate scripts for generating your tables, views, and stored procedures on both systems as they will differ.
You may have the need to do something tricky for performance reasons that is specific to one database platform. For example, making a new partition in Oracle.
You should try to do it at this level by encapsulating it in a view or stored procedure.
Your client code can call the stored procedure with the same signature on any database. You can write a stored procedure that does nothing or lots depending on what that databse requires.
My suggestion would be to use an existing (free) framework, like nHibernate, which abstracts out the dependence on the database for you. Alternatively, you'll need to define your own abstraction layer that is able to interact with drivers for either of the two databases.
as a complement to the other answers, you should tak a look at DbProviderFactories architecture in ADO.Net... a bit low-profiled but maybe useful for you.
As many people have pointed out, using an ORM could solve your problem. I've used LLBLGen with great success. Alternatively you can use the interfaces IConnection, ICommand and so on to roll your own ConnectionFactory.
I would use an OR/M. Most of these have support for many different database vendors and have a database agnostic language to do quering and the like.
I can recommend NHibnernate for C#.
Came across this:
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
And wondering if this is the right solution as I am not that big of a fan of creating a class for every stored procedure or do I use Enterprise Library for ASP.net 2.0 project.
You definitely shouldn't be creating a class for every stored procedure. There are a number of approaches you can take to handling your database interactions. You should have a good look at the major frameworks out there and decide which one best suits you. The Castle Project solution is great, and relies on nHibernate (nHibernate). LINQ is a similar offering by Mircrosoft (LINQ Project). Both of these solutions are full ORM frameworks (Object Relational Mapping) and will generate dynamic SQL to persist your objects in the database. Each also has it's own quirks and likes you to structure your objects in particular ways. If you don't want to manage the SQL your system uses, I would definitely recommend one of these approaches.
I come from a database background, and prefer a bit more control over my SQL. In particular I like to have my interractions handled by stored procedures. I find this enables me to control both the SQL better for optimisation, but helps me manage database security in a more friendly manner. To accommodate this approach, I recommend something like iBatis (iBatis). iBatis isn't a full ORM, but rather a simple SQL mapper. The downside to my approach is that you need to write a lot more code (SQL), but I don't mind the trade-off.
Is there any possibility of upgrading to framework 3.5? if so take a look at LINQ to SQL and Entity Framework as this will accomplish alot of this for you.
If not then as long as it generates standard code that doesnt tie you into 3rd party libraries then you could certainly use it. At my workplace we have our own generator similar to this and it works well although we will shortly be moving to LINQ to SQL.
There are many ways of wrapping a database table in a C# class; you probably want to investigate a few alternatives before choosing between the one you've linked to and the Entity Framework.
There's a software pattern called the "active record pattern" which describes exactly this approach - one C# class for each table, with load/save methods like Customer.GetById(), Customer.Save(), and so on.
For ASP.NET 2.0, check out the Castle Project's ActiveRecord implementation and a third-party Visual Studio plugin tool called ActiveWriter that lets you generate class wrappers for your tables using a drag'n'drop interface.
You will need to determine at what point you need sets of data that are composed from your tables, and whether you want SQL to produce these with stored procedures or if your business logic layer will handle these. As Dr8k says, nHibernate will create SQL for you, but there is a learning curve with nHibernate. The ORM will be in control of how you are getting the data and depending on your environment and DBA's conmfort level you may other issues to overcome.
If you more comfortable with SQL, then there is another tool called SubSonic that will create wrappers ala Active Record for you while offering you the ability to use stored procedures as well. There is also a nice query tool with a fluent interface that you can use if you are not able to use LINQ.
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.