Reputation: 145
I'm trying to create a one to many relationship in an app I'm building to learn .NET and Angualr. I'm running into a roadblock and can't seem to find an answer on it when searching to issue.
I have three tables too store the following:
Using Entity Framework when trying to override the OnModelCreating method, I'm getting the following error under .HasOne in the builder.Entity() method:
The type 'int' must be a reference type in order to use it as parameter 'TRelatedEntity' in the generic type or method 'EntityTypeBuilder.HasOne(Expression>)'
I'm not sure how to get around this and correct the issue. Is anyone able to point me in the right direction on this?
DataContext file (which is where my error is occurring)
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Outmatch.API.Models;
namespace Outmatch.API.Data
{
// DbContext is what is called to connect to the database and pass data along.
public class DataContext : IdentityDbContext<User, Role, int, IdentityUserClaim<int>, UserRole,
IdentityUserLogin<int>, IdentityRoleClaim<int>, IdentityUserToken<int>>
{
public DataContext(DbContextOptions<DataContext> options) : base(options) {}
// Values name represents the table name in the database.
public DbSet<Value> Values { get; set; }
public DbSet<Locations> Locations { get; set; }
public DbSet<Organizations> Organization { get; set; }
public DbSet<OrgToClient> OrgToClients { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserRole>(userRole =>
{
userRole.HasKey(ur => new {ur.UserId, ur.RoleId});
userRole.HasOne(ur => ur.Role)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
userRole.HasOne(ur => ur.User)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
builder.Entity<OrgToClient>()
.HasKey(c => new {c.ClientId, c.OrganizationId});
builder.Entity<OrgToClient>()
.HasOne(u => u.OrganizationId) // Error is occurring here under .HasOne
.WithMany(u => u.ClientId)
.HasForeignKey(u => u.OrganizationId)
.OnDelete(DeleteBehavior.Restrict);
}
}
}
OrgToClient model:
namespace Outmatch.API.Models
{
public class OrgToClient
{
public int OrganizationId { get; set; }
public int ClientId { get; set; }
public User Users { get; set; }
public Organizations Organizations { get; set; }
}
}
User.cs model
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;
namespace Outmatch.API.Models
{
// List of properties for the User (Client) table in the db
public class User : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime ActiveDate { get; set; }
public DateTime EndDate { get; set; }
public virtual ICollection<UserRole> UserRoles { get; set; }
public ICollection<OrgToClient> ClientId { get; set; }
public ICollection<OrgToClient> OrganizationId { get; set; }
}
}
Organization Model
using System;
using System.Collections.Generic;
namespace Outmatch.API.Models
{
public class Organizations
{
public int Id { get; set; }
public string OrganizationName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
public ICollection<OrgToClient> ClientId { get; set; }
public ICollection<OrgToClient> OrganizationId { get; set; }
}
}
Any assistance would be greatly appreciated
Upvotes: 0
Views: 1236
Reputation: 823
HasOne
takes a parameter of type Expression<Func<TEntity,TRelatedEntity>>
, meaning that your predicate (passed to HasOne) should be returning a navigation property not the foreign key
try HasOne (u => u.Organizations)
Upvotes: 2
Reputation: 628
Possibly try fixing the case of:
.withMany(u => u.ClientId)
to be:
.WithMany(u => u.ClientId)
This may result in a null value in non-nullable Int OrgToClient.ClientID
causing the type reference error observed.
Upvotes: 1