1

I am trying the Model first approach of Entity Framework. I am a newbie to Entity Framework and learning from here and coded the same way. But I am getting this error

Unable to update the EntitySet 'Users' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.

the code I am running is:

protected void Page_Load(object sender, EventArgs e)
    {

        User use = new User();
        use.First_Name = "Arslan";
        use.Last_Name = "Akbar";
        use.Password = "alone";
        use.Email = "arslan@gmail.com";
        use.Designation = "Head";
        using (CustomersEntities ce = new CustomersEntities())

        {

            //int count = ce.Users.Count();
            //count++;
           // use.Id = count;

            ce.Users.Add(use);
            ce.SaveChanges();
          //  ce.Users.Create(use);
          //  ce.SaveChanges();
            //ce.Entry(use).State = System.Data.EntityState.Added;

        }

    }

I am unable to identify the issue.

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
Naveed Yousaf
  • 133
  • 2
  • 13

2 Answers2

0

There are a number of things that could be causing this, some are covered in comments above:

  • Trying to update a view
  • Trying to update a table that does not have a primary key
  • The primary key was added to the table directly, not via model first, the C# code therefore does not see the primary key
  • There is a reference to another table and there are some problems with the keys

I would take a backup, delete what you have and then try again from scratch.

PS: I just worked through the example in your link, it worked fine for me.

What I noticed was that the Add method was "AddObject" not "Add". You should check the target framework for your project.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • I just did not make Id as a primary key.The procedure is that i just created that primary key deleted the table from Model then again imported them now it's working fine. – Naveed Yousaf Nov 13 '13 at 05:13
-1

Database table doesn't have a primary key, in your data base set primary key for user table.

behnam shateri
  • 1,223
  • 13
  • 18