This project is read-only.

Rehydrating Enums


I have a class that uses an enum as a property type. the objects save just fine to the database which seems to imply the mapping is working but when trying to read the data back from the database I get this error:
An exception of type 'System.InvalidOperationException' occurred in System.Data.Linq.dll but was not handled in user code
Additional information: Bad Storage property: 'accountType' on member 'CookieJar.Domain.Account.AccountType'.
I have another project that uses the same class but uses an actual external XML file for mapping and LinqToSql has no problems rehydrating the object from the database so I know LinqToSql can handle mapping enums automatically.
The property is declared as:
public AccountType AccountType { get; set; }
And is mapped like so:
        Map(x => x.AccountType)
When the database is created the column is called AccountType and is set to INT.
Any ideas? I've exhuasted the internet search engines on this matter.


JeremyS wrote Sep 17, 2010 at 5:36 PM

I am unable to reproduce this. In my tests, enum properties are successfully hydrated. If you could put together a small sample that reproduces the problem and attach it to this issue then I'll be able to investigate further. Thanks.

critchie wrote Sep 17, 2010 at 6:54 PM

I'll put something together as quickly as I can.

"In my tests, ..." Are there enum tests somewhere? I looked over all the tests from the source downlaod and didn't see any enitites using enums.

I figure I'm not getting something right... enum mapping is free right out of the LinqToSql box so I'm not sure why I'm having so much trouble.

I can tell you this for now... it dies on the static method in IDataAccess that tries to find a key name. The call to GetModel() fails. This method is called because of a direct call on FindById().

But I'll play some more and see where it takes me.

critchie wrote Sep 17, 2010 at 9:37 PM

I have it I think. It was a configuration issue. I tried using MapTypesFromAssemblyContaining<Customer> but this doesn't seem to work for me. Working through some of your tests I came across cfg.MappingSource().

When I configure using something like this:
        ActiveRecordConfiguration.Configure(cfg =>
            FluentMappingSource mapping = new FluentMappingSource("LinqToSqlFun");
            string fileName = @"LinqToSqlFun.sdf";
            cfg.ConnectionStringIs("Data Source=" + fileName + ";Persist Security Info=False;");
It works fine.

JeremyS wrote Sep 17, 2010 at 11:18 PM

The ActiveRecord implementation is really experimental...I wouldn't recommend using this in a production app. If you use the 'plain' fluent linq to sql (newing up a FluentMappingSource and using that to create a DataContext rather than using ARConfiguration) then you shouldn't see any issues with this.

The ActiveRecord implementation by default uses a convention-based mappingsource which attempts to infer the type of mapping to used based on the property type and it doesn't look like this gets this right for enum types. I never got around to stabalizing this code as I haven't used L2S for almost a year now and Microsoft have come out with a very similar solution in the form of EF's 'Code first' model.

wrote Feb 13, 2013 at 3:55 AM