1

我正在尝试将 Enum 支持与 EF 5 和 dot.NET 4.5 一起使用。

我的情况如下。

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

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

当我尝试添加枚举时,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: 引用约束的 Dependent Role 中所有属性的类型必须与 Principal Role 中相应的属性类型相同。实体“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 语句。

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 就可以了。

4

1 回答 1

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; }
}
于 2013-05-30T17:15:10.447 回答