简体   繁体   中英

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.

The RelationId is what points to the linked object and the type indicates which type to link to. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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