簡體   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