2013-05-23 2 views
1

EF 5 및 dot.NET 4.5에서 Enum 지원을 사용하려고합니다.EF 5 ENUM with navigation

내가 가진 상황은 다음과 같습니다.

POCO : 세상이 행복 인 ENUM을 사용하지 위에 나열된 코드와

public partial class tbl_pp_Message 

    { 
     public tbl_pp_Message() 
     { 
      this.tbl_pp_MessageDistribution = new List<tbl_pp_MessageDistribution>(); 
     } 

     public int MessageId { get; set; } 
     public string Subject { get; set; } 
     public string From { get; set; } 
     public string Body { get; set; } 
     public int PriorityId { get; set; } 
     public System.DateTime CreateDate { get; set; } 
     public int CreateById { get; set; } 

     [ForeignKey("PriorityId")] 
     public virtual tbl_pp_MessagePriority tbl_pp_MessagePriority { get; set; } 
     public virtual ICollection<tbl_pp_MessageDistribution> tbl_pp_MessageDistribution { get; set; } 
    } 





public partial class tbl_pp_MessagePriority 

    { 
     public tbl_pp_MessagePriority() 
     { 
      this.tbl_pp_Message = new List<tbl_pp_Message>(); 
     } 

     public int MessagePriorityId { get; set; } 
     public string Description { get; set; } 
     public Nullable<int> DisplayOrder { get; set; } 
     public bool ShowAlert { get; set; } 

     public virtual ICollection<tbl_pp_Message> tbl_pp_Message { get; set; } 
    } 

.

열거 형을 추가하려고하면 POCO가 다음과 같이 보입니다. 는 외래 키 속성을 좋아하지 않기 때문에

[Flags] 
    public enum MessagePriorityEnum : int 

    { 
     NONE = 0, 
     Urgent = 1, 
     Normal = 2, 
     Low = 3 
    } 

public partial class tbl_pp_Message 

    { 
     public tbl_pp_Message() 
     { 
      this.tbl_pp_MessageDistribution = new List<tbl_pp_MessageDistribution>(); 
     } 

     public int MessageId { get; set; } 
     public string Subject { get; set; } 
     public string From { get; set; } 
     public string Body { get; set; } 
     public MessagePriorityEnum PriorityId { get; set; } 
     public System.DateTime CreateDate { get; set; } 
     public int CreateById { get; set; } 

     [ForeignKey("PriorityId")] 
     public virtual tbl_pp_MessagePriority tbl_pp_MessagePriority { get; set; } 
     public virtual ICollection<tbl_pp_MessageDistribution> tbl_pp_MessageDistribution { get; set; } 
    } 

는 저장 중에 작동하지 않습니다. 이 오류는 저장 중에 발생합니다.

지정한 스키마가 유효하지 않습니다. 오류 :

(129,6) : 오류 0112 : 참조 제한 조건의 종속 역할에있는 모든 특성 유형이 주 속성의 해당 특성 유형과 같아야합니다. 엔티티 'tbl_pp_Message'의 'PriorityId'속성 유형이 참조 제약 'tbl_pp_MessagePriority_tbl_pp_Message'의 엔티티 'tbl_pp_MessagePriority'의 'MessagePriorityId'속성 유형과 일치하지 않습니다.

tbl_pp_MessagePriority에 대한 탐색이 tbl_pp_Message POCO에서 제거 된 경우. 그리고 네비게이션이 tbl_pp_MessagePriority POCO에서 tbl_pp_Message로 다시 제거되면 작동합니다. 탐색 기능이 제거 된 상태에서 이와 같은 SQL 문이 생성됩니다. tbl_pp_MessagePriority_MessagePriorityId가 삽입에 추가되지 않은 경우

insert [dbo].[tbl_pp_Message] 
     ([Subject], 
     [From], 
     [Body], 
     [PriorityId], 
     [CreateDate], 
     [CreateById], 
     [tbl_pp_MessagePriority_MessagePriorityId]) 
values ('Test ENUM EF Code First 5.0' /* @0 */, 
     'Daniel.Bivens' /* @1 */, 
     'Test 01 Enum Body - The Magic Unicorn Edition' /* @2 */, 
     2 /* @3 */, 
     '2013-05-23T10:52:09' /* @4 */, 
     5 /* @5 */, 
     null) 


select [MessageId] 
from [dbo].[tbl_pp_Message] 
where @@ROWCOUNT > 0 
     and [MessageId] = scope_identity() 

그것은 확인 될 것입니다.

ENUM 지원을 사용하는 동안 EF 코드에서 탐색을 유지하려면 어떻게해야합니까? DataAnnotation 또는 Mapping API가 있습니까?

제안 사항을 게시 해주십시오. 이 프로젝트는 EDMX/T4 템플릿을 사용하고 있지 않습니다.

작업 POCO : 더 탐색이 필요하지 않은 경우 작업 POCO 괜찮을 것

public partial class tbl_pp_Message 

    { 
     //public tbl_pp_Message() 
     //{ 
     // this.tbl_pp_MessageDistribution = new List<tbl_pp_MessageDistribution>(); 
     //} 

     public int MessageId { get; set; } 
     public string Subject { get; set; } 
     public string From { get; set; } 
     public string Body { get; set; } 
     public MessagePriorityEnum PriorityId { get; set; } 
     public System.DateTime CreateDate { get; set; } 
     public int CreateById { get; set; } 

     //[ForeignKey("PriorityId")] 
     //public virtual tbl_pp_MessagePriority tbl_pp_MessagePriority { get; set; } 
     //public virtual ICollection<tbl_pp_MessageDistribution> tbl_pp_MessageDistribution { get; set; } 
    } 




public partial class tbl_pp_MessagePriority 

    { 
     //public tbl_pp_MessagePriority() 
     //{ 
     // this.tbl_pp_Message = new List<tbl_pp_Message>(); 
     //} 

     public int MessagePriorityId { get; set; } 
     public string Description { get; set; } 
     public Nullable<int> DisplayOrder { get; set; } 
     public bool ShowAlert { get; set; } 

     //public virtual ICollection<tbl_pp_Message> tbl_pp_Message { get; set; } 
    } 

.

답변

3

실종 된 점은 조회 테이블 기본 키를 int 대신 동일한 ENUM 유형으로 설정하는 것이 었습니다.

ENUM 및 탐색 기능이있는 작업 POCO.

public partial class tbl_pp_Message 
    { 
    public tbl_pp_Message() 
    { 
     this.tbl_pp_MessageAction = new List<tbl_pp_MessageAction>(); 
     this.tbl_pp_MessageAttachment = new List<tbl_pp_MessageAttachment>(); 
     this.tbl_pp_MessageDistribution = new List<tbl_pp_MessageDistribution>(); 
    } 

    public int MessageId { get; set; } 
    public string Subject { get; set; } 
    public string From { get; set; } 
    public string Body { get; set; } 
    public MessageTypeEnum TypeId { get; set; } 
    public System.DateTime CreateDate { get; set; } 
    public int CreateById { get; set; } 

    [ForeignKey("TypeId")] 
    public virtual tbl_pp_MessageType tbl_pp_MessageType { get; set; } 
    public virtual ICollection<tbl_pp_MessageAction> tbl_pp_MessageAction { get; set; } 
    public virtual ICollection<tbl_pp_MessageAttachment> tbl_pp_MessageAttachment { get; set; } 
    public virtual ICollection<tbl_pp_MessageDistribution> tbl_pp_MessageDistribution { get; set; } 
} 




public partial class tbl_pp_MessageType 
    { 
    public tbl_pp_MessageType() 
    { 
     this.tbl_pp_Message = new List<tbl_pp_Message>(); 
    } 

    [Key] 
    public MessageTypeEnum MessageTypeId { get; set; } 
    public string Description { get; set; } 
    public Nullable<int> DisplayOrder { get; set; } 
    public bool ShowAlert { get; set; } 

    public virtual ICollection<tbl_pp_Message> tbl_pp_Message { get; set; } 
}