简体   繁体   中英

What is the best way to assign object with enumeration type in entity framework?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM