2011-05-20 104 views
2

我試圖將存在於我的模型中的枚舉保存到數據庫中,但每次我都做Entity Framwework抱怨沒有與Enum關聯的查找表。我不想查找表,我只想枚舉存在於代碼中並作爲整數存儲在數據庫中。同時節省 實體不公開他們的關係 國外 鍵性質發生實體框架僅作爲POCO枚舉

錯誤。 EntityEntries 屬性將返回null,因爲 單個實體不能被標識爲 異常的來源。處理 的異常,而通過在您的實體類型中公開外鍵 屬性,可以使 更容易保存。有關詳細信息,請參閱 InnerException。

「無法插入NULL值插入 列 '類型',表 'Test.dbo.Interests';列 不允許爲空INSERT 失敗\ r \ n此語句已終止

的類型的值是絕對不爲空,EF會把它這樣導致它無法找到我不想在第一時間對外鍵表。

如何獲得實體框架將我的枚舉視爲一個int然後轉換當我調用數據庫來檢索我的模型時退出?

+0

可能重複的[如何在實體框架中使用Enums?](http://stackoverflow.com/questions/1526339/how-to-work-with-enums-in-entity-framework) – 2011-05-20 14:49:37

+0

如某些陳述這不是EF的支持功能,請轉到http://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions/suggestions/1012609-support-enums-as-property-types- on-entities?ref =標題和投票將包含在將來的版本 – 2011-05-20 14:57:07

回答

6

實體框架根本不支持枚舉,因此您不能將枚舉屬性映射到數據庫。你必須使用int屬性,如果你想要枚舉,你必須定義另一個未映射的屬性(在自動生成的實體的情況下,你必須在你的部分類中定義它),它將int轉換爲枚舉。

編輯: 在以後的版本中增加了Enum支持。如果你想映射枚舉(整數值,而不是字符串),你需要使用.NET 4.5和EF5或EF6.x和.NET 4.x.

+0

Enums已被討論爲未來版本:http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef -power-tools-ctp1-released.aspx?CommentPosted = true#commentmessage – 2011-05-20 14:56:03

+0

查看最新版本的實體框架中的enum支持。 – juFo 2014-03-20 13:10:02

+0

@juFo:當然,但這是2011年的答案,其中不支持枚舉。在線程中還有另外一個答案,在.NET 4.5 + EF5中添加了枚舉支持。 – 2014-03-20 13:22:39

2

我發現這樣做,它的工作原理,它只是不直接的Enum。您必須創建一個可以隱式映射來回映射的外觀類。它可以工作,但不像我希望的那樣順利。

http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/

public enum InterestTypes 
{ 
    [Description("Attraction - for sightseers and adventurers.")] Attraction = 1, 
    [Description("Event - fun for everyone (limited time).")] Event = 2, 
    [Description("Recreation - parks, hiking, movies...")] Recreation = 3, 
    [Description("Restaurant - good eats.")] Restaurant = 4 
} 

public class InterestType 
{ 
    private InterestType() 
     : this(default(InterestTypes)) 
    { 
    } 

    public InterestType(int value) 
    { 
     Value = value; 
    } 

    public InterestType(InterestTypes type) 
    { 
     Value = (int) type; 
    } 

    public int Value { get; private set; } 

    public static implicit operator int(InterestType type) 
    { 
     return type.Value; 
    } 

    public static implicit operator InterestType(int value) 
    { 
     return new InterestType(value); 
    } 

    public static implicit operator InterestTypes(InterestType type) 
    { 
     return (InterestTypes) type.Value; 
    } 

    public static implicit operator InterestType(InterestTypes type) 
    { 
     return new InterestType(type); 
    } 
} 

在你的DataContext你需要在OnModelCreating方法如下。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.ComplexType<InterestType>() 
      .Property(o => o.Value) 
      .HasColumnName("Type"); 
    } 

這告訴實體框架將int值映射到數據庫中的值。我想你也可以使用字符串,如果你想將值作爲字符串存儲在數據庫中。

0

在我的項目中,我將枚舉映射到數據庫中的整數列,並限制對POCO模型中屬性的訪問。就像下面的例子。

public enum MyEnum 
{ 
    EnumProp1, 
    EnumProp2 
} 

public class MyEntity 
{ 
    public long Id{get;set;} 

    [Column("MyEnum")] 
    public int IdMyEnum{ get;protected set;} 

    [NotMapped] 
    public MyEnum 
    { 
     get{ return (MyEnum)this.IdMyEnum; } 
     set{ this.IdMyEnum = (int)value; } 
    } 
} 

希望它有助於!