the User class contains 3 properties: UserId of type int, Name of type string and Address of my custom type Address.
Address contains 4 string properties: Streetname, Housenumber, Zipcode and City.
Now I would like the forward mapping engine to generate a table like:
UserId (int NOT NULL)
What would be considered 'best practice' to forward map this model to a SQL Server table in this manner?
I saw a thread about Type Conversion here.
Implementing the INotifyPropertyChanged interface, would that be recommended in this case as well?
6 Answers, 1 is accepted
Mapping two classes to a single table is not supported by default. A workaround for your case would be to declare the Address class as a persistent struct. Fields of structs are added as columns to the table of the persistent class. That way you would reach the desired effect of having the User and Address fields in one table.
the Telerik team
That sounds like exactly what I need!
So I just need to change the Address class to a persisten struct then.
Makes sense for my domain model, because Address is simply a structure for holding related data together.
I'll give it a try!
Well, that works as espected, great!
However, now I have something more sophisticated, like this:
(for brevity, I ommited all FieldAlias attributes etc. I made sure I didn't screw up there)
This works great when saving data into the database and even when retrieving Users.
But when I try querying on the Firstname property of the Name property of the PersonalInfo property of the Profile property of a User, I get a runtime error: 'Field 'Name' not found on class 'Profile''
For example, take this query:
It simply throws up, while the next query:
works without any problems.
My guess is that it has something to do with having nested complex struct types.
If that's the case, how can I work around this? Transforming the structs into classes is not an option, because I want everything stored in just two tables: Users and Profiles.
You may download the sample project here.
As I answered in the support ticket that you sent to us, the problem is related to resolving the FieldAlias attribute of the Name property. The workaround is to expose the name field as public and use it directly in the Linq query:
While the problem is being fixed, this could be helpful if someone faces the same issue.
the Telerik team
That workaround works around the issue, thank you!