Entity Framework 6 - finally, success with an Entity 'mapping'

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Entity Framework 6 - finally, success with an Entity 'mapping'

Post by Phil Hepburn »

Hi Robert, Team, and guys,

Well, I thought you may wish to know that yesterday I got my first X# successful mapping of an Entity, albeit a simple one.

I have a simple 'Supplier' class with properties of just value type. I then have a second class called 'Product' which has mainly value type properties but also one other, a 'virtual' property which is itself of business class type 'Supplier'.

The following image shows the seed data bound to two data grid controls :-
PearlsEF6_31.jpg
PearlsEF6_31.jpg (78.66 KiB) Viewed 237 times
In the top data grid you can see the Supplier object as the ToString() method involves three of its value type properties.

Now the good thing about Entity mapping is that the LINQ code is still simple - no need for JOINs which many 'fear', or hate (Nick!).

Here is the code to retrieve the Product data (Image '_32' ) :-
PearlsEF6_32.jpg
PearlsEF6_32.jpg (38.18 KiB) Viewed 237 times
Here below is the simple WPF data binding :-
PearlsEF6_33.jpg
PearlsEF6_33.jpg (41.99 KiB) Viewed 237 times
Of course if the query using LINQ is simple then the complexity has to be in the mapping and also for me in how I seed the test data. Below is some code for table population - note that the Supplier table data needs to be seeded (and in place in the SQL database) before this Product seed code is run :- image '_35'
PearlsEF6_35.jpg
PearlsEF6_35.jpg (86.83 KiB) Viewed 237 times
If we seed and populate in the wrong order we can get this - see below :- (image '_36')

*** Note 36,37,38 images in next post - too many links !!! Robert ? ***
... image 36 ...

Reversing these lines of code get us success once again :- (image '_37' )

image '_37'

Now you may like to see just what a simple change I had to make to the Product class definition to bring all this about - get stuff to happen. Well below we need to view line 276, that's all, and see that the other properties are on lines 254-256. Most of the rest of the class definition has been collapsed so we can see clearly what is going on, even if it is not much ;-0)

image '_38 (next post)

So for those people who understand English and British humour we could say that "nobody can see the join ! "

Regards,
Phil.

P.S. I will now go and add more classes like 'Order' and 'OrderLine', 'Customer' and 'Invoice', and make Entity mappings right from their inclusion (from the start).
Oh! and by the way, I am managing in VS without any Intellisense, but it would be nice to have what Frabrice is working on for us ;-0)
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Entity Framework 6 - finally, success with an Entity 'mapping'

Post by Phil Hepburn »

Robert - this post has been a real painful experience at this end ;-0((

I had to cut my small image attachments down to four (three short of what I wished).

I will now try and attach the rest - BUT - the reading experience for the viewers will be poorer than I wished.

Can you please increase the max to about seven ?

Its best (for me) to have small images closer to the explanation text than one or two larger ones.

Its hard enough researching and posting without technical issues such as this.
PearlsEF6_36.jpg
PearlsEF6_36.jpg (61.84 KiB) Viewed 237 times
PearlsEF6_37.jpg
PearlsEF6_37.jpg (75.68 KiB) Viewed 237 times

PearlsEF6_38.jpg
PearlsEF6_38.jpg (112.1 KiB) Viewed 237 times
Regards,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Entity Framework 6 - finally, success with an Entity 'mapping'

Post by Phil Hepburn »

Good day Robert and all colleagues,

In case you think my posts show that I always have success, and hit no issues, be assured that that is definitely NOT the case! Yesterday I had one of those 'backwards' days, but I have learned a LOT from the problems and issues - be most careful when 'seeding' a newly created database based on Entity 'mapping'. The Foreign Key constraints built into the 'Code First' solution by EF6 can bite you (hard). For a good laugh at my expense, see the error message sent back to me from the SQL Server - image '_45' but I won't insert it, just attach it, for obvious reasons when you try to view it!

Once I got my head straight about Customer / Order identities and FK constraints, then this morning was progress as usual, more or less, - thank God. Here in image '_46' is what I was after yesterday :-
PearlsEF6_45.jpg
PearlsEF6_45.jpg (95.92 KiB) Viewed 237 times
The main WPF form has a data grid control with the collection of Entities 'bound' to it.

Notice that because the oOrderList is a 'virtual property' of the business class 'Customer', we need to display the list in a drop list, or combo box control.

The entity framework mapping has done its stuff and joined Customer / Order tables on the 'CustomerId' value (or column), I would guess. No work at all by me/us. See the next image '_49' to see how easy it is to bind the list of entities to the WPF form, line 64 does it all :-
PearlsEF6_49.jpg
PearlsEF6_49.jpg (107.99 KiB) Viewed 237 times
If you are interested to see how easy it is to script the XAML required for the templates, then the three image files '_47', '_48', and '_50' will show you how. This is lifted almost directly from my Cologne 2015 example and eNotes, on data binding and EF6. I just found it and copied etc..
PearlsEF6_47.jpg
PearlsEF6_47.jpg (58.9 KiB) Viewed 237 times

PearlsEF6_48.jpg
PearlsEF6_48.jpg (38.9 KiB) Viewed 237 times

PearlsEF6_50.jpg
PearlsEF6_50.jpg (53.9 KiB) Viewed 237 times
Just so you know, the actual data is still stored in 'flat' tables in the database, it is the EF6 'mapping' in my .NET solution which makes all the difference, and also does most of the work. here are the Customer and Order tables after the Seeding and Population - check them out :-
PearlsEF6_41.jpg
PearlsEF6_41.jpg (33.82 KiB) Viewed 237 times

PearlsEF6_42.jpg
PearlsEF6_42.jpg (67.64 KiB) Viewed 237 times

PearlsEF6_43.jpg
PearlsEF6_43.jpg (68.78 KiB) Viewed 237 times
Remember, that all the SQL database / table creation and data seeding is done by the EF6 support stuff, all I am doing is making a .NET Entity 'mapping' - which quite frankly is reasonably easy to do - and certainly easy to copy and adapt from another working example.

Thanks to Nick Friend for his help and support with EF6, and an example or two!

Now I am on my way to 'OrderLine' and 'Invoice' - wish me luck, and no nasty huge error messages ;-0)

Best Regards,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Entity Framework 6 - finally, success with an Entity 'mapping'

Post by Phil Hepburn »

SORRY guys !

The image / file 'Insert and Attach' system in the Form posting editor appears to have a mind of its own !?

Checkout the image numbers to go with the correct text parts.

I will try better next time - however, we no longer have a restriction on the number of links / attachments.

Cheers for now,
Phil.

P.S. if you are interested in this EF and X# work with LINQ then plan your schedule to come to Cologne in April - provided that Meinhard and Michael allow me to do a couple of sessions on it. Fingers crossed.
Post Reply