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