1

I have a model like this:

namespace Ad.NegCred.Data.Model {
    public enum DataKind {
        F, //Takibe alınıp henüz tahsil edilmeyen ferdi kredi bildirimi
        FA, //Aynı dönemde takibe alınan ve tahsil edilen ferdi kredi bildirimi
        FF, //daha önceki dönemlerde takibe alındığı bildirilmiş ferdi kredi tahsil bildirimi
        K, //Takibe alınıp henüz tahsil edilmeyan kredi kartı
        KA, //Aynı dönemde takibe alınan ve tahsil edilen kredi kartı
        KF //Daha önceki dönemlerde takibe alındığı bildirilmiş kredi kartı tahsil    bildirimi
    }

    public class Datum {
        [Key]
        public long Id { get; set; }
        public DataKind Kind { get; set; }
        [StringLength(25, MinimumLength = 2)]
        public string Name { get; set; }
    }
}

When database created by EF, all columns but the Kind column are created. Another enum column which is part of a complex type not shown here is not created as well.

Why does EF 5 behave like this? Is there a setting that I do not know about?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
koducu
  • 85
  • 3
  • 7
  • This almost feels like EF4 behavior. What is the version of EntityFramework.dll you are using? Don't you target .NET Framework 4 by any chance? – Pawel Feb 03 '13 at 06:50
  • @Pawel- Sorry for late return. Properties page accessed from Solution Explorer shows Runtime version v4.0.30319, Version 4.4.0.0 and the Propertes page accessed from Windows explorer shows File Varsion 4.4.20627.0, Product Version 5.0.0.net40. You may be correct. I'll check it. – koducu Feb 04 '13 at 08:45
  • v4.4.0.0 means you are using EF5 but targeting .NET Framework 4. This is why you don't get enum types - System.Data.Entity.dll in .NET Framework 4 that is used by EntityFramework.dll don't know how to handle them. – Pawel Feb 04 '13 at 18:29

4 Answers4

7

You seem to be using EF5 and target .NET Framework 4. This won't work since System.Data.Entity.dll in .NET Framework 4 used by the EntityFrameork.dll cannot handle enums. You either need to move to EF5 and .NET Framework 4.5 or to EF6 where you can target .NET Framework 4 and use enums since EF6 doesn't depend on components shipped in .NET Framework anymore.

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • Thank you very much for your accurate answer Pawel. I am new to EF5 and haven't come across an explanation that mentions enum problem of EF5 on .Net 4. All of them advertises that EF5 handles enums. – koducu Feb 05 '13 at 06:04
0

Should work Ok, see MS EF site http://msdn.microsoft.com/en-us/data/hh859576

I use this and it works... and Enum is not created as TABLE on DB. The video shows you how to create ENUM on DB if that is required.

namespace XYZ.Core.Enum
{
public enum MasterEventType : int
{
    Undefined = 0,  
    Logon = 1,
    Logoff = 2,   
 }
}

namespace XYZ.Core.Model
{
public class MasterUserEventLog  
{
    // the rest is not relevant here....
    public MasterEventType EventType{ get; set; }       // what happened
}
{
phil soady
  • 11,043
  • 5
  • 50
  • 95
0

No table will be created by EF and that is by design. EF handles the enums internally. You don't have to worry about them. I have used Enum and they work fine. If you however want to explicitly store Enum in the database then I suggest creating rolling in your own code. However, you should note that making entities has the usual ceremony associated with it for enum as well.

You can visit the link http://msdn.microsoft.com/en-us/data/hh859576.aspx where it is clearly written that Ef does not create any table for the Enum.

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
0

In EF 6 you will also find this behaviour occurs if you make your enum type derive from uint rather than the default int. e.g.

public enum LogStatus : uint

The above will not automatically generate a column in code first migrations so you have to drop the uint part:

public enum LogStatus

Which defaults to int basis and code-first is happy to create this column automatically.

The Senator
  • 5,181
  • 2
  • 34
  • 49