102

I'm trying to insert some data in my database using Entity Framework model, but for some unknown reasons to me, it does nothing.

Am I missing something here?

using (var context = new DatabaseEntities())
{
    var t = new test
    {
        ID = Guid.NewGuid(),
        name = "blah",
    };
    context.AddTotest(t);
    context.SaveChanges();
}
3
  • Try 'SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);'
    – Willem
    Jan 12, 2012 at 13:00
  • 6
    Code First? Model First? Does it break or does it just not store anything? What does SQL Profiler say? Is there anything being sent to the database at all? Jan 12, 2012 at 13:00
  • This AddTotest(t) method is a custom method. Since it's not shown what happens there, this question can't be answered. Jan 18 at 10:09

3 Answers 3

129

It should be:

context.TableName.Add(TableEntityInstance);

For versions of entity framework before 6, it was:

context.TableName.AddObject(TableEntityInstance);

Where:

  1. TableName: the name of the table in the database.
  2. TableEntityInstance: an instance of the table entity class.

If your table is Orders, then:

Order order = new Order();
context.Orders.Add(order);

For example:

 var id = Guid.NewGuid();
    
 // insert
 using (var db = new EfContext("name=EfSample"))
 {
    var customers = db.Set<Customer>();
    customers.Add( new Customer { CustomerId = id, Name = "John Doe" } );
  
    db.SaveChanges();
 }

Here is an example:

public void UpdatePlayerScreen(byte[] imageBytes, string installationKey)
{
  var player = (from p in this.ObjectContext.Players where p.InstallationKey == installationKey select p).FirstOrDefault();

  var current = (from d in this.ObjectContext.Screenshots where d.PlayerID == player.ID select d).FirstOrDefault();

  if (current != null)
  {
    current.Screen = imageBytes;
    current.Refreshed = DateTime.Now;

    this.ObjectContext.SaveChanges();
  }
  else
  {
    Screenshot screenshot = new Screenshot();

    screenshot.ID = Guid.NewGuid();
    screenshot.Interval = 1000;
    screenshot.IsTurnedOn = true;
    screenshot.PlayerID = player.ID;
    screenshot.Refreshed = DateTime.Now;
    screenshot.Screen = imageBytes;

    this.ObjectContext.Screenshots.Add(screenshot);
    this.ObjectContext.SaveChanges();
  }
}
9
  • 1
    I don't have any AddObject method.
    – Rocshy
    Jan 12, 2012 at 13:26
  • @Dennis Traub asked you about your Model. Please provide more info about it.
    – NoWar
    Jan 12, 2012 at 13:29
  • 31
    Use .Add rather than .AddObject Mar 7, 2016 at 5:09
  • 10
    On EF 6, you would use .Add rather than .AddObject
    – David
    Jul 26, 2016 at 13:38
  • 1
    Fantastic answer! thanks for this as it shows how to read and write using the entity framework May 22, 2018 at 11:04
45
var context = new DatabaseEntities();

var t = new test //Make sure you have a table called test in DB
{
    ID = Guid.NewGuid(),
    name = "blah",
};

context.test.Add(t);
context.SaveChanges();

Should do it

0
11

[HttpPost] // it use when you write logic on button click event

public ActionResult DemoInsert(EmployeeModel emp)
{
    Employee emptbl = new Employee();    // make object of table
    emptbl.EmpName = emp.EmpName;
    emptbl.EmpAddress = emp.EmpAddress;  // add if any field you want insert
    dbc.Employees.Add(emptbl);           // pass the table object 
    dbc.SaveChanges();

    return View();
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.