fbpx
Welcome, Guest
Username: Password: Remember me
Share your code snippets, screen shots etc. here
  • Page:
  • 1

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

Entity Framework 6 - finally, success with an Entity 'mapping' 2 years 6 months ago #808

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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 :-



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' ) :-



Here below is the simple WPF data binding :-



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'



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)
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by robert.

Entity Framework 6 - finally, success with an Entity 'mapping' 2 years 6 months ago #809

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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.







Regards,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

Entity Framework 6 - finally, success with an Entity 'mapping' 2 years 6 months ago #810

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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 :-



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 :-



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..







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 :-







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.

Please Log in or Create an account to join the conversation.

Entity Framework 6 - finally, success with an Entity 'mapping' 2 years 6 months ago #811

  Phil Hepburn's Avatar Topic Author Phil Hepburn Offline Posts: 742
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.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1