将使用实体框架模型数据 [英] Insert data using Entity Framework model

查看:116
本文介绍了将使用实体框架模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想插入使用实体框架模型我的数据库中的一些数据,但对于一些未知的原因对我来说,它什么都不做。



我缺少的东西在这里?

 使用(VAR上下文=新DatabaseEntities())
{
变种T =新的考验
{
ID = Guid.NewGuid(),
NAME =嗒嗒,
};
context.AddTotest(T);
context.SaveChanges();
}


解决方案

它应该是

  context.TableName.AddObject(TableEntityInstance); 



其中:




  1. 表名:在数据库中的表的名称

  2. TableEntityInstance :表实体类的一个实例



如果你的表是订单的话:

 订购订购=新订单(); 
context.Orders.AddObject(订单);

例如:

  VAR ID = Guid.NewGuid(); 

//插入
使用(VAR DB =新EfContext(NAME = EfSample))
{
VAR的客户= db.Set<客户>() ;
customers.Add(新客户{客户ID = ID,名字=李四});

db.SaveChanges();
}

下面是一个活生生的例子:



<为pre> 公共无效UpdatePlayerScreen(字节[] imageBytes,串installationKey)
{
VAR球员=(由对在this.ObjectContext.Players其中p。 installationKey == installationKey选择p).FirstOrDefault();

无功电流=(从this.ObjectContext.Screenshots d其中d.PlayerID == player.ID选择D).FirstOrDefault();

如果(电流!= NULL)
{
current.Screen = imageBytes;
current.Refreshed = DateTime.Now;

this.ObjectContext.SaveChanges();
}
,否则
{
截图截图=新截图();

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(截图);
this.ObjectContext.SaveChanges();
}
}


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

解决方案

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.

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

这篇关于将使用实体框架模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆