Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Is it at all possible to add a view to the Entity model without a unique identifier?</p> </blockquote> <p>If without a primary key, no. That will result to this kind of <a href="http://www.ienablemuch.com/2011/06/mapping-class-to-database-view-with.html" rel="noreferrer">error</a>:</p> <blockquote> <p>One or more validation errors were detected during model generation:</p> <p>System.Data.Edm.EdmEntityType: : EntityType 'SalesOnEachCountry' has no key defined. Define the key for this EntityType. System.Data.Edm.EdmEntitySet: EntityType: The EntitySet SalesOnEachCountryList is based on type SalesOnEachCountry that has no keys defined.</p> </blockquote> <p>If without a unique identifier, yes, albeit it has a non-desirable output. Records with same identifier would reference the same object, this is called <a href="http://en.wikipedia.org/wiki/Identity_map_pattern" rel="noreferrer">Identity Map Pattern</a> </p> <p>An example, even if your view produces these two rows:</p> <pre><code>Country Year TotalSales Philippines 2010 20.000000 Philippines 2011 40.000000 </code></pre> <p>If you will just map the primary key on Country field only, e.g. </p> <pre><code>public class SalesOnEachCountry { [Key] public int CountryId { get; set; } public string CountryName { get; set; } public int OrYear { get; set; } public long SalesCount { get; set; } public decimal TotalSales { get; set; } } </code></pre> <p>, even your view produces the above two rows on your Oracle query editor, Entity Framework produces this incorrect output:</p> <pre><code>Country Year TotalSales Philippines 2010 20.000000 Philippines 2010 20.000000 </code></pre> <p>Entity Framework will take it that the second row is same object as first row.</p> <p>To guarantee uniqueness, you must identify what columns that makes each row unique. In the above example, Year must be included so the primary key is unique. i.e. </p> <pre><code>public class SalesOnEachCountry { [Key, Column(Order=0)] public int CountryId { get; set; } public string CountryName { get; set; } [Key, Column(Order=1)] public int OrYear { get; set; } public long SalesCount { get; set; } public decimal TotalSales { get; set; } } </code></pre> <p>Making your primary key similar to the attributes above, Entity Framework can correctly map your each view's row to their own objects. Hence, Entity Framework can now display exactly the same rows your view have.</p> <pre><code>Country Year TotalSales Philippines 2010 20.000000 Philippines 2011 40.000000 </code></pre> <p>Full details here: <a href="http://www.ienablemuch.com/2011/06/mapping-class-to-database-view-with.html" rel="noreferrer">http://www.ienablemuch.com/2011/06/mapping-class-to-database-view-with.html</a></p> <hr/> <p>Then regarding your views which don't have any columns to make a row unique, the easiest way to guarantee Entity Framework can map each of your view's row to their own objects is to create a separate column for your view's <em>primary key</em>, a good candidate is to just create a row number column on each row. e.g.</p> <pre><code>create view RowNumberedView as select row_number() over(order by &lt;columns of your view sorting&gt;) as RN , * from your_existing_view </code></pre> <p>Then assign the <code>[Key]</code> attribute on RN property of your <code>class RowNumberedView</code></p>
 

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