开发者

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.Firs开发者_Go百科t(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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜