C# 使用实体框架模型插入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8835434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:43:39  来源:igfitidea点击:

Insert data using Entity Framework model

c#.netentity-framework

提问by Rocshy

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();
}

采纳答案by Developer

It should be:

它应该是:

context.TableName.AddObject(TableEntityInstance);

Where:

在哪里:

  1. TableName: the name of the table in the database.
  2. TableEntityInstance: an instance of the table entity class.
  1. TableName: 数据库中表的名称。
  2. TableEntityInstance:表实体类的一个实例。

If your table is Orders, then:

如果您的表是Orders,则:

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();
  }
}

回答by Nawed Nabi Zada

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

应该做

回答by sanket parikh

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

[HttpPost] // 在按钮点击事件上编写逻辑时使用

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();
}

回答by yu yang Jian

I'm using EF6, and I find something strange,

我正在使用 EF6,但我发现了一些奇怪的东西,

Suppose Customer has constructor with parameter ,

假设 Customer 有带参数的构造函数,

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

如果我使用 new Customer(id, "name"), 并且做

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

It run through without error, but when I look into the DataBase, I find in fact that the data Is NOT be Inserted,

它运行没有错误,但是当我查看数据库时,我发现实际上没有插入数据,

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

但是如果我添加大括号,请使用new Customer(id, "name"){}并执行

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

the data will then actually BE Inserted,

然后数据将被实际插入,

seems the Curly Brackets make the difference, I guess that only when add Curly Brackets, entity framework will recognize this is a real concrete data.

看起来Curly Brackets 有所作为,我猜只有当添加Curly Brackets 时,实体框架才会识别这是一个真正的具体数据。