I have a couple of tables which are related using a multiple field key, but depending on where the application is deployed, the key may be different.
For example:
-Customer- -Rep-
CompanyID CompanyID
StoreID StoreID
RepID RepID
CustID
In some deployments a Rep’s Customers are identified by joining on CompanyID, StoreID, RepID. In other deployments the Rep’s StoreID will be a string of blanks and then the join is on CompanyID, RepID. The Customer will still have values in StoreID.
In SQL I just put an ‘OR StoreID = ” “‘ where we join on the StoreID fields, but I don’t see how I can do that with the entity framework.
I could set up one association for each situation and then use the appropriate one, but unless I can at runtime alias the name to the appropriate association, this would make the application code a bit messy.
Edit:
It looks like I can use the MetadataWorkspace to create the necessary association on-the-fly. I will investigate this.
Edit:
This kind of problem can be solved by splitting the model to use different csdl files (where the associations are defined) depending on which behavior is required. See MerickOWA’s answer and the MSDN article linked. This introduces some minor annoyances, primarily that the designer doesn’t support it, so you’ll be reduced to editing XML files, and also you’ll have to control which csdl resource is used. The csdl is specified in the EF connection string, which by default is in the app.config. To simplify managing this string (I needed control over the database connect string anyway), I wrote a utility class to build my EF connection string. I based this class on the code I found in Fixing the Config: Using Entity Framework in shared libraries.
One possibility you might look into is trying to split up the edmx file into its subparts the CSDL, SSDL, and MSL.
The CSDL is your entity object layout while your SSDL is your table layout and the MSL maps between the two.
Possibly you could try having one CSDL with two or more SSDL/MSL to handle databases with different column layouts.
I don’t have alot of experience using this advance situation but it certainly seems possible.
Check out this article on MSDN