简体   繁体   中英

Simple Relationships with ADO.NET Entity Data Model

I just started a simple project in C# and added "ADO.NET Entity Data Model"

My problem is simple, I have a "Lesson" table and a "User" table and a foreign key between user_id and lesson.user_id. I did implement the table models as in this msdn tutorial but it does not say about the relationship between two tables.

Here is my fail code:

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.
}

数据库设计师

为您的数据库表定义一个主键。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM