Fluent NHibernate 能给我带来什么?

发布于 2024-12-02 01:36:03 字数 505 浏览 1 评论 0原文

总的来说,我对 Fluent NHibernate 和 ORM 工具还很陌生。我不太确定在这种情况下我应该对该工具抱有什么期望。我有基金、控股和资产表以及相应的对象。基金对象有一个控股集合,每个控股都有一个资产。当我保存基金时,我希望后续持有的资产和资产也保存下来。

我似乎发生了这种情况,但是,我似乎正在尝试将新记录插入每个表中,而不是更新现有记录。我在数据库中有独特的约束,因此保存失败。我是否需要查询现有的资产和资产才能更新它们?

我确信这个问题已经有了答案,但我不知道如何搜索才能找到它。

提前致谢。相关映射如下

public FundMap() 
{
...
HasMany(x => x._holdings)
    .Cascade.All();
}

public HoldingMap()
{
...
References(x => x._fund);
References(x => x._asset).Cascade.SaveUpdate();
...
}

I am new to Fluent NHibernate and ORM tools in general. I'm not exactly sure what I should be expecting from the tool in this situation. I have Fund, Holding, and Asset tables with corresponding objects. The Fund object has a collection of Holdings and each holding has an Asset. When I save the fund I would like the subsequent holdings and assets save as well.

I seem to have that happening, however, I seem to be attempting to insert a new record into each table rather than updating the existing ones. I have unique constraints in the db so the saves are failing. Do I need to be querying the existing holdings and assets to get them to update?

I'm sure there has already been an answer for this question, but I couldn't figure out how to search to find it.

Thanks in advance. Relevant Mappings below

public FundMap() 
{
...
HasMany(x => x._holdings)
    .Cascade.All();
}

public HoldingMap()
{
...
References(x => x._fund);
References(x => x._asset).Cascade.SaveUpdate();
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2024-12-09 01:36:03

您不需要查询现有的集合,这不是必需的。您可以创建一个新对象并为其提供与数据库中对象相同的键,调用 SaveOrUpdate() ,它将更新数据库。

您通常查询数据库的原因是因为您根据键以外的其他条件进行搜索。这也会获取与该实体相关的关系对象。就您而言,是一个拥有许多“控股”的“基金”。

例如,
如果您的数据库中有一个密钥为 1 的现有基金以及与该基金关联的控股,并且该特定控股的密钥为 1,您可以执行以下操作

var fund = new Fund{
   ID = 1,
   Name = "YourNewName",
   Holdings = new List<Holding>{
     new Holding{
       ID = 1
       Description = "NewHoldingDescription"
     }
   }
}

//and then perform a SaveOrUpdate() on the fund, it should update the entries in the database

You don't need to query the existing set, this is not a requirement. You could just create a new object and give it the same key of the object in the database, call SaveOrUpdate() and it will update the database.

The reason you generally query the database is because you search on other criteria other than the key. This also grabs relational objects tied to this entity. In your case, a "Fund" that has many "Holdings".

For Example,
if you had an existing Fund in the database with the key of 1 and a Holding associated with the Fund and that particular Holding had the key of 1, you could do the following

var fund = new Fund{
   ID = 1,
   Name = "YourNewName",
   Holdings = new List<Holding>{
     new Holding{
       ID = 1
       Description = "NewHoldingDescription"
     }
   }
}

//and then perform a SaveOrUpdate() on the fund, it should update the entries in the database
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文