使用实体框架4.0问题更新asp.net mvc2中的多对多关系

时间:2011-02-14 05:29:38

标签: mysql asp.net-mvc-2 entity-framework-4

有人可以指出我做错了什么。我在这个网站上经历了所有相关的问题,但我认为我的问题更多的是MYSQL,正如提到的错误。问题是当我尝试在asp.net mvc2中使用EF更新两个表之间的关系时。

我有这些表,我正在使用MYSQL。

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| courseid   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| coursename | varchar(100)     | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

系:

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| facultyid | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(100)     | YES  |     | NULL    |                |
| lastname  | varchar(100)     | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+

我有这个表来创建关系

+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| courseid  | int(10) unsigned | NO   | MUL | NULL    |       |
| facultyid | int(10) unsigned | NO   | MUL | NULL    |       |
+-----------+------------------+------+-----+---------+-------+

现在,当我想将课程与教师联系起来时,课程和教师都存在,使用下面的代码我得到了一个MYSQL错误:

[AcceptVerbs("POST")]
    public ActionResult AddCourse(int id, FormCollection collection)
    {
        //I made this static values for debuging
        Course course = scheduleEnt.Courses1.First(p => p.courseid == 5); //existing course
        Faculty faculty = scheduleEnt.Faculties1.First(p => p.facultyid == 1); //existing faculty
        faculty.courses.Add(course);
        UpdateModel(faculty);
        scheduleEnt.SaveChanges();//This is where the error generated

        var cc = 5;

        return RedirectToAction("AddCourse", new { cid = cc });
    }

这是错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT
     `course_faculty`.`courseid`,
     `course_faculty`.`facultyid`
' at line 1

1 个答案:

答案 0 :(得分:0)

好吧,事实证明,在我的情况下,问题是启用n:m关系的链接表没有明确地设置主键。因此,虽然只有两个字段都是FK,但DBA并没有将它们设置为自己的主键。

我发现如果我在ssdl部分下查看edmx的源代码,那么有些元素与我在尝试执行更新时遇到的错误相匹配:

<EntitySet Name="x_map" EntityType="DBModel.Store.x_map" store:Type="Tables" store:Schema="xxx" store:Name="x_map">
        <DefiningQuery>SELECT
  `xxx`.`xxx`, 
  `xxx`.`xxx`
  FROM `xxx` AS `xxx`</DefiningQuery>
      </EntitySet>

然后在定义的EntityType

之上
<!--Errors Found During Generation:
  warning 6002: The table/view 'xxx.x_map' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
  -->

更改数据库架构以获得正确的PK并更新EDMX解决了我的问题。

希望这可以帮助您解决问题。