Guid in Storage, but String in Conceptual
Posted: Wed 06 Jul 2011 16:41
Hello there,
We use Guids (RAW(16)) as primary keys for our tables, so far so good. EF4 also supports Guids properly.
However we are not satisfied with System.Guid type being a struct. Our classes have many relations which are optional, so this is effective waste of memory even if field is Nullable. We have many dictionaries which use Guid as a key, those dictionaries occupy lot of memory holding basically a copy of all Guids used as primary keys. If some wise guy from MS would make Guid a reference type, this would save a lots of memory and processing power.
Now add in this play WCF Data Services, where you cannot modify your EF-generated classes (because of hard-coded metadata publishing mechanism). So these Guids basically coming down to the Silverlight client, causing there the same memory and performance problems.
So my idea was to replace all Guids with String types in conceptual model, but to leave Guids in Storage model. Unfortunately, EF Designer complaints that this mapping is not possible.
Is it possible to make such a mapping possible?
I've done some tests: if EF doesn't really know that column is RAW(16), instead of VARCHAR2(32), saving and querying work fine. Trick is to write base64 encoded version of the guid in string field.
However, Guids loaded as string back look like 8 chinese symbols - no base64 decoding is taking place.
Any ideas how to get rid of Guids in conceptual model, but preserve them in DB?
Thank you in advance,
Alladin
We use Guids (RAW(16)) as primary keys for our tables, so far so good. EF4 also supports Guids properly.
However we are not satisfied with System.Guid type being a struct. Our classes have many relations which are optional, so this is effective waste of memory even if field is Nullable. We have many dictionaries which use Guid as a key, those dictionaries occupy lot of memory holding basically a copy of all Guids used as primary keys. If some wise guy from MS would make Guid a reference type, this would save a lots of memory and processing power.
Now add in this play WCF Data Services, where you cannot modify your EF-generated classes (because of hard-coded metadata publishing mechanism). So these Guids basically coming down to the Silverlight client, causing there the same memory and performance problems.
So my idea was to replace all Guids with String types in conceptual model, but to leave Guids in Storage model. Unfortunately, EF Designer complaints that this mapping is not possible.
Is it possible to make such a mapping possible?
I've done some tests: if EF doesn't really know that column is RAW(16), instead of VARCHAR2(32), saving and querying work fine. Trick is to write base64 encoded version of the guid in string field.
However, Guids loaded as string back look like 8 chinese symbols - no base64 decoding is taking place.
Any ideas how to get rid of Guids in conceptual model, but preserve them in DB?
Thank you in advance,
Alladin