Quantcast
Channel: EntityFramework Reverse POCO Code First Generator
Viewing all articles
Browse latest Browse all 1642

New Post: Views - LeftJoin - PrimaryKey(s)

$
0
0
Hi Mathias,

EF won't work with any view/table without a primary key.
Because you are joining to the dbo.OrderDetail, the dbo.[Order].Id will be duplicated for every row of order details, and therefore cannot be used as a primary key due to it not being unique. The generator is then forced to use all your columns that are not nullable as primary keys. Hence the primary key being defined as HasKey(x => new { x.OrderId, x.CustomerId, x.ShippingName, x.ShippingAddress, x.ShippingCity, x.ShippingRegion, x.ShippingZip, x.ShippingCountry, x.IsCancelled });

However, you are seeing a problem as even with all these columns selected, your data is creating a duplicate primary key.
Using ROW_NUMBER(), NEWID() or NEWSEQUENTIALID() are all ideal, as this guarantees a unique column per row.

The only problem with using a "select * from..." is that you no longer specify a WHERE clause in the database, and it will be done in LINQ after you have retrieved all the data from the database, which could be huge. This is definitely the wrong solution. Just wait till that view returns a million rows back to your application before your linq query gets its hands on it.

Viewing all articles
Browse latest Browse all 1642

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>