4

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

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

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

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

4

2 回答 2

2

使它们可以为空属性

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);
于 2013-01-26T06:06:05.753 回答
2

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

于 2014-12-04T01:03:46.400 回答