简体   繁体   中英

How do I create linked X entities for a X entity using Entity Framework Core 2.0

I want create a Product entity and that entity can have list of Products as LinkedProducts but I don't know why my codes not working. I'm student and very newbie to EF core mapping.
I created 2 Entity Classes Product.cs and ProductLink.cs, here is my classes

Product.cs

    public class Product : Content
    {
        public virtual string Description { get; set; }

        public virtual Guid? ThumbnailImageId { get; set; }

        [ForeignKey("ThumbnailImageId")]
        public virtual Media ThumbnailImage { get; set; }

        public virtual List<ProductLink> ProductLinks { get; set; } = new List<ProductLink>();
    }

ProductLink.cs

    public class ProductLink : EntityBase
    {
        [ForeignKey("ProductId")]
        public virtual Product Product { get; set; }

        public virtual Guid? ProductId { get; set; }

        [ForeignKey("LinkedProductId")]
        public virtual Product LinkedProduct { get; set; }

        public virtual Guid? LinkedProductId { get; set; }
    }

Error :

Unable to determine the relationship represented by navigation property 'Product.ProductLinks' of type 'List'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

You have two relationships through ProductLink.Product and ProductLink.LinkedProduct reference navigation properties, but only one collection navigation property Product.ProductLinks , so EF does not know how to map them.

The minimum change required is to map the ProductLinks to one of the two related properties, for instance using InverseProperty attribute:

[InverseProperty("Product")]
public virtual List<ProductLink> ProductLinks { get; set; } = new List<ProductLink>();

For more info, see Relationships documentation topic.

So a bit more information on what exactly you're trying to do here would be helpful. From what I can see though, everything can probably be consolidated into a single Product class like so.

    public class Product : Content
    {
        public virtual string NormalizedName { get; set; }

        public virtual string ShortDescription { get; set; }

        public virtual string Description { get; set; }

        public virtual Guid? ThumbnailImageId { get; set; }

        [ForeignKey("ThumbnailImageId")]
        public virtual Media ThumbnailImage { get; set; }

        [ForeignKey("ProductId")]
        public virtual Guid? ProductId { get; set; }

        [ForeignKey("LinkId")]
        public virtual Guid? LinkId { get; set; }

        public virtual List<Product> ProductLinks { get; set; } = new List<Product>();
    }

What error are you receiving exactly?

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