0
votes

I am trying to get initialise a foreign key on my database in EntityFrameworkCore, and following this guide https://docs.microsoft.com/en-us/ef/core/modeling/keys?tabs=fluent-api I believe I should just be able to use the .HasForeignKey method. However I am getting the following error

'KeyBuilder' does not contain a definition for 'HasForeignKey' and no accessible extension method 'HasForeignKey' accepting a first argument of type 'KeyBuilder' could be found (are you missing a using directive or an assembly reference?)

Here is my code

using Microsoft.EntityFrameworkCore;
using TemplateApi.Models;

namespace TemplateApi
{
    public class DatabaseContext : DbContext
    {
        public DbSet<Department> Departments { get; set; }
        public DbSet<Role> Roles { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Department>()
                .HasKey(b => b.ID);
                

            modelBuilder.Entity<Role>()
                .HasKey(b => b.Id)
                .HasForeignKey(b => b.Department_Id);
        }
    }
}

Department:

using System.Collections.Generic;

namespace TemplateApi.Models
{
    public class Department
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public ICollection<Role> Roles { get; set; }

    }
}

Role:

namespace TemplateApi.Models
{
    public class Role
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Department_Id { get; set; }
        public Department Department { get; set; }
    }
}

And my csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.7">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
  </ItemGroup>

</Project>

I have no idea how to fix this error, as all of what I have seen online has said to add the Relational element of Entity framework core

1

1 Answers

0
votes

You need to do something like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Department>()
        .HasKey(b => b.ID);

    modelBuilder.Entity<Role>()
        .HasKey(b => b.Id);
        
    modelBuilder.Entity<Role>()
        .HasOne(p => p.Department)
        .WithMany(b => b.Roles)
        .HasForeignKey(p => p.Department_Id);
}

Take a look at Relationships in EF Core.