Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add an item to an ISet<T>?
    primarykey
    data
    text
    <p>I have three classes called <code>Broker</code>, <code>Instrument</code> and <code>BrokerInstrument</code>.</p> <pre><code>using Iesi.Collections.Generic; public class Broker : ActiveDefaultEntity { public virtual string Name { get; set; } public virtual ISet&lt;BrokerInstrument&gt; BrokerInstruments { get; set; } } public class Instrument : Entity { public virtual string Name { get; set; } public virtual string Symbol {get; set;} public virtual ISet&lt;BrokerInstrument&gt; BrokerInstruments { get; set; } public virtual bool IsActive { get; set; } } public class BrokerInstrument : Entity { public virtual Broker Broker { get; set; } public virtual Instrument Instrument { get; set; } public virtual decimal MinIncrement { get; set; } } </code></pre> <p>If I create three new objects (one of each type) using those classes how to I associate them? For example:</p> <pre><code>Instrument instrument = new Instrument { Name = "Test Instrument", Symbol = "Test", IsActive = true }; Broker broker = new Broker { Name = "My Test Broker", IsActive = true, IsDefault = false }; BrokerInstrument brokerInstrument = new BrokerInstrument { Broker = broker, Instrument = instrument, MinIncrement = 0.01M }; </code></pre> <p>How does the <code>instrument</code> "know" that a new <code>brokerInstrument</code> is now associated with it? If I now run <code>if (instrument.Brokerinstruments == null)</code> I get <code>true</code>. Do I have to associate the objects in both the <code>BrokerInstrument</code> declaration and then go back and add it to the <code>instrument.BrokerInstruments ISet</code>?</p> <p>If I try and do: <code>instrument.BrokerInstruments.Add(instrument)</code> I get an error because its null. Confused. What am I missing? What is the best way to model relationships like this? these objects will get persisted to a database using NHibernate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload