0

Important note: the column online in my MySQL table is of type enum and I'm not allowed to change the database

So here's my entity:

public enum onlineStatus { online = 1, offline = 0 };
public onlineStatus online { get; set; }

And in my db context:

public class UsersContext : DbContext
{
    public DbSet<User> Users { get; set; }

    public void AddUser(User user) // I'm using a custom membership provider that calls this method
    {
        try
        {
            user.online = 0; // this will give me the word "offline"

            Users.Add(user);
            SaveChanges();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

Somehow user.online = 0; doesn't add to the database. And how do I make it so that it saves the value 0 instead of offline. Thanks in advance.

Teddy
  • 1
  • 3
  • Which EF version you are using? – SBirthare Nov 25 '14 at 08:59
  • Possible duplicate of http://stackoverflow.com/questions/15818622/how-to-map-an-enum-property-in-entity-framework-4 – SBirthare Nov 25 '14 at 09:01
  • Using EF 6 I do believe – Teddy Nov 25 '14 at 09:05
  • From version 5, EF added inherent support for Enum as data type. Did you check this http://msdn.microsoft.com/en-in/data/hh859576.aspx – SBirthare Nov 25 '14 at 09:07
  • Isn't that what I'm doing? – Teddy Nov 25 '14 at 09:21
  • Oh and the column "online" in my table is of type enum (MySQL enum) – Teddy Nov 25 '14 at 09:22
  • I am not sure why you have column type enum, i created a console app with EF6 and used your code all works fine... I see column in DB as int... – SBirthare Nov 25 '14 at 10:04
  • It does not store offline in DB, it store 0 or 1. I can see that in my DB. And the data type in DB is int. I can add the code I wrote if that helps. Something to do with the way you have setup your DB. – SBirthare Nov 25 '14 at 11:44
  • If I change the data type in db to int it works. But I'm not supposed to do any changes to the db. Any solution? – Teddy Nov 25 '14 at 11:56
  • Oh i see... It becomes interesting question then... My suggestion is to highlight this point in your OP. May be someone knows a solution. – SBirthare Nov 25 '14 at 11:59
  • Solution: user.online = Snowy2.Entities.User.onlineStatus.online; will save the value 1 in the database. Can't believe this took me almost a day, lol. – Teddy Nov 25 '14 at 12:18
  • Ah I had done the same in my demo. user.Online = User.OnlineStatus.Online; But thought your column data type is ENUM. – SBirthare Nov 25 '14 at 12:20

0 Answers0