【发布时间】:2014-02-12 14:58:03
【问题描述】:
我有一个用于测试的小型 SQL Server 2012“学校”数据库。它有这些 TableName(FieldName1, FieldName2....):
Course(CourseNo, CourseName), Student(StudentId, StudentName, Birth), Enrollment(EnrollNo, StudentNo, CourseId)
Enrollment 表使用外键将学生链接到课程 - CourseNo:CourseID 和 StudentId:StudentNo
在 VS2013 网站中创建 EF 模型图时,注册实体显示“导航”到课程和学生
我在 html 页面中使用 JSON 来访问“控制器”,该控制器使用 EF6 和 linq 从数据库中检索数据以填充 jQuery Mobile 应用程序中的页面。第一个测试是在 html 表中仅列出“Course”表的内容。如果我的 EF 模型只包含 Course 表,则代码可以完美运行。如果我添加 Student 和 Enrollment 表(并且什么都不做),代码会失败,调试器信息表明 JSON 无法解决命名问题。所有字段名称都是唯一的,但注册表导航显示课程和学生。显然,任何涉及相同名称的引用都会导致 JSON 失败。作为一个附带测试,使用 Gridview 向站点添加一个 ASP 页面,该页面使用完全相同的 DB 检索代码,所有三个表都存在,效果很好。
我在尝试结合 JSON 和 EF6-linq 时有什么遗漏吗?
这里是html端的重要部分:
$.getJSON("api/CourseTitles",
function (data) {
$('#coursenames').empty(); // Clear the table body.
// Loop through the list of courses.
$.each(data, function (key, val) {
// Add a table row for the course.
$('#coursenames').append('<tr><td>' + val.CourseNo + ' ' +
'</td><td>' + val.CourseName + '</td></tr>');
});
});
.................
<table>
<thead>
<tr><th>Number</th><th>Name</th></tr>
</thead>
<tbody id="coursenames"></tbody>
</table>
这是控制器代码:
Public Function GetCourseTitles() As IEnumerable(Of Course)
Dim myContext As New SchoolEntities1()
Return From courselist In myContext.Courses Select courselist
End Function
编辑 - 这有效:
Public Function GetCourseTitles() As IEnumerable(Of Course)
Dim myContext As New SchoolEntities()
myContext.Configuration.ProxyCreationEnabled = False
Return From courselist In myContext.Courses Select courselist
End Function
【问题讨论】:
标签: json entity-framework