3 Replies - 3037 Views - Last Post: 19 December 2013 - 01:06 PM Rate Topic: -----

#1 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Proper EF model class problem

Posted 19 December 2013 - 08:02 AM

I'm having trouble writing the model class for a table (using EF).
Here's my table:
CREATE TABLE [dbo].[UsersInGroups] (
    [UserId]  INT NOT NULL,
    [GroupId] INT NOT NULL,
    PRIMARY KEY CLUSTERED ([UserId] ASC, [GroupId] ASC),
    CONSTRAINT [fk_UserId_Group] FOREIGN KEY ([UserId])     REFERENCES [dbo].[UserProfile] ([UserId]),
    CONSTRAINT [fk_RoleId_Group] FOREIGN KEY ([GroupId]) REFERENCES [dbo].[Group] ([GroupId])
);

And here's my model and DBContext:
[Table("UsersInGroups")]
public class UsersInGroups
{
     [ForeignKey("UserId")]
    public int UserId { get; set; }
     [ForeignKey("GroupId")]
    public int GroupId { get; set; }
}

public class UsersInGroupsContext : DbContext
{
    public UsersInGroupsContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<UsersInGroups> UsersInGroups { get; set; } 
}

I'm getting the folowing exception:
The navigation property 'UserId' is not a declared property on type 'UsersInGroups'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.
Here's the relevant code:
UsersContext dbUsers = new UsersContext();
var currentUser = dbUsers.UserProfiles.First(user => user.UserName == User.Identity.Name);
UsersInGroups group = new UsersInGroups();
group.GroupId = id;
group.UserId = currentUser.UserId;
dbUsersInGroupsContext.UsersInGroups.Add(group);
db.SaveChanges();
The error is thrown on the following line:
dbUsersInGroupsContext.UsersInGroups.Add(group);


I can't seem to find what's the problem. Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Proper EF model class problem

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Proper EF model class problem

Posted 19 December 2013 - 12:20 PM

You might be confusing it by having the property and foreign key names be identical. I'd change the key names as a test.
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Proper EF model class problem

Posted 19 December 2013 - 12:21 PM

This doesn't belong in Advanced Discussion, moving to general C# Help.
Was This Post Helpful? 1
  • +
  • -

#4 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Proper EF model class problem

Posted 19 December 2013 - 01:06 PM

Yeah, I was unsure where to put it, guess I chose the wrong forum, sorry.

I've used the VS ADO.NET Entity Data Model creation wizard, and all problems seem to have magically vanished. I'm 99% sure that my navigation property/context class was wrong. Can't really put my finger on it ATM, and I'm short on time, so I've made a backup of it, I'll find what was the problem later.

P.S.

A bit offtopic, but disregard the fact that I'm using the EF inefficiently, and am probably querying the DB a lot more than I should, I'll create an efficient wrapper class (and port all the code) for all the data interactions later.

Anyway, thanks for all the help!

This post has been edited by RexGrammer: 19 December 2013 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1