繁体   English   中英

实体框架代码优先-外键数组

[英]Entity Framework code first - Array of foreign keys

我有一个包含其他实体列表的实体。
其他实体是“只读”的,仅是一个关联。
含义是该实体仅应具有其他实体ID的列表,仅此而已。
我该如何做到这一点,以便以后可以拥有尽可能简单的更新图?

 public class Worker
 {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public string Name { get; set; }

    public double Age { get; set; }

    public int? ManagerID { get; set; }

    [ForeignKey("ManagerID")]
    public virtual Worker Manager { get; set; }

    /// <summary>
    /// This is just an association to the tasks. 
    /// An update graph should ignore the entity itself since 
    /// the task is immutable in this context!!!
    /// </summary>
    public virtual ICollection<Task> AssignedTasks { get; set; }
}

public class Task
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public string TaskName { get; set; }

    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    /// <summary>
    /// This is just an association to the workers. 
    /// An update graph should ignore the entity itself 
    /// since the worker is immutable in this context!!!
    /// </summary>
    public virtual ICollection<Worker> AssignedWorkers { get; set; }
}

我只需要实体ID ...但是集合用于构建数据库架构...

在上面的示例中-在更新时,工作人员只需要更新分配给他的任务ID,而不是其全部数据即可

您可以将实体的实际列表标记为私有虚拟。 这将阻止外部访问。 然后将ID列表创建为对实体列表执行.Select(e => e.Id)的属性。 实体列表只能从父实体内部进行修改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM