views:

235

answers:

2

Hi

I made an Entity database and it generated all the tables but it seems to have forgotten about the aspnet_usersinRoles table.

I don't know why it would skip this one or how to add it.

When in I look in the Model Browser through VS2008 I see the the aspnet_UsersInRoles table.

+1  A: 

No, it didn't "forget" about aspnet_UsersInRoles. This table consists only of two foreign keys to other tables. Since you've mapped those as well, aspnet_UsersInRoles is subsumed into a many to many relationship within your model. In other words, it's there; it just isn't shown as a separate entity, since it serves only to represent a relationship between two other entities.

Craig Stuntz
But what happens if I still want to add records to that field. Since I need to have records in it to actually make those relationships. Thats what I don't like about EF is they hide the FK. It should not hide it and I am happy in Version 2.0 this will be fixed.
chobo2
You use the related ends, i.e.: someUser.Roles.Add(new Role(...));
Craig Stuntz
I don't follow if the UsersInRoles Tables are not shown in any way how do I insert stuff into it?
chobo2
Can you give me like a full example to show me what you mean? Thanks
chobo2
It **is** shown in the relationship. I've already given you a *full* code example (presuming you want to add a new Role). in my first comment. Don't let the fact that you dont' have to write a mountain of code to make this work deceive you; it really is that simple. To link a user to an existing Role you would do something like someUser.Roles.Add(someRole);
Craig Stuntz
I understand that's how you add a Role to the Roles table but does it put the stuff in the UsersInRole table too? Thats what I am getting at like does it look at that relationship and start adding those fields in it?Also since your doing SomeUser.Roles.Add(new Role()) that almost looks like your using the membership stuff instead of adding something by an entity.
chobo2
To rephrase: Making a relationship *is* adding records. Removing a relationship is deleting records.
Craig Stuntz
Yes, calling someUser.Roles.Add() does add a record to the (subsumed) usersInRoles table. To be clear, I wouldn't normally deal with ASP.NET Membership this way, because it only works with the SQL Membership provider; it won't work with any other provider. But as a general example of handling many-to-many relationships in the EF, yes, that's how it works.
Craig Stuntz
But calling someUser.Roles.Add() has nothing to do with the EF it goes and does its on thing to add it. I am asking how if I would never uses the Roles.Add() from the membership classes how I would add a Role to the UsersInRoles using the EF.
chobo2
1) someUser.Roles.Add() has *everything* to do with the EF. It is an EF method. 2) To add a user/role you should use the Membership API. But pretending for a second that this is just a regular many-to-many relationship in the EF and not the special case of Membership tables, you add a record to UsersInRoles by calling either someUser.Roles.Add() or someRole.Users.Add(). That **does** add a record to UsersInRoles, even if you can't see it right away.
Craig Stuntz
A: 

This is a limitation of entity framework apparently. I came across this very same problem while trying to implement an RIA solution to bridge ASP.NET membership and Silverlight.

This link has a workaround, although I never really got it to work exactly how I wanted. I ended going with plain ol' Linq2SQL.

billb
I will check that out. Thanks
chobo2
It is possible to dumb down the EF to the point where it's as basic as LINQ to SQL, but not worth the effort. Far better to try and understand how it's *intended* to work.
Craig Stuntz