[英]Simple Relationships with ADO.NET Entity Data Model
我刚刚用C#开始了一个简单的项目,并添加了“ ADO.NET实体数据模型”
我的问题很简单,我有一个“课程”表和一个“用户”表,以及user_id和lesson.user_id之间的外键。 我确实像本msdn教程中那样实现了表模型,但并未说明两个表之间的关系。
这是我的失败代码:
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.
}
为您的数据库表定义一个主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.