julealgon
julealgon

Reputation: 8182

Storing an Exception object in Entity Framework

We are looking at adding an Exception property to one of our models that is currently mapped using Entity Framework code first.

Is there a way to map a property of type Exception directly somehow, perhaps by making it binary serialized in the database, thus maintaining a type-safe model class?

I assume I could make this work somehow by implementing two properties in the model, for instance:

public Exception Exception {get; set;}
internal byte[] SerializedException {get; set;}

Then, I would mark the real exception property as Ignore on the modelbuilder inside the context and let EF use the binary version. This is obviously very clumsy, as I would need to implement the serialization/deserialization in the getter/setter for the real exception and, as I said, I'm only assuming it's possible, but have not yet tested it.

I'm very curious if it is somehow possible to implement this using the type directly.

Upvotes: 0

Views: 117

Answers (1)

Tim Copenhaver
Tim Copenhaver

Reputation: 3302

Unfortunately, I don't believe there is any way to do what you want with EF. The normal solution is to use the internal property like you mentioned above - just have Exception deserialize from the backing property the first time it is accessed.

It's not pretty, but EF does not expose an extension point for plugging in custom type converters between the POCO and the database, so it's the best we can do.

Upvotes: 1

Related Questions