views:

183

answers:

2

Currently I've been working with Entity Framework 1.0 which is located under a service façade.

Below is one of the save methods I've created to either update or insert the device in question.

This currently works but, I can't help feel that its a bit of a hack having to set the referenced properties to null then re-attach them just to get an insert to work. The changedDevice already holds these values, so why do I need to assign them again.

So, I thought I'll update the model to EF4. That way I can just directly access the foreign keys. However, on doing this I've found that there doesn't seem to be an easy way to add the foreign keys except by removing the entity from the diagram and re-adding it. I don't want to do this as I've already been through all the entity properties renaming them from the DB column names. Can anyone help?

 /// <summary>
    /// Saves the non network device.
    /// </summary>
    /// <param name="nonNetworkDeviceDto">The non network device dto.</param>
    public void SaveNonNetworkDevice(NonNetworkDeviceDto nonNetworkDeviceDto)
    {
        using (var context = new AssetNetworkEntities2())
        {
            var changedDevice = TransformationHelper.ConvertNonNetworkDeviceDtoToEntity(nonNetworkDeviceDto);
            if (!nonNetworkDeviceDto.DeviceId.Equals(-1))
            {
                var originalDevice =
                context.NonNetworkDevices.Include("Status").Include("NonNetworkType").FirstOrDefault(
                  d => d.DeviceId.Equals(nonNetworkDeviceDto.DeviceId));
                context.ApplyAllReferencedPropertyChanges(originalDevice, changedDevice);
                context.ApplyCurrentValues(originalDevice.EntityKey.EntitySetName, changedDevice);
            }
            else
            {
                var maxNetworkDevice = context.NonNetworkDevices.OrderBy("it.DeviceId DESC").First();
                changedDevice.DeviceId = maxNetworkDevice.DeviceId + 1;
                var status = changedDevice.Status;
                var nonNetworkType = changedDevice.NonNetworkType;
                changedDevice.Status = null;
                changedDevice.NonNetworkType = null;
                context.AttachTo("DeviceStatuses", status);
                if (nonNetworkType != null)
                {
                    context.AttachTo("NonNetworkTypes", nonNetworkType);
                }

                changedDevice.Status = status;
                changedDevice.NonNetworkType = nonNetworkType;
                context.AddToNonNetworkDevices(changedDevice);
            }

            context.SaveChanges();
        }
    }
+1  A: 

You are going to need to edit the EDMX file in an XML editor then. Maybe update one table automatically, DIFF the EDMX file to see what changed and then go in and edit all the others.

Renaming all your field names was a bold but risky move if you are using a database-first approach. Renaming the fields in the database to better names would be preferable if you have that flexibility.

The other issue you will encounter is that EF4 is going to want to pluralize your entity names unless you ask it not to. If you do update the model from EF1 to EF4 and let it pluralize the names remember to go check any weakly typed Include() calls, and while fixing them change them to one of the many strongly typed .Include()'s you'll find on the web.

Hightechrider
Thanks Hightechrider, although I was hoping there might be an easier way! For this project I've just struggled on with EF1 and managed to build some extension methods which preform the tasks I need. When you mention Strongly Typed includes do you mean what is described here http://mattias-jakobsson.net/Item/36/Strongly%20typed%20include%20in%20EF
duthiega
As mention above, I built some extension methods. The process I used I blogged http://gordonduthie.net/2010/04/29/saving-entities-with-references-in-entity-framework-using-extension-methods-2/
duthiega
Yes, that's one of the strongly typed Include options. Alex James has another.
Hightechrider
A: 

I had the same issue this is how I ended up fixing my model.

You should be able to recreate your relationship. If you set it as a many to one relationship you will get the check box for Add foreign key property to enabled and you can check it to create add the property to your entity. Don't forget to fix up the name.

You can also access the relationship details by double clicking on the line in the diagram.

Note the property created needed to be mapped to the FK field in the DB.

SzabV