Article information
Article relates to
Telerik OpenAccess ORM
Created by
Ivailo Ivanov
Last modified
July 27, 2011
Last modified by
EntitiesModel model =
new
EntitiesModel();
var exceptionQuery = from objectA
in
model.TableAs
where objectA.PrimaryKeyA == 1
select objectA;
In order to avoid it, there is only one approach that is not error prone – to split the ForeignKeyA into two columns, each of them participating in only one foreign key relation. In our example, let’s leave the first column as it is and create a new column called TableBForeignKeyA. Here is the sequence of actions for implementing the change properly:
For reverse mapping:
1. Create a new column in the database table (TableBForeignKeyA) with the same type as ForeignKeyA 2. Reallocate the foreign key between TableA and TableB to point to the new column in TableA (TableBForeignKey instead of ForeignKeyA) 3. Open your Domain Model 4. Update from database TableA and TableB 5. Remove the classes TableA and TableB 6. Add the classes again (to ensure the associations are properly refreshed) 7. Save the Domain Model
For forward mapping:
1. Add new property in the Domain Model TableA class and name it TableBForeignKeyA. The type should be the same as ForeignKeyA’s type. 2. Double-click on the association between TableA and TableB. Change the property for TableA (Source Class section) to TableBForeignKeyA. 3. Save the Domain Model 4. Update the database from the Domain Model
In our sample Domain Model, the diagram will become:
public
partial
class
TableA
{
int
? GetForeignKeyA()
if
(ForeignKeyA !=
null
)
return
ForeignKeyA.PrimaryKeyA;
(TableB !=
TableB.PrimaryKeyB;
;
}
void
SetForeignKeyA(WorkaroundEntitiesModel model,
? foreignKey)
ForeignKeyA = model.TableAs.FirstOrDefault(ta => ta.PrimaryKeyA == foreignKey.Value);
(ForeignKeyA ==
ForeignKeyA =
TableA { PrimaryKeyA = foreignKey.Value };
TableB = model.TableBs.FirstOrDefault(tb => tb.PrimaryKeyB == foreignKey.Value);
(TableB ==
TableB =
TableB { PrimaryKeyB = foreignKey.Value };
Resources Buy Try