-3
\$\begingroup\$

I tried to create a .Net core API using Microsoft Documentation but I ran into some issues.

The app is working but only for a "single" table. The includes are empty.

        var contact = await _context.Contacts
            .Include(c => c.ContactSkills)
                .ThenInclude(cs => cs.Skill)
            .AsNoTracking()
            .FirstOrDefaultAsync(m => m.Id == id);

Also the Swagger documentation is not generating, I tried the attributes and swapping some settings but didn't managed to make it work.

Here's my repo git : https://github.com/AnthonyDaSilvaFe/OpenWebChallenge.API

Any ideas?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to the Code Review Community, questions about code that is not working are off-topic on code review. Please read How do I ask a good questio?. \$\endgroup\$ Commented Jan 15, 2021 at 12:35
  • \$\begingroup\$ What do you mean " for a "single" table". Could you please explain what is not working properly? \$\endgroup\$ Commented Jan 15, 2021 at 12:39
  • 1
    \$\begingroup\$ Welcome to Code Review.I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. \$\endgroup\$ Commented Jan 15, 2021 at 12:44
  • \$\begingroup\$ Sorry, I asked the question on stackoverflow with code sample and examples and people told me to come here for code review. So I guess I'll close both questions and stays with my problem ^^ \$\endgroup\$ Commented Jan 15, 2021 at 13:01

1 Answer 1

-1
\$\begingroup\$

Your EF context is not properly configured. It doesn't have any primary keys and doesn't have any relations. EF doesn't work at all without primary keys or if you don't need them you have to include "NoPrimaryKey" statement. And no any query will run properly if your relations are not configured. You have to change your code to this:

The class ContactSkill should show what table are foreign keys of:

public class ContactSkill
{
    [Key]
    public int Id { get; set; }

    public int ContactId { get; set; }

    [ForeignKey(nameof(ContactId))]
    [InverseProperty(nameof(ContactSkill.Contact.ContactSkills))]
    public virtual Contact Contact { get; set; }

    public int SkillId { get; set; }

    [ForeignKey(nameof(SkillId))]
    [InverseProperty(nameof(ContactSkill.Contact.ContactSkills))]
    public virtual Skill Skill { get; set; }
}

And configure many-to-many relations in if fluent-api EF core context:

public class ApiContext : DbContext
{
    public ApiContext()
    {
    }

    public ApiContext(DbContextOptions<ApiContext> options)
        : base(options)
    {
    }
    public DbSet<Contact> Contacts { get; set; }
    public DbSet<Skill> Skills { get; set; }
    public DbSet<ContactSkill> ContactSkills { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
            if (!optionsBuilder.IsConfigured)
            {
            }

    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ContactSkill>(entity =>
        {
            entity.HasOne(d => d.Contact)
                .WithMany(p => p.ContactSkills)
                .HasForeignKey(d => d.ContactId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_ContactSkill_Contact");

            entity.HasOne(d => d.Skill)
                .WithMany(p => p.ContactSkills)
                .HasForeignKey(d => d.SkillId)
                .HasConstraintName("FK_ContactSkill_Skill");
        });

    }

The class Contact should include primary key attribute and virual collection of ContactSkills:

public class Contact
{
    public Contact()
    {
        ContactSkills = new HashSet<ContactSkill>();
    }
    [Key]
    public int Id { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string FirstName { get; set; }
  
    public string FullName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public string MobilePhoneNumber { get; set; }
    [InverseProperty(nameof(ContactSkill.Contact))]
    public virtual ICollection<ContactSkill> ContactSkills { get; set; }
}

The class Skill should include primary key attribute and virual collection of ContactSkills:

public class Skill
{
    public Skill()
    {
        ContactSkills = new HashSet<ContactSkill>();
    }
    [Required]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public int ExpertiseLevel { get; set; }
    [InverseProperty(nameof(ContactSkill.Skill))]
    public virtual ICollection<ContactSkill> ContactSkills { get; set; }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to Code Review. Please refuse to answer off-topic questions. \$\endgroup\$ Commented Jan 15, 2021 at 13:36
  • \$\begingroup\$ Thanks @Sergey will try asap. Sorry for breaking the rules of the forum ^^" \$\endgroup\$ Commented Jan 15, 2021 at 14:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.