Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Join Relationship Tables

$
0
0

Hey everyone. 

I'm learning .NET and am building a project to learn with.  I've run into a an issue that I'm wondering if I can get a hand on, trying to figure out how to do it.  

I have three tables: 

  • Organizations (Holds the details for each organization that has been created) 
  • Users (Holds the details for each user added to the system)
  • OrganizationUsers (The relationship table between the other two tables). 

The OrganizationUsers table has two columns. OrganizationsId and UserId

Where I'm getting stuck is writing out the builder framework so that when a new user is created, it assigned the user to an organization in the OrganizationUsers table. When building the entity in my DataContext file, I'm getting the following errors when adding the .WithMany statement:

  • Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<Outmatch.API.Models.OrganizationUsers>' 
  • Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type 

Here are the three models: 

User.cs

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 virtual ICollection<OrganizationUsers> OrganizationUsers { get; set; }
    }
}

Organization.cs

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 virtual ICollection<OrganizationUsers> OrganizationUsers { get; set; }
    }
}

OrganizationUsers

using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;

namespace Outmatch.API.Models
{
    public class OrganizationUsers
    {
        public virtual User UserId { get; set; }
        public virtual Organizations OrganizationsId { get; set; }
    }
}

My DataContext.cs file:

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; }

        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<OrganizationUsers>(organizationUsers => 
                {
                    organizationUsers.HasOne(ou => ou.OrganizationsId)
                        .WithMany(ou => ou.Id); // Errors mentioned above are occurring here with ou.Id
                });
        }
    }
}

Any ideas what what I am doing wrong here, and what I should be doing instead?  I appreciate all input!


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>