【问题标题】:EF 5 ENUM with Navigation带导航功能的 EF 5 ENUM
【发布时间】:2013-05-23 16:30:04
【问题描述】:

我正在尝试将 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; }
    }

如果上面列出的代码不使用 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: 引用约束的 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 就可以了。

【问题讨论】:

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


    【解决方案1】:

    我缺少的是将查找表主键设置为相同的 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; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多