【问题标题】:Using existing lists to update the list in C#使用现有列表更新 C# 中的列表
【发布时间】:2014-11-20 12:38:08
【问题描述】:

我有一个 BaseEntity 类,其中包含我的所有字段,称为 ActionGame。因此,如果我想创建一个新的动作游戏,我会执行以下操作:

var actionGame = new ActionGame
{
  price = textbox1.text,
  name = textbox2.text,
  type = textbox3.text
};
gameService.CreateUpdateActionGame(actionGame)

这会创建一个新的动作游戏,并使用我所有的 NHibernate 映射将其保存到数据库中。但是,我想更新一个现有的动作游戏,所以我不想创建一个新的 ActionGame,因为我正在做 var actionGame = new ActionGame 我能做些什么来代替呢?它应该类似于我在上面完成创建但不使用新的方式,因为那样会创建一个新记录

基础实体类:

public class ActionGame : BaseEntity
{
  public virtual string price { get; set; }
  public virtual string name { get; set; }
  public virtual string type { get; set; }

  public ActionGames()
  {
    ActionGames = new List<ActionGames>();
  }
}

【问题讨论】:

    标签: c# asp.net nhibernate collections


    【解决方案1】:

    您应该从数据库加载要更新的项目,然后更新项目并使用该对象调用 CreateUpdateActionGame。

    我不知道你的加载方法,但是是这样的:

    var actionGame = gameService.GetByName("the name to update");
    actionGame.price = textbox1.text;
    actionGame.name = textbox2.text;
    actionGame.type = textbox3.text;
    
    gameService.CreateUpdateActionGame(actionGame);
    

    【讨论】:

    • 另一种方法是创建一个与数据库中记录匹配的 ID 值的新实例,更新其他值并使用 NHibernate Merge() 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多