开发者

Using enums with Entity Framework 4.1 code first

开发者 https://www.devze.com 2023-03-25 03:21 出处:网络
I am using entity framework 4.1 code first. I have a GrantApplication class: public class GrantApplication

I am using entity framework 4.1 code first.

I have a GrantApplication class:

public class GrantApplication
{
   // Just some of the properties are listed
   public int Id { get; set; }
   public GrantApplicationState GrantApplicationState { get; set; }
}

GrantApplicationState is an enum and looks like this:

public enum GrantApplicationState
{
   Applying = 1,
   Submitted = 2,
   cknowledged = 3
}

Just before I go and add the grant application the database I set the grant application state:

public void Insert(GrantApplication grantApplication)
{
   // Set t开发者_运维问答he current state to applying
   grantApplication.GrantApplicationState = GrantApplicationState.Applying;

   // Insert the new grant application
   grantApplicationRepository.Insert(grantApplication);
}

In my database I have a GrantApplication table with a GrantApplicationStateId that links to a GrantApplicationState table.

How do I get EF to add the state id from GrantApplication.GrantApplicationState to the GrantApplicationStateId column? Is this possible? And when I retrieve the GrantApplication object then it will need to be set as well. Is this the way to do it or do I have to create another property in my GrantApplication class called GrantApplicationStateId?


You must create another property:

public class GrantApplication
{

   public int Id { get; set; }
   ...

   public int GrantApplicationStateId { get; set; }

   [NotMapped] // Perhaps not need
   public GrantApplicationState GrantApplicationState 
   { 
       get { return (GrantApplicationState)GrantApplicationStateId; }
       set { GrantApplicationStateId = (int)value; }
   }
}

EFv4.1 doesn't support enums at all - you cannot map them. This will change in EFv4.2.


Still EF not support for Enums.. it will be on EF 5.0..check my try on here http://the--semicolon.blogspot.com/p/handling-enum-in-code-first-entity.html

0

精彩评论

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