SQLGrinder
SQLGrinder

Reputation: 383

Entity Framework will not recognize webpage_UsersInRoles

I am using EF5 VS2012 and using SimpleMembership. I have let MS auto-create the SQL tables

WebSecurity.InitializeDatabaseConnection("SqlRoleManagerConnection", "webpages_Users", "UserID", "Username", true);

And whenever I attempt to create a model from DB to create EDMX, it omits the webpages_UsersInRoles table. There are references to this table in the XML but it does not appear on the diagram and no classes are generated for it. I am running VS2012 Update 1 so this is not related to the commonly reported bug. I have also manually selected Run Custom Tool which does not fix.

As you probably know, this missing table only contains two FK fields to link the Users and Roles tables.

I have attempted creating a new project and new EDMX files and they all produce the same result - missing webpages_UsersInRoles diagram & classes.

EDIT: I can repeating go into Update from DB and select the table and it will not add to the diagram or class. What is the reason for this behavior and how can I force EF to connect everything so I can use this table and class?

enter image description here

Upvotes: 2

Views: 1424

Answers (2)

Tuyen Nguyen
Tuyen Nguyen

Reputation: 4479

I think it's the way EF works with Many-To-Many relationships, below are 2 explanations and how to work with them:

http://nileshhirapra.blogspot.in/2012/03/entity-framework-insert-operation-with.html

http://weblogs.asp.net/zeeshanhirani/archive/2008/08/21/many-to-many-mappings-in-entity-framework.aspx

Upvotes: 0

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

Generally you don't reference that table directly in your code. You work with the Membership API.

Here's a good post to read about it: Seeding Membership & Roles in ASP.NET MVC 4

Sample code:

private void SeedMembership()
{
    if (!Roles.RoleExists("Administrator"))
    {
        Roles.CreateRole("Administrator");
    }

    if (!Roles.RoleExists("Teacher"))
    {
        Roles.CreateRole("Teacher");
    }

    if (!Roles.RoleExists("Student"))
    {
        Roles.CreateRole("Student");
    }

    if (!WebSecurity.UserExists("leniel"))
    {
        WebSecurity.CreateUserAndAccount("leniel", "mypass");
    }

    if (!Roles.GetRolesForUser("leniel").Contains("Administrator"))
    {
        Roles.AddUsersToRoles(new[] { "leniel" }, new[] { "Administrator" });
    }

    if (!WebSecurity.UserExists("tester"))
    {
            WebSecurity.CreateUserAndAccount("tester", "test123");
    }

    if (!Roles.GetRolesForUser("tester").Contains("Administrator"))
    {
        Roles.AddUsersToRoles(new[] { "tester" }, new[] { "Administrator" });
    }
}

Upvotes: 2

Related Questions