Phil Huhn
Phil Huhn

Reputation: 4111

.Net Core navigation on one-to-many relationship not working

I am converting a .Net Framework web-site to a .Net Core web-site.

When I place a breakpoint and drill into the Company data, I see the relationship, but I see no Server data. I have checked in SQL Server and verified that I do have one Company and one associated Server.

I have a one-to-many relationship reflected in a SQL Server diagram as follows: Company-Server one-to-many SQL Server diagram

Some of the entity class for the Company is as follows:

public class Company
{
    //
    [Required(ErrorMessage = "'Company Id' is required.")]
    public int CompanyId { get; set; }
    [Required(ErrorMessage = "'Company Short Name' is required."), MaxLength(12, ErrorMessage = "'CompanyShortName' must be 12 or less characters.")]
    public string CompanyShortName { get; set; }
    [Required(ErrorMessage = "'Company Name' is required."), MaxLength(80, ErrorMessage = "'CompanyName' must be 80 or less characters.")]
    public string CompanyName { get; set; }
    [MaxLength(80, ErrorMessage = "'Address' must be 80 or less characters.")]
    public string Address { get; set; }
    ...
    //
    public ICollection<Server> Servers { get; } = new List<Server>();
    public ICollection<ApplicationUser> Users { get; } = new List<ApplicationUser>();
    public ICollection<EmailTemplate> EmailTemplates { get; } = new List<EmailTemplate>();
    //
}

Some of the entity class for the Server is as follows:

[Table("Servers")]
public class Server : IServer
{
    //
    [Required(ErrorMessage = "'Server Id' is required.")]
    public int ServerId { get; set; }
    [Required(ErrorMessage = "'Company Id' is required.")]
    public int CompanyId { get; set; }      // FK of the company that manages the server.
    [Required(ErrorMessage = "'Server Short Name' is required."),
        MinLength(6, ErrorMessage = "'Server Short Name' must be 6 or up to 12 characters."),
        MaxLength(12, ErrorMessage = "'Server Short Name' must be 12 or less characters.")]
    public string ServerShortName { get; set; }  // for login
    [Required(ErrorMessage = "'Server Name' is required."), MaxLength(80, ErrorMessage = "'Server Name' must be 80 or less characters.")]
    public string ServerName { get; set; }      // internal 
    ...
    //
    public virtual Company Company { get; set; }
    //
    public ICollection<ApplicationUserServer> UserServers { get; }
        = new List<ApplicationUserServer>();
    public ICollection<NetworkLog> NetworkLogs { get; }
        = new List<NetworkLog>();
    public ICollection<Incident> Incidents { get; }
        = new List<Incident>();
    //
}

In the database context the OnModelCreating is as follows:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        ...
        modelBuilder.Entity<Company>((item) =>
        {
            item.HasKey(c => c.CompanyId);
            item.HasMany(u => u.Servers).WithOne(s => s.Company).HasForeignKey(s => s.CompanyId);
            item.HasIndex(c => c.CompanyShortName).IsUnique()
                .HasName("Idx_Companies_ShortName");
        });
        //
        modelBuilder.Entity<Server>((item) =>
        {
            item.HasKey(s => s.ServerId);
            item.HasMany(u => u.UserServers).WithOne(u => u.Server).HasForeignKey(u => u.ServerId);
            item.HasOne(c => c.Company).WithMany(s => s.Servers)
                .HasForeignKey(s => s.CompanyId).OnDelete(DeleteBehavior.Restrict);
            // index
            item.HasIndex(s => s.ServerShortName).IsUnique()
                .HasName("Idx_AspNetServers_ShortName");
        });
        ...
    }

Many things in .Net Core are different from my .Net Framework version, so apparently I am not understanding something. As I am gathering more information, I have noted other missing navigational data. Additionally, my in-memory test works fine and I can see the navigational Server data. This is the .Net Framework version on GitHub:

Net-Incident on GitHub

Upvotes: 2

Views: 456

Answers (1)

fared
fared

Reputation: 170

You have to include those relations in your query results

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .ToList();
}

More info

Upvotes: 1

Related Questions