title: Mapping many to one table relation to view
body: <p>I am trying to map a many to one relationship. I feel I'm close but not quite there.</p>
  
  <p>I have this sensor table:</p>
  
  <pre><code>SensorId
  FK_LocationId
  Name
  etc...
  </code></pre>
  
  <p>Which holds many data records in the <code>Data</code> Table.</p>
  
  <pre><code>DataId
  FK_SensorId
  Time
  Value
  </code></pre>
  
  <p>And I am trying to create a Model for this.</p>
  
  <pre><code>public class DataSensor
  {
      public int Id { get; set; }
      public int DataNodeId { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public bool active;
      public bool alarm;
  }
  
  public class GatheredData
  {
      public int Id { get; set; }
      public int DataSensorId { get; set; }
      public DateTime Time { get; set; }
      public float value { get; set; }
  
      [ForeignKey("DataSensorId")]
      public virtual DataSensor datasensor { get; set; }
  }
  </code></pre>
  
  <p>It should actually be the other way around, at least in my mind. Where the sensor would hold a <code>List&lt;Data&gt;</code> but there is no FK to link that in Sensor. A <code>Data</code> record is just mapped to a sensor by the FK of that sensor.</p>
  
  <p>The problem I'm facing here is that I would have this line in my view now:</p>
  
  <pre><code>@model IEnumerable&lt;DataVisualization.Models.Data&gt;
  </code></pre>
  
  <p>And instead of looping over my sensors to display the information and then show the data (eventually a chart). I have to loop all the data, somehow organize it how I want and then display it. So I would still need:</p>
  
  <pre><code>@model IEnumerable&lt;DataVisualization.Models.DataSensor&gt;
  </code></pre>
  
  <p>But this does not give me access to the data since that is in <code>Data</code> and <code>DataSensor</code> does not expose any of that afaik. So I thought about somekind of class that maps them together:</p>
  
  <pre><code>public class DataViewModel
  {
      public DataSensor dataSensor { get; set; }
      public List&lt;GatheredData&gt; gatheredData { get; set; }
  }
  </code></pre>
  
  <p>And my view would require:</p>
  
  <pre><code>@model IEnumerable&lt;DataVisualization.Models.DataViewModel&gt;
  </code></pre>
  
  <p>This seemed an elegant way but I was not able to make it work. Probably since this would require <code>public DbSet&lt;DataViewModel&gt; dataViewModel { get; set; }</code> in the <code>DbContext</code> and that would produce a awkward table in my database.</p>
  
  <p>So any help on how to create the Model, work with it in the Controller and displaying it in the View would be greatly appreciated.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>What about this Model so I have access to the data connected to this?</p>
  
  <pre><code>public class DataSensor
  {
      [Key]
      public int Id { get; set; }
      public int DataNodeId { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public bool active;
      public bool alarm;
  
      public virtual ICollection&lt;DataSensor&gt; Data { get; set; }
  }
  </code></pre>
  
  <p>However, this creates a column <code>DataSensor_Id</code> in the database table. This obviously is undesirable since then it would be a one to one.</p>