开发者

What can I expect from Fluent NHibernate?

开发者 https://www.devze.com 2023-03-31 11:28 出处:网络
I am new to Fluent NHibernate and ORM tools in general. I\'m not exactly sure what I should be expecting from t开发者_运维问答he tool in this situation. I have Fund, Holding, and Asset tables with cor

I am new to Fluent NHibernate and ORM tools in general. I'm not exactly sure what I should be expecting from t开发者_运维问答he 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();
...
}


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号