简体   繁体   English

具有共享对象表的实体框架导航

[英]Entity Framework navigation with shared object table

I have Projects,Tasks, and Work objects. 我有项目,任务和工作对象。 I have an object Tag that can linked to any of these. 我有一个可以链接到其中任何一个的对象标签。 Any on of these objects could have multiple tags associated with them. 这些对象中的任何一个都可以具有与其关联的多个标签。 The tag object has TagId, TypeId, RelationId. 标签对象具有TagId,TypeId,RelationId。

The RelationId is what points to the linked object and the type indicates which type to link to. RelationId是指向链接对象的对象,类型指示要链接到的类型。 How can I do this in a navigation property for each object so I do not pull the wrong tag objects. 如何在每个对象的导航属性中执行此操作,以免出现错误的标记对象。 I know I can use Linq to do this Where(ProjectId == RelationId && TypeId == 1) , but this only works if I am writing queries and doesn't allow for navigation properties. 我知道我可以使用Linq来执行Where(ProjectId == RelationId && TypeId == 1) ,但这仅在我编写查询且不允许导航属性的情况下有效。

You entity structure should be something like this: 您的实体结构应如下所示:

class Projects
{
    public int ProjectId { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

class Tasks
{
    public int TaskId { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

class Tag
{
    public int TagId { get; set; }
    public int TypeId { get; set; }
    public int RelationId { get; set; }
    public virtual Projects Project { get; set; }
    public virtual Tasks Task { get; set; }
}

This is called an independent association in Entity Framework https://msdn.microsoft.com/en-gb/data/jj713564.aspx 这在实体框架https://msdn.microsoft.com/zh-cn/data/jj713564.aspx中称为独立关联

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

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