开发者

.NET ORM help needed

开发者 https://www.devze.com 2023-02-02 07:46 出处:网络
I have SQL server db ready with simple (not many constraints) tables. Now i need to generate DAL and object classes for tables.

I have SQL server db ready with simple (not many constraints) tables. Now i need to generate DAL and object classes for tables.

How much can .NET ORM (like NHibernate or any other free FW you suggest) help in this ? Basically helping out with laborious tasks of writing class and db access functions. I will better spend time in writing business logic for those object classes.

Will it be easy to add/alter tables later ? I have conceptual idea about ORMs but no practical experience in using any开发者_StackOverflow中文版.


You could use Linq to Sql or Entity Framework to autogenerate your classes as well as Data Access functionality.

You can drag and drop your database tables into the DBML (L2S) or EDMX (EF) from within Visual Studio and it will build your classes with members mapped one-to-one to columns, as well as any relationships you have defined.

To create Linq to Sql Classes or ADO.NET Entity Data Model follow these steps:

  • right-click in Visual Studio on one of your Projects
  • Add = > New Item
  • Select Linq to Sql Classes or ADO.NET Entity Data Model
  • This will create a blank design view for creating and customizing your classes.
  • User Server Explorer to open a connection to your database
  • Drag and Drop tables onto the DBML or EDMX canvas and Save when finished.

For instance:

  • table User becomes class User
  • If table User has column FirstName it becomes property FirstName
  • If table User has a relationship with table Contact it will generate IList<Contact> Contacts on your User class. This will let you write code like user.Contacts.Where(c => c.ContactId == 7000) from within the generated DataContext

If you need a more customized way to map your tables you can use SQLMetal which will also generate your classes but allow you to make changes. For instance, converting int properties to enums.


NHibernate is also a good option and has come a long way.


There are many tools that will generate DALs for you.

SubSonic is not an ORM, but a code generation tool that will generate a DAL for you.

There is Entity Framework and Linq to SQL (from Microsoft) that will also generate a DAL for you, with a design surface in visual studio.

With all of these adding and altering tables required regenerating the DAL, so changes and customizations to these files can get lost.


Both Linq2Sql and Entity framework can help you with this to a great extent. With regards to your question about updating tables: Yes, it is quite easy to update tables later. At least a lot easier than if you had written everything by hand (where you would have to do modifications by hand too).


Nhibernate will handle all of your data access logic. I recommend that you define every foreign key and primary key that you can. It is also simpler to have an explicit primary key instead of a composite primary key.

To generate the object classes, I recommend using T4 templates. There are various sample templates available online and it is relatively straight forward to write your own to do exactly what you want.

It is also a good idea to let these object classes serve as data containers and implement your business logic in separate classes that use these DAL classes. This will simplify being able to regenerate them should the underlying tables change.


I'm a little late the party, but Cocoon ORM on Nuget and Github is good. It writes the data access code dynamically. http://guidelinetech.github.io/cocoon-orm/

Feature list below from the website:

  • Automapping of database fields to domain object properties
  • AutoCrud with auto Join creation on get (select)
  • Automapping domain object properties to stored procedure parameters
  • Dynamically creates easy to inspect parametrized SQL that is execution plan and cache friendly
  • Uses simple data annotations similar to that of the Entity Framework
  • Regression and benchmarking tools for the truly paranoid (like us)
  • Table generation tools for use in a code first environment
  • Class generation tools for use in code second environment
  • Easily create non-integer sequential unique IDs using the included Comb GUID or Sequential UID generator.
  • Compatible with SQL Server 2008/2012/20014/+ and Azure SQL (workable with other TSQL databases)
  • Use the Cocoon ORM libraries in your own proprietary code under the weak copyleft LGPL license (changes to Cocoon ORM itself must be shared. Please let us know so we can brain things together!)


Try Vega https://github.com/aadreja/vega. One of the best & fastest .net ORM with enterprise features.

0

精彩评论

暂无评论...
验证码 换一张
取 消