【发布时间】:2014-04-02 18:18:03
【问题描述】:
我刚刚在 C# 中启动了一个简单的项目并添加了“ADO.NET 实体数据模型”
我的问题很简单,我有一个“课程”表和一个“用户”表,以及 user_id 和 course.user_id 之间的外键。我确实按照this msdn tutorial 实现了表模型,但它没有说明两个表之间的关系。
这是我的失败代码:
User user = null;
string title = "title";
string content = "content";
using (var db = new Entities())
{
//search for the last user
var query = from b in db.Users
orderby b.RegisterDate descending
select b;
foreach (var item in query.Take(1))
{
user = item;
}
//create lesson
Lesson lesson = new Lesson
{
Content = content,
Grade = "X1",
PostDate = DateTime.Now,
Title = title,
User = user, // user {System.Data.Entity.DynamicProxier.User_1DBAF22....}
UserId = user.ID
};
db.Lessons.Add(lesson);
db.SaveChanges(); //DBUpdateException was unhandled \n Unable to update the EntitySet 'Lessons' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
}
【问题讨论】:
-
从图片看起来你在课程表中有很多键。另请查看:stackoverflow.com/questions/7583770/…
-
我不知道什么是视图,所以我肯定没有。我做了功课,找不到简单的解决方案。