【问题标题】:Data model for User Notifications of many Types多种类型的用户通知的数据模型
【发布时间】:2014-12-04 09:03:46
【问题描述】:

我试图弄清楚如何为通知建模。这是我尝试过的。

public class NotificationType
{
    public int NotificationTypeID { get; set; }
    public string Type { get; set; }
    public string Action { get; set; }
}

public class Notification
{
    public int NotificationID { get; set; }
    public bool ReadStatus { get; set; }
    public DateTime DateCreated { get; set; }
    [ForeignKey("User")]
    public int UserID { get; set; }
    [ForeignKey("NotificationType")]
    public int NotificationTypeID { get; set; }

    public int CommentID { get; set; }
    public int ProjectID { get; set; }

    public Comment Comment { get; set; }
    public Project Project { get; set; }
    public NotificationType NotificationType { get; set; }
    public User User { get; set; }
}

坦率地说,我觉得我仍然不知道自己在做什么,但是让我告诉你我想要做什么,如果这是一个坏主意,请告诉我,如果不是,请告诉我我是怎么做的。

我有Notifications 用于与CommentReplyProject 相关的操作 - 这就是我拥有所有这些导航属性和字段的原因。我想基本上使用NotificationType 来确定它是什么,然后使用适当的 ID 字段来获取信息以向用户显示通知。

第一个问题,我似乎无法将这些 ID (CommentID ProjectID ) 字段设为 Nullable,因此我不必总是拥有它们。

如何使用数据注释和/或流畅的 api 使它们可以为空,或者更好地设计我的模型?

【问题讨论】:

    标签: entity-framework ef-code-first data-modeling


    【解决方案1】:

    使它们可以为空属性

    public class Notification
    {
        public int NotificationID { get; set; }
        public bool ReadStatus { get; set; }
        public DateTime DateCreated { get; set; }
        [ForeignKey("User")]
        public int UserID { get; set; }
        [ForeignKey("NotificationType")]
        public int NotificationTypeID { get; set; }
    
        public int? CommentID { get; set; }
        public int? ProjectID { get; set; }
    
        public Comment Comment { get; set; }
        public Project Project { get; set; }
        public NotificationType NotificationType { get; set; }
        public User User { get; set; }
    }
    

    然后使用fluent API

    modelBuilder.Entity<Notification>().HasOptional(n => n.Comment).WithMany()
        .HasForeignKey(n => n.CommentID);
    

    【讨论】:

      【解决方案2】:

      我发现这是一种复杂但完整的通知系统:http://library.blackboard.com/ref/df5b20ed-ce8d-4428-a595-a0091b23dda3/Content/Mini_TOCs/mt_admin_app_system_manage_comm_notify_frame.htm 我认为可以从中借鉴很多好的想法。

      【讨论】:

        猜你喜欢
        • 2011-04-26
        • 2012-07-16
        • 2013-09-06
        • 1970-01-01
        • 2014-02-15
        • 2012-12-04
        • 2015-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多