My application uses asp.net 4, C# and Entity Framework. My database contains a table Companies which has many Locations, which in turn has many Devices. A Company is keyed to the aspnet_user which is currently logged in.
I use grid views to display and edit the Locations and Devices for a single Company. This is easily achieved for the Locations as they have a direct link to a Company…
LocationListEntityDataSource.AutoGenerateWhereClause = true;
LocationListEntityDataSource.WhereParameters.Clear();
LocationListEntityDataSource.WhereParameters.Add("CompanyGuid", System.Data.DbType.Guid, Tools.Tools.getCompanyGuidString());
However I can not work out how to filter the Devices by Company as they only have a link to a Location. I had hope to use something similar to the following, but I get an exception stating that Location.CompanyGuid could not be found…
DeviceListEntityDataSource.AutoGenerateWhereClause = true;
DeviceListEntityDataSource.WhereParameters.Clear();
DeviceListEntityDataSource.WhereParameters.Add("Location.CompanyGuid", System.Data.DbType.Guid, Tools.Tools.getCompanyGuidString());
Any ideas on how this can be achieved?
Consider Company to have one to many relationship with Locations as well as Devices.
When you put a virtual property of
ICollection<Device> Devices;to theCompany class. This will be just a navigation property. This will not create any column in company table for Devices. Then you have virtual property Company comapany and FK int CompanyId in the Device model. This will only add one field toDevicetable which is not big wastage of space.