How can I insert data into my database using Entity Framework ?
I’m trying this code bellow, but, in the method AddObject I have to pass 2 parameter:
- string entitySetName
- object entity
What is the entitySetName ?
The object entity is my model ?
EntityNetimoveis.San2011Entities db = new EntityNetimoveis.San2011Entities();
Model.Gerenciar.UsuarioCurso us = new Model.Gerenciar.UsuarioCurso();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
try
{
us.Usuario_Id = Convert.ToInt32(context.Request["id"]);
db.San_UsuarioCurso.Context.AddObject(us);
db.SaveChanges();
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("");
}
}
It’s the name of your ObjectSet, in your case
San_UsuarioCurso.You’re going back and forth in your references however.
In
db.San_UsuarioCurso.Context.You’re referencing your context (db) and then going to your objectset (San_UsuarioCurso) and then going back to your context (Context).You can add directly to the object set like this, and not have to define the name of the entity set:
Or if you want to use your function this should work as well: