I have a fairly simple Web API 2.2 (EF6) setup where I have Users and Preferences. Users can have many Preferences, and Preferences can have many Users, so I have a join table called UserPreferences. Here are those tables:
CREATE TABLE [dbo].[Users](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Email] [varchar](50) NULL)
CREATE TABLE [dbo].[Preferences](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[PossibleValues] [nvarchar](250) NULL)
CREATE TABLE [dbo].[UserPreferences](
[Id] [int] IDENTITY(1,1) NOT NULL,
[PreferenceId] [int] NOT NULL,
[UserId] [int] NOT NULL,
[Value] [nvarchar](50) NULL)
As you can see, a Preference has a list of possible values (comma-delimited), and a UserPreference has a Value, which should be what the User has selected as their chosen value. So, for example, if the PossibleValues of the Preference are 'Blue,Green,Red,White', the Value of the UserPreference might be 'Blue'. From the way I understand it, this type of relationship is many-to-many, but since I have added another field to the join table (Value), Entity Framework can't handle this as a many-to-many. According to this question's answer, I have to create two one-to-many relationships for this to work. So I have followed the advice of that post:
public partial class User
{
public User()
{
Preferences = new HashSet<UserPreference>();
}
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
public virtual ICollection<UserPreference> Preferences { get; set; }
}
public partial class Preference
{
public Preference()
{
Users = new HashSet<UserPreference>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string PossibleValues { get; set; }
public virtual ICollection<UserPreference> Users { get; set; }
}
public partial class UserPreference
{
public int Id { get; set; }
[Key,Column(Order = 0)]
public int UserId { get; set; }
[Key, Column(Order = 1)]
public int PreferenceId { get; set; }
public virtual User User { get; set; }
public virtual Preference Preference { get; set; }
public string Value { get; set; }
}
I think that looks right, but when I query the service using a Controller method like this in my UsersController:
// GET: odata/Users(5)/Preferences
[EnableQuery]
public IQueryable<UserPreference> GetPreferences([FromODataUri] int key)
{
return _db.Users.Where(m => m.Id == key).SelectMany(m => m.Preferences);
}
I get an exception stating "Invalid object name: 'dbo.UserPreferences'."
"message": "Invalid object name 'dbo.UserPreferences'.",
"type": "System.Data.SqlClient.SqlException",
I clearly have the table UserPreferences, so I can only assume it doesn't look like EF expects it to, but I have no idea what I am supposed to do to get this to work. I don't need to get a list of Users for a Preference, but I do need to get a list of Preferences for a User, and I just cannot get that to work.