I'm getting started with Entity Framework and need some help understanding the functionality between two Tables. To start with I have PhoneRecords
and PhoneModels
. Each PhoneRecord
has a property called PhoneModelId
that relates to one of the PhoneModels
. In MySQL these properties are setup as a foreign key relationship.
Now I've loaded a sample of PhoneRecords
into a DataGrid
. I have a TextBox
that I want to reflect the model on the PhoneRecord
. At present my TextBox
looks like so:
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectedRecord.PhoneModelId}"/>
and, unsurprisingly, the Id
is displayed instead of the Name
of the PhoneModel
. Usually without EntityFramework I would just have a property on the PhoneRecord
itself called PhoneModelName
or something similar which I would get using a JOIN
when writing out my SELECT
commands..
It seems to me that that method is old fashioned, and that Entity Framework is a powerful tool for getting around writing long CRUD operations. However, I am stuck in seeing how I am able to bind in MVVM to the PhoneModel
name rather than using the PhoneModelId
property on the PhoneRecord
.
Try something like:
"{Binding SelectedRecord.PhoneModel.Name}"
I'm not sure which version of EF you're using or which flavour (code-first, DB first, code-first with existing database, etc.)... but...
In EF if you have an object that has a foreign key relationship it will attach that object to it's parent or child. So each PhoneRecord will have a PhoneModel object attached to it, and vice versa. If you're using code first, you'll have to code this relationship, if you're using DB first it should already exist int the model (EDMX) and you should be able to navigate to a PhoneModel from a PhoneRecord and back again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.