views:

227

answers:

2

Hi,

i have an UI object VehicleUI which is inherited from Vehicle. It's the same, but it has some extra propertys for the user entry process.

Now i want to save the VehicleUI to the database in the table Vehicle (using the entityframework). In my opinion this must be possbile because the VehicleUI has all the properties of the Vehicle, plus some extra. I can call the context.AddToVehicle, but at runtime i get an error saying there is no mapping for the VehicleUI ( which is correct, because i have only a mapping for the Vehicle)

So i tried casting the VehicleUI to a Vehicle and then save it, but even when i cast it the Type still remains a VehicleUI and not a Vehicle.

So want i want to do is create a new fresh Vehicle object based on the VehicleUI object, but ofcourse without me typing vehicle.x = vehicleui.x, vehicle.y = vehicleui.y etc etc

Is there a way to create a new vehicle and say: i give you an inherited object, please copy all the info from the propertys you have in common.

Michel

+2  A: 

To get your inherited vehicleUI to work with the datacontext, I think you'll have to change the XML mapping file, which is what maps the database to the object and vice versa.

Have a look here under the mapping section.

Tony
Bu at that point i won't be able to update the model automaticly?
Michel
If you do it manually, I guess not, no. Why don't you just use your vehicleUI as an intermediary class between your UI and database context? Assign VehicleUI properties to Vehicle's properties to update database...
Tony
That's what i'm trying to do, but i was looking for a way to a avoid me coding all properties like A.x=B.x, A.y=B.y to do what you say: ' Assign VehicleUI properties to Vehicle's properties '. I was looking to some kind of automatic way, first because the class contains 25 properties, and second because i won't forget one when i update a class and forget to update the 'assign' routine.
Michel
Inherit and implement INotifypropertychanged interface in your VehicleUI class and handle event in your Vehicle class with assigning properties in event handler. Now everytime properties change in VehicleUI class, will automatically change in Vehicle class
Tony
+1  A: 

Object instances can't change their type, ever. I don't think having VehicleUI be a subtype of Vehicle gains you much; they're still different types.

Most people use a tool like AutoMapper (or simpler versions of same) to do the kind of mapping you're describing when saving to the DB.

When creating a VehicleUI instance for display, use projection instead.

Craig Stuntz
Yeah, the projection is what i do now, but i wanted to avond typing all the properties, like LastName = e.Person.LastName etc.
Michel
Did try the T4 template also for generating this code for me, by looping all the class' properties and generate the XX= E.Person.XX code. However, i had to reference the DLL in which the object is located for that, and on compile the T4 template would lock it so the compiler couldn't compile :(
Michel
I think the projection could be generated in code. Write a function which returns `Expression<Func<TEntity, TPresentation>>`. Then call `.Select(CreateProjection<Vehicle, VehicleUI>())`.
Craig Stuntz