EF Core doesn't recognize many-to-many relationship

iKingNinja 60 Reputation points
2024-05-23T15:23:05.96+00:00

I have a many-to-many relationship, altough EF Core seems not to recognize it.

The relationship exists between JobOffer and JobOfferTag. JobOffer inherits from BaseJobOffer.

BaseJobOffer.cs

    public class BaseJobOffer
    {
        [Required]
        [TrimmedStringLength(50, 20)]
        public required string Title { get; set; }

        [Required]
        [TrimmedStringLength(500, 100)]
        public required string Description { get; set; }

        [Required]
        [Range(5, int.MaxValue)]
        public required int PaymentInDollars { get; set; }

        [Required]
        public required PaymentMethod PaymentMethod { get; set; }
        
        public BaseJobOffer()
        {
        }

        [SetsRequiredMembers]
        public BaseJobOffer(BaseJobOffer offer)
        {
            Title = offer.Title;
            Description = offer.Description;
            PaymentInDollars = offer.PaymentInDollars;
            PaymentMethod = offer.PaymentMethod;
        }
    }

JobOffer.cs

public class JobOffer : BaseJobOffer
{
    public int Id { get; set; }
    public DateTimeOffset Created { get; private set; } = DateTimeOffset.UtcNow;
    public ICollection<JobOfferTag> Tags = []; // Reference navigation for many to many relationship

    public long UserId { get; set; }
    public ApplicationUser User { get; set; } = null!;

    public JobOffer() : base()
    {
    }

    [SetsRequiredMembers]
    public JobOffer(BaseJobOffer offer) : base(offer)
    {
    }
}

JobOfferTag.cs

public class JobOfferTag
{
    public int Id { get; set; }
    public required string Value { get; set; }

    public ICollection<JobOffer> JobOffers { get; set; } = []; // Reference navigation for many to many relationship
}

EF Core only creates joboffers and joboffertag tables with joboffers having a JobOfferTagId column which should not be the case at all. I expect EF Core to create a join table instead.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
725 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,629 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,496 Reputation points
    2024-05-23T18:08:05.5266667+00:00

    I believe EF only considers properties with at least a getter. If you change this field:

    public ICollection<JobOfferTag> Tags = [];
    

    For a property with a public get it should infer the relationship correctly:

    public ICollection<JobOfferTag> Tags { get; } = [];
    
    0 comments No comments

  2. Hongrui Yu-MSFT 945 Reputation points Microsoft Vendor
    2024-05-24T07:21:04.53+00:00

    Hi,@iKingNinja. Welcome to Microsoft Q&A. 

    1.As P a u l said, EF Core will infer the properties in the DeSet, not the fields.

    2. You could also change Tags to properties , as follows:

    
    public ICollection<JobOfferTag> Tags { get; set; } = [];
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments