0

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 the 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?

1
  • There may be an easier way to do this in EF4, but in our LINQ to SQL we use AutoMapper for things like this. It's also good at taking multiple objects and mashing them into a single DTO, ViewModel, etc. Commented Aug 3, 2011 at 11:38

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I will check it out. It doesn't seem to support alot of stuff. When will EF v4.2 be out?
Nobody knows. Only first CTP is out so it can take 6 months or more.
Then when I need to go and set the state, would I then have to set grantApplication.GrantApplicationState = GrantApplicationState.Applying; and grantApplication.GrantApplicationStateId as well? How would you do it?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.