简体   繁体   English

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

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

I have an entity with a list of other entites. 我有一个包含其他实体列表的实体。
The other entities are "read only" and are only a mere association. 其他实体是“只读”的,仅是一个关联。
The meaning is that the entity should only have a list of the other entities ids and nothing more. 含义是该实体仅应具有其他实体ID的列表,仅此而已。
How can I achive this so I can have as simple as possible update graph later on? 我该如何做到这一点,以便以后可以拥有尽可能简单的更新图?

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

I need just the entities ids ... BUT the collections are used to build the DB schema... 我只需要实体ID ...但是集合用于构建数据库架构...

In the example above - On update the worker just need to update the tasks ids that were assigned to him and not their whole data 在上面的示例中-在更新时,工作人员只需要更新分配给他的任务ID,而不是其全部数据即可

You can mark the actual list of entities as private virtual. 您可以将实体的实际列表标记为私有虚拟。 This will prevent outside access. 这将阻止外部访问。 Then create the list of ids as a property that does a .Select(e => e.Id) on your entity list. 然后将ID列表创建为对实体列表执行.Select(e => e.Id)的属性。 The entity list would only be able to be modified from inside the parent entity. 实体列表只能从父实体内部进行修改。

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

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