Insert data using the Entity Framework model

I am trying to insert some data into my database using the Entity Framework model, but for some unknown reason I am not doing anything.

Did I miss something?

using (var context = new DatabaseEntities()) { var t = new test { ID = Guid.NewGuid(), name = "blah", }; context.AddTotest(t); context.SaveChanges(); } 
+49
c # entity-framework
Jan 12 '12 at 12:57
source share
4 answers

It should be:

 context.TableName.AddObject(TableEntityInstance); 

Where:

  • TableName : the name of the table in the database.
  • TableEntityInstance : An instance of the table entity class.

If your table is Orders , then:

 Order order = new Order(); context.Orders.AddObject(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 a live 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.AddObject(screenshot); this.ObjectContext.SaveChanges(); } } 
+60
Jan 12 2018-12-12T00:
source share
 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(); 

Gotta do it

+22
May 16 '13 at 9:42
source share

[HttpPost] // used when writing logic on a 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(); } 
+3
Jun 30 '16 at 16:44
source share

I use EF6 and I find something strange

Suppose the Client has a constructor with a parameter,

if I use new Customer(id, "name") and do

  using (var db = new EfContext("name=EfSample")) { db.Customers.Add( new Customer(id, "name") ); db.SaveChanges(); } 

It works without errors, but when I look into the database, I find that the data is NOT inserted,

But if I add curly braces, use new Customer(id, "name"){} and do

  using (var db = new EfContext("name=EfSample")) { db.Customers.Add( new Customer(id, "name"){} ); db.SaveChanges(); } 

then the data will be actually inserted,

it seems that Curly Brackets matter, I think that only when adding Curly Brackets does the entity structure recognize that this is real concrete data.

+2
Apr 07 '17 at 3:49 on
source share



All Articles