0

I am fairly new to entity framework and I want to know what is the best approach to assign enumeration field to an object.

I want to write:

myObject.Status = Status.Active;

Shall I do:

myObject.Status = _context.myObjects.First(x=>x.Status.StatusId == Status.ActiveId);

and define

public partial class Status
{
     public const int ActiveId = 1;
}

or can I do something like:

public partial class Status
{
     public static Status Active = new Status(1, "Active");
}

which works out as

myObject.Status = Status.Active;

Or 3rd option can be just to forgot about mapping status into entity framework and just use Id on domain objects

myObject.StatusId = Status.Active.Id;

Can you let me know what is the best practice or simply what do you prefer yourself?

Thanks

Marek
  • 2,419
  • 6
  • 34
  • 38

1 Answers1

1

I've answered a question regarding enum here: How do I map a column to a complex type in EF4 using code first CTP5?

this would allow you to use a complex type to map against an enumeration and is the way i prefer it.

Hope this helps.

Community
  • 1
  • 1
Joakim
  • 2,217
  • 15
  • 20