Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why are you using a datagrid for a single entity? I definitely would not stick an ObservableCollection property in your model to cover this.</p> <p>If you are wanting to show all entity2s for all entity1s. You can bind the ItemsSource of your datagrid to a collection of entity1 and drill in to the properties of entity2.</p> <p>Another option is to build a custom form for presenting the entity2 data.</p> <p><strong>Attempt 1</strong></p> <p><strike> You can definitely drill into the properties in Entity2 from Entity1. Try this:</p> <ol> <li>Populate an ObservableCollection with the instances of Entity1 that you want to use. </li> <li>Set the ItemsSource on your DataGrid to that ObservableCollection </li> <li>Set AutoGenerateColumns to false on your DataGrid </li> <li>Add the columns you desire to the datagrid setting the binding path to be Entity2.PropertyName</li> <li>You may have to temporarily create an instance of Entity2 for each Entity1 for this to work. </strike></li> </ol> <p><strong>Attempt 2</strong> Entity Framework will allow for a single entity to be constructed from two tables. You may want to consider that implementation. The big question there is "do I really care if I have worthless records in Table2 if Table2 only extends Table1?"</p> <p><strong>Attempt 3</strong> Instead of working with Entity1 and Entity2 directly, I think you should introduce a new model into your domain. Having a class that would wrap the properties of Entity1 and Entity2 is probably going to be the best choice. Then, when you are ready to update the database, you can determine if you have an instance of Entity2. </p> <p>Following the WPF Application Framework, you might have something like this:</p> <pre><code>//In your application layer public class RecordsViewModel { public ObservableCollection&lt;Record&gt; Records {get;set;} } //In your domain layer public class Record { //Properties from Entity1 //Properties from Entity2 } //In your Data Access Layer public class Repository { public IEnumerable&lt;Record&gt; GetRecords() { return db.Entity1.Include("Entity2") .Select(e =&gt; new Record() { //object initialization }); } public void Update(IEnumerable&lt;Record&gt; records) { var recordIds = records.Select(r =&gt; r.Id); var entities = db.Entity1.Include("Entity2").Where(e =&gt; recordIds.Contains(e.Id)); foreach(var record in records) { var entity = entities.SingleOrDefault(e =&gt; e.Id == record.Id); if (entity == null) { entity = new Entity1(); db.Entity1.Add(entity); } //update properties on Entity1 //check if Entity2 should exist //If so, create/update entity2 //If not, decide if you should delete entity2 or simply set Entity1.Entity2 to null } } } </code></pre>
 

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