Best way to convert IList or IEnumerable to Array

Which version of .NET are you using? If it’s .NET 3.5, I’d just call ToArray() and be done with it. If you only have a non-generic IEnumerable, do something like this: IEnumerable query = …; MyEntityType[] array = query.Cast<MyEntityType>().ToArray(); If you don’t know the type within that method but the method’s callers do know it, … Read more

NHibernate vs LINQ to SQL

LINQ to SQL forces you to use the table-per-class pattern. The benefits of using this pattern are that it’s quick and easy to implement and it takes very little effort to get your domain running based on an existing database structure. For simple applications, this is perfectly acceptable (and oftentimes even preferable), but for more … Read more

Syntax to define a NHibernate Filter with Fluent Nhibernate?

If you build Fluent from source, there is now support for filters. You use them like this: First create a class inheriting from FluentNHibernate.Mapping.FilterDefinition: using FluentNHibernate.Mapping; namespace PonyApp.FluentFilters { public class PonyConditionFilter : FilterDefinition { public PonyConditionFilter() { WithName(“PonyConditionFilter”) .AddParameter(“condition”,NHibernate.NHibernateUtil.String); } } } In your ClassMap for your class, use the ApplyFilter method: namespace PonyApp.Entities.Mappings … Read more

How do add NOLOCK with nHibernate?

SetLockMode(LockMode.None) or connection.isolation ReadUncomitted does NOT append a NOLOCK to your queries. Ayende goes into the correct answer on his blog: If you’re using <sql-query> you can do the following: <sql-query name=”PeopleByName”> <return alias=”person” class=”Person”/> SELECT {person.*} FROM People {person} WITH(nolock) WHERE {person}.Name LIKE :name </sql-query> Note the WTIH(nolock) appended to the FROM clause.

How to implement Unit of Work that works with EF and NHibernate

As a side not building solution supporting different provides on top of the linq is way to disaster. Linq and IQueryable are leaky abstractions – each Linq provider can have its own “features” and limitations. Moreover EF itselfs adds some logic via custom extension methods for IQueryable (like Include or AsNoTracking in EFv4.1). These methods … Read more

Why doesn’t Entity Framework support ODBC?

The Entity Framework doesn’t require new data providers, exactly. Rather, it requires Entity Framework providers which extend existing data providers, to provide additional services required by the Entity Framework, such as complex SQL generation, which are not part of the existing ADO.NET data model. I don’t think that there’s anything stopping anyone from writing an … Read more