简体   繁体   中英

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 . This table will contain zero or many dynamic records (Free text key and free text value) from a table called PersonDynamicInfo .

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

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

    public PersonDynamicInfo(){ Id(x => x.Id); Table("PersonDynamicInfo"); Map(x => x.Key);
    Map(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. Let's say for example that I create a person with a record on PersonDynamicInfo with the values: " Key: A | Value : 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. What will happen is that the Person.PersonDynamicInfo will contain the two records instead of only containing the last one.

Is there way I can achieve this without manually deleting the unused records on PersonDynamicInfo?

Many thanks!

I think what you need is a reference to the Person in your PersonDynamicInfo mapping:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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