0

i am trying to pass a dropdown selected value to database my datacontracts has the following code

      public EnumTypes.RegardingObjectType RegardingObjectType { get; set; }
    public Guid RegardingObjectId { get; set; }

enumtypes.cs has the following

    public class EnumTypes
{

    public enum RegardingObjectType
    {
        UnknownOrNone = 0,
        Account = 1,
        Cellsite = 2,

eventdal.cs has the following

       private t_Event MapEventToEntity(Event newevent, t_Event eventToBeChanged)
    {
   eventToBeChanged.RegardingObjectType = int.Parse(newevent.RegardingObjectType.ToString());

it builds fine but getting exception while running

user2167089
  • 151
  • 6
  • 20

3 Answers3

1

Entity Framework 4 does not support using enums as column types. If you want to have native support for enums, you'll need to upgrade to Entity Framework 5.

If you absolutely have to use EF 4 (because of a project that's already been implemented in it), then I recommend that you go around it as so:

public int RegardingObjectTypeAsInt { get; set; }

[NotMapped]
public EnumTypes.RegardingObjectType RegardingObjectType
{
  get { return (EnumTypes.RegardingObjectType) this.RegardingObjectTypeAsInt; }
  set { this.RegardingObjectTypeAsInt = (int)value; }
}

It's not a pretty solution, but it's a solid workaround to the problem, and you still get to bind to it on your pages and treat it as the enum itself in your other code.

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
0

this worked

eventToBeChanged.RegardingObjectType = (int)newevent.RegardingObjectType;

instead of

  eventToBeChanged.RegardingObjectType = int.Parse(newevent.RegardingObjectType.ToString());
user2167089
  • 151
  • 6
  • 20
0

If you want to map the string value to the database, check out this answer:

How to map an enum property in Entity Framework 4

As @IronMan84 pointed out though, I'm not sure if this works with multi-word enums - but it's useful and it works none-the-less.

Community
  • 1
  • 1
MattSull
  • 5,514
  • 5
  • 46
  • 68