Using the Entity Framework, how can I reflect the relationships of many, many, and add the entites that exist to the newly created object?

I am new to Entity Framework and trying to figure it out. I have created a database that is not very complicated. There are about 7 tables and 3 of them are mapping tables for linking one table record to another. The example I use here is the following:

  • Table user

    • Userid
    • Username
  • Table role

    • Role id
    • Rolename
  • Table: UserRole

    • Userid
    • Role id

Foreign keys are displayed in my database. When I create a new Entity model in VS 2008, the diagram seems to be correct, but does not create a table for the UserRole table. The relationship is displayed as β€œPlenty for many" between the user and the role.

, , , , . , UserRole, , . :

ObjectQuery<Role> roles = context.Roles;
Role role = context.Roles.Where(c => c.RoleName == "Subscriber").First();

 User user = new User
 { 
  DisplayName = "TestCreate2",
  Email = "test@test.com",
  Password = "test"
 };            
 context.AttachTo("Roles", role);
 user.Roles.Add(role);            
 context.AddToUsers(user);
 context.SaveChanges();

, :

EntitySet 'UserRoles', DefiningQuery, .

xml, UserRole:

<EntitySet Name="UserRoles" EntityType="RememberTheJourneyModel.Store.UserRoles" store:Type="Tables" store:Schema="dbo" store:Name="UserRoles">
        <DefiningQuery>SELECT 
  [UserRoles].[Role_id] AS [Role_id], 
  [UserRoles].[User_id] AS [User_id]
  FROM [dbo].[UserRoles] AS [UserRoles]</DefiningQuery>
      </EntitySet>

, , , . , , UserRole, , .

google , , , , . , , EF , , , . , . ( SQL SERVER 2005 EXPRESS) ? , xml, . .

+5
1

, ... , , .

SQL Server Management Studio. User_Id Role_Id, ... , User_Id Role_Id.. .

Entity Framework.. , , .

, , , EF , .

+4

All Articles