I started studying the Entity Framework, trying to understand things that are simple for many people.
I have multiple entities in different tables, but all of these entities have a non-foreign key relationship to the same table containing a list of images. Roughly speaking, using the Owner
field, I want to get a list of images URL’s. How to correctly set up relationships using the Fluent API
to access a list of images using Include()
. Is this possible and am I making any mistakes when designing?
What are the best practices to achieve this task?
public class ImageUrl
{
public Guid Owner { get; set; }
public string Url { get; set; }
public int? SortIndex { get; set; }
}
public class EntityOne
{
[Key]
public Guid Id { get; set; }
public ICollection<ImageUrl> ImagesUrls { get; set; }
}
public class EntityOne
{
[Key]
public Guid Id { get; set; }
public ICollection<ImageUrl> ImagesUrls { get; set; }
}
In SQL this is simple command
SELECT Url, SortIndex
FROM ImageUrl
WHERE Owner = @id
I’m trying to establish a relationship, but I don’t really understand if it’s even possible
e.HasMany(p => p.ImagesUrls)
.WithOne()
.HasPrincipalKey("Owner");
Прохор Гарагуля is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.