0

I want to save data to my database after a user registered. I wrote code in Registerfunctin in Account controller. the code is:

[HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    ///My code for saving in dtabase
                            Recommend ad = new Recommend();      
                            ad.estate = 1;
                            ad.industry = 1;
                            storedb.AddToRecommends(ad);         
                            storedb.SaveChanges();
                    ///
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }


            // If we got this far, something failed, redisplay form
            return View(model);
        }

Recommend is the name of table that I want to store 1 in estate and industry column. I used this way for saving befor in another controller and everything were OK, But now I get this exception:

Exception

Can anybody tells me whats the problem? Thanks alot

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
starrr
  • 1,013
  • 1
  • 17
  • 48
  • 1
    Are you using StoredProcedure to handle the Add and Update for this Recommend table ?? – Wahid Bitar Jun 17 '12 at 09:48
  • can you paste here the details in the "Copy exception details to the clipboard"? This info is usually much more helpful. Also can you paste a picture of your table design? – Sparkle Jun 17 '12 at 22:43

1 Answers1

2

The exception is pretty clear what the problem is: The EntitySet Recommend and the type Recommend is defined by a Defining Query. That means it is the result of a SQL query specified in the store schema of the EDMX file. This is similar to mapping a view from the database, i.e. it is read-only and you can't update it, can't insert data into the set and you can't delete data from it.

It is possible though to map Stored Procedures to the Insert, Update, Delete operations of the entity defined by a Defining Query, but the exception also says that no procedure is specified to support the Insert operation.

You are talking about a Recommend table, so the question is why the Recommend set isn't directly mapped to this table. As far as I know a Defining Query must be written manually by a developer and put into the EDMX file manually. The "Update model from database" does not create a Defining Query. Who wrote this query? How did you create the EDMX file at all? Database-First or Model-First or hand-written XML or...?

Edit

The last paragraph in my answer above is not correct. It's possible that a developer could have written a Defining Query manually, but when you use Database-First approach (which I assume) much more likely is that your table in the database does not have a primary key. In that case the answer here applies:

Well when a table is encountered without a PrimaryKey it is treated as a View.

And views show up in the EDMX file (open in an XML editor to see) in the StorageModel\EntitySet[n]\DefiningQuery element.

Solution: Check in the database if your Recommend table has a primary key. If it doesn't have a key, add one.

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420