c# - Using Entity Framework, how do I reflect a many to many relationship and add entites that exist to a new entity being created? -


I'm new to the unit framework and I'm trying to understand things. I have created a database that is not very complicated. It has about 7 tables and 3 of these mapping tables to add a table record w / other. Example I is using this:

  • The table user <

  • RoleId
  • Role Name
  • Table: UserRole

    • UserId
    • RoleId
  • The foreign keys are mapped to my database. When I'm making a new unit model inside VS 2008, relationships look right in the picture, but UserRole does not have a table for the table. Relationship between the user and amp has been mapped as many; role.

    My problem is that I can make new users and I can make new roles, but I can not understand how to create a new user with an existing role. In addition, the UserRule table is probably not mapped properly within the model. Even my code is here:

      object_city & lt; Role & gt; Roles = references Roles; Role Role = Reference. Where (C => C. RelayName == "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 ();  

    Here I am getting the error:

    Unable to update the entity 'UserRoll' because there is any element defined in it and support There is no element present in the element in the current operation.

    Here's an XML that is related to the user role table:

      & lt; Entity Name = "UserRule" EntityType = "RememberTheJourneyModel.Store .UserRoles" Store: Type = "Tables" Store: Schema = "DBO" Store: Name = "Userrollles" & gt; & Lt; Defining & gt; SELECT [UserRoles] [Role_id], [UserRoles] as [Role_id] [User_id] AS [User_id] FROM [dbo]. [UserRoles] as [UserRoles] & lt; / DefiningQuery & gt; & Lt; / EntitySet & gt;  

    It was just pulling teeth to examine the context as if he gave me a real role unit. I am sure this problem is something with the map of the user neole, but I I'm just starting up and I do not know why it might be wrong.

    And I actually discovered Google & amp; This site is but I think I have not come up with the right search criteria which helps me in deciding it. I got a question that says there are problems with EF, but if you are creating a mapping table then both columns make the primary key, it works itself, I'm not sure how to do this database Is done in (using SQL Server 2005 Express) or in Mapping? This is a private project, so if necessary I can post more information about code or XML. Any and all help would be appreciated.

    I remember this problem even before ... and I believe that I The association table is decided by creating both column keys.

    You can create the primary key in both columns SQL Server Management Studio Table Designer. Just highlight both User_Id and Role_Id at the same time, right-click on the left and select the primary key ... It will do so that any record is not the same combination of user_id and roll_id. Make each entry unique.

    Then you can update your data model in the unit criteria designer .. and hopefully it will be good to move forward.

    I believe that if no key is defined in a table so that the record is unique, then EF will not be able to update the record properly, and therefore the guard of that case structure.


    Comments

    Popular posts from this blog

    oracle - The fastest way to check if some records in a database table? -

    php - multilevel menu with multilevel array -

    jQuery UI: Datepicker month format -