简体   繁体   English

流利的NHibernate,具有键/值列关系

[英]Fluent NHibernate with Key/Value Column Relation

I'm trying to create a table that will work as additional data for another table. 我正在尝试创建一个表,该表将用作另一个表的其他数据。

  • For example I have my main table called Person . 例如,我有一个名为Person的主表。 This table will contain zero or many dynamic records (Free text key and free text value) from a table called PersonDynamicInfo . 该表将包含零个或多个来自称为PersonDynamicInfo的表的动态记录(自由文本键和自由文本值)。

    public PersonMap(){ Id(x => x.Id); 公共PersonMap(){Id(x => x.Id); Table("Person"); Table(“ Person”); Map(x => x.Name); Map(x => x.Name);

    HasMany(x => x.PersonDynamicInfo) .KeyColumn("Id") .Access.CamelCaseField(Prefix.Underscore) .AsSet() .Cascade.AllDeleteOrphan() .Not.KeyNullable(); HasMany(x => x.PersonDynamicInfo).KeyColumn(“ Id”).Access.CamelCaseField(Prefix.Underscore).AsSet().Cascade.AllDeleteOrphan().Not.KeyNullable(); } }

    public PersonDynamicInfo(){ Id(x => x.Id); 公共PersonDynamicInfo(){Id(x => x.Id); Table("PersonDynamicInfo"); Table(“ PersonDynamicInfo”); Map(x => x.Key); Map(x => x.Key);
    Map(x => x.Value); 映射(x => x.Value); } }

What I want to achieve is the following: 我要实现以下目标:

  • When I access the Person object, I want to be able to edit Person.PersonDynamicInfo so that the information on this table is the same as the object. 访问Person对象时,我希望能够编辑Person.PersonDynamicInfo,以使此表上的信息与该对象相同。 Let's say for example that I create a person with a record on PersonDynamicInfo with the values: " Key: A | Value : TEST ". 例如,假设我创建了一个在PersonDynamicInfo上具有记录的人,其值是:“ 键: A | :TEST ”。 When I access the Person object I will remove all the Person.PersonDynamicInfo (from the collection) and add a new one: " Key: B | Value : TEST " and Save() this Person object. 当我访问Person对象时,我将从集合中删除所有Person.PersonDynamicInfo并添加一个新对象:“ 键: B | :TEST ”并保存()此Person对象。 What will happen is that the Person.PersonDynamicInfo will contain the two records instead of only containing the last one. 将会发生的是Person.PersonDynamicInfo将包含两个记录,而不是仅包含最后一个。

Is there way I can achieve this without manually deleting the unused records on PersonDynamicInfo? 有没有我可以手动删除PersonDynamicInfo上未使用的记录的方法?

Many thanks! 非常感谢!

I think what you need is a reference to the Person in your PersonDynamicInfo mapping: 我认为您需要的是在PersonDynamicInfo映射中对Person的引用:

public PersonDynamicInfo(){ 
    Id(x => x.Id); 
    Table("PersonDynamicInfo"); 
    Map(x => x.Key);
    Map(x => x.Value);
    References(x => x.Person, "PersonId").Cascade.All(); 
} 

I may have misunderstood your question, however. 但是,我可能误解了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM