none
many to many RRS feed

  • Question

  • In  the edmx a table was missing and I noticed the many to many relationship between 2 tables which as I remember it, entity framework hides index tables and creates that relationship.

    The problem I’m having is that when I generate a controller MVC 5 web the view displays 2 fields of one table and when those fields are provided with data I get an error when saving the changers, id cannot contain null

    When checking the create post view I can see that both values are present, so this must be an issues with the relationship created in the edmx

    Guessing I need to provide a id for the other table and have changers persist to all 3 tables

    I’m guessing I need to include other fields which don’t appear on the view

    Could someone help

    Monday, February 10, 2014 4:26 PM

All replies

  • Hello,

    >>In  the edmx a table was missing and I noticed the many to many relationship between 2 tables which as I remember it, entity framework hides index tables and creates that relationship.

    This is by designed, the model does not show the cross table.

    >>The problem I’m having is that when I generate a controller MVC 5 web the view displays 2 fields of one table and when those fields are provided with data I get an error when saving the changers, id cannot contain null

    Being confused about this, because the two entities should be added into database independent as below:

    Course course = new Course() { CourseID = 3, CourseName = "3", TestOne = TestOne.One };
    
                    db.Courses.Add(course);
    
                    db.SaveChanges();

    If we need to create an associated relationship, we use codes like below:

    Student st = db.Students.FirstOrDefault();
    
                    Course course = db.Courses.FirstOrDefault();
    
                    course.Students.Add(st);

    Then it will create a record in the cross table.

    Could you please share the program codes so that we can test it to check why it throws an error?

    And if you want to know more about many to many in Entity  Framework, please refer to link below:

    http://www.codeproject.com/Articles/396321/Many-to-Many-relation-with-Entity-Framework-in-ASP

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, February 11, 2014 9:03 AM
    Moderator
  • Yes I understand about cross tables and if you do want it to show you just add another field like date created for example

    The problem I think is the tables have a computed value in the database because the table that join have primary keys but are not identities columns and set to identity in the model.

    I now think that looking closer at the database, I cant just supply one table with a data update which is where the problem begins and maybe why the database is expecting another id

    The insert into the table expects a computed value created in the primary table to add this value to one key in the cross table and the other primary table to compute a value in that primary key also and add that to the cross table

    I'm still researching this a the moment

     
    Tuesday, February 11, 2014 4:54 PM
  • Hello,

    Since we could not reproduce it, could you please provide a small demo to skydriver which can reproduce this issue?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 13, 2014 9:22 AM
    Moderator
  • You already have the example. This is about the

    Microsoft.AspNet.Identity in MVC 5 web

    Trying to add a role only so users can be added later

    I can't see where the userID is generated and trying to add a roll is where I get the id issue

       

    Thursday, February 13, 2014 4:42 PM
  • Could you provide the links regarding Microsoft.AspNet.Identity that you are looking? Because there are lots of links regarding it.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 14, 2014 7:57 AM
    Moderator
  • Sorry but I have many other things to attend to at the moment and consider this closed

    Thank you

    Friday, February 14, 2014 8:16 AM