Enum is stored correctly in DB but is not deserialized to type correctly in C #
I have an enum:
public enum EnumSample{
Setup = 1,
Pending = 7
}
I have an object (I am using EF) stored in a database with a property EnumSample
set to EnumSample.Pending
. The database reflects this value as 7
if it were a query. However, when I get the same object from C # (using EF), I get the wrong type mapping (I get Setup
when I should get Pending
). All identifiers are verified. Screenshot below. What could be causing this?
UPDATE
Restarting the application gives me what I expect - the problem is that the dbContext instance is bound between requests. I can do using var db = new AppContext
and it works great.
I have the following settings on mine UnityContainer
:
container.RegisterType<AppContext>(new ContainerControlledLifetimeManager());
I have tried various different lifetime managers - PerHttpRequestLifetimeManager
does not resolve other guidelines I have seen.
An example of a service level method:
public class EventService : BaseService
{
public readonly AppContext _db;
private readonly NotificationService _notificationService;
public EventService(AppContext db, NotificationService notificationService)
{
_db = db;
_notificationService = notificationService;
}
The behavior I need is that EventService
both NotificationService
have the same instance AppContext
, but between requests the context is located and I get a new state / new instance.
source to share
PerThreadLifetimeManager
does not guarantee that every request will be served by a different thread. Unity does not have a timeout manager by default. You need to install the package NuGet Unity.MVC , which has built a new life-time manager: PerRequestLifetimeManager
.
@ SB2055 See this answer for details .
source to share