带导航功能的EF 5 ENUM

时间:2013-05-23 16:30:05

标签: enums entity-framework-5 entity-relationship

我正在尝试使用EF 5和dot.NET 4.5的Enum支持。

我的情况如下。

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 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; }
    }

使用上面列出的代码不使用ENUM,世界很高兴。

当我尝试添加枚举时,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; }
    }

这在保存期间不起作用,因为它不喜欢ForeignKey属性。保存期间将抛出此错误。

指定的架构无效。错误:

(129,6):错误0112:引用约束的从属角色中的所有属性的类型必须与主体角色中的相应属性类型相同。实体'tbl_pp_Message'上的属性'PriorityId'的类型与引用约束'tbl_pp_MessagePriority_tbl_pp_Message'中实体'tbl_pp_MessagePriority'上的属性'MessagePriorityId'的类型不匹配。

如果从tbl_pp_Message POCO中删除了tbl_pp_MessagePriority的导航。导航从tbl_pp_MessagePriority POCO中删除回到tbl_pp_Message然后它可以工作。 删除导航后,它将生成一个这样的SQL语句。

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()

如果没有将tbl_pp_MessagePriority_MessagePriorityId添加到插入中,那就没问题了。

在使用ENUM支持时,可以做些什么来保持导航在EF代码中?是否有DataAnnotation或Mapping API?

请发布您的任何建议。该项目未使用EDMX / T4模板。

工作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; }
    }

如果不需要导航,工作POCO就可以了。

1 个答案:

答案 0 :(得分:3)

我缺少的是将查找表主键设置为相同的ENUM类型而不是int。

使用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; }
}