首先使用带有Entity Framework 4.1代码的枚举

时间:2011-08-03 11:23:13

标签: ado.net entity-framework-4.1

我首先使用实体​​框架4.1代码。

我有一个GrantApplication课程:

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

GrantApplicationState是一个枚举,看起来像这样:

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

在我去添加授权应用程序数据库之前,我设置了授权应用程序状态:

public void Insert(GrantApplication grantApplication)
{
   // Set the current state to applying
   grantApplication.GrantApplicationState = GrantApplicationState.Applying;

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

在我的数据库中,我有一个GrantApplication表,其中GrantApplicationStateId链接到GrantApplicationState表。

如何让EF将状态ID从GrantApplication.GrantApplicationState添加到GrantApplicationStateId列?这可能吗?当我检索GrantApplication对象时,它也需要设置。这是这样做的方法还是我必须在我的GrantApplication类中创建另一个名为GrantApplicationStateId的属性?

2 个答案:

答案 0 :(得分:3)

您必须创建另一个属性:

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根本不支持枚举 - 您无法映射它们。这将在EFv4.2中发生变化。

答案 1 :(得分:1)

仍然EF不支持Enums ..它将在EF 5.0上。检查我的尝试在这里 http://the--semicolon.blogspot.com/p/handling-enum-in-code-first-entity.html