0

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 database designer

ioan
  • 751
  • 1
  • 9
  • 26
  • From the picture it looks like you have a lot of keys in the Lesson table. Also check this out: http://stackoverflow.com/questions/7583770/unable-to-update-the-entityset-because-it-has-a-definingquery-and-no-updatefu – Dani J Jan 13 '14 at 17:35
  • I don't know what a view is, therefore I surely don't have one. I did my homework, couldn't find a simple solution. – ioan Jan 13 '14 at 17:36

1 Answers1

0

Define a primary key for your database tables.

Erwin
  • 3,060
  • 26
  • 24