views:

281

answers:

5

Hello, everyone!

Is there are an easy way to store some of entitie's properties in a column as a bulk, as XML or something? Querieng by those properties of course is not an option, but it still'd be valuble to be able to extend data model without database migration.

+1  A: 

Not in NHibernate. I don't know about Entity framework.

You still need a database migration to store the XML field, so it won't prevent you having to do a database schema update when you find this feature in Entity framework or some other framework.

Paco
+2  A: 

Hi!

As far as I know you are not able to do this directly with NHibernate, but you could implement a private property which composes and decomposes your fields to a string and map to that property instead of mapping your fields directly.

But I am not sure if this really something you should do in the first place. Usually requirements tend to come up during development and lifetime of an application and once you are going to need one of the fields - even if you now think you will never need to query for that field - you will have a hard time. Adding a column to a table of an existing database is not much of a deal and you still need to update the xml for every tuple in the table, so I really think it is better to store only one field in a column.

Best Regards,
Oliver Hanappi

Oliver Hanappi
Thank's Oliver. We've thought about that, but it'll be nice to have some kind of ORM extention insted of base class for all entities.We designing read-only data model to be used by our web servers. So it's pretty mutch defined what filters will be used, but it's everchanging what data we will show.
Artem Tikhomirov
A: 

You could try converting the xml to a string before you store it in the database.

The Entity Framework does not support a native-XML data type. This means that when an entity is mapped to a table with an XML column, the equivalent entity property for the XML column is a string. Objects can be disconnected and serialized as XML. For more information, see Serializing Objects (Entity Framework).

http://msdn.microsoft.com/en-us/library/cc716791.aspx

Shiraz Bhaiji
A: 

Yes, you can do this in NHibernate using a serializable object and an IUserType implementation. This link describes how to create an IUserType implementation for a SQL Server XML field and this link describes how to build on that to serialize an object to an XML field.

Jamie Ide
Thank's Jamie. As I've mentioned, I want to store several properties in a single column. So what will happen, if several properties mapped using this IUserType to the same column will change at the same time? As far as I understand this API the last change applied will override the rest...
Artem Tikhomirov
I would map the properties that will be persisted as XML as a properties of a component object. So your IUserType would take the component object and serialize (NullSafeSet) and de-serialize (NullSafeGet) the component object. I haven't actually done this -- I had a use case for it and looked into enough to know that it could be done but ended up with a different solution. Unfortunately I can only point you in the (hopefully) right direction.
Jamie Ide
+2  A: 

For NHibernate you can use dynamic-component nhforge.org/doc/nh/en/index.html#components-dynamic or using the dictionary as a name-value list nhforge.org/doc/nh/en/index.html#collections-mapping or even Duck-Typing http://fabiomaulo.blogspot.com/2009/07/duck-typing-with-nhibernate.html

Fabio Maulo