109

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);' Commented 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? Commented 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. Commented Jan 18, 2023 at 10:09

3 Answers 3

138

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();
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

I don't have any AddObject method.
@Dennis Traub asked you about your Model. Please provide more info about it.
Use .Add rather than .AddObject
On EF 6, you would use .Add rather than .AddObject
Fantastic answer! thanks for this as it shows how to read and write using the entity framework
|
47
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

Comments

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 Comment

if any query ask me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.