Linked Models
Another solution to the problem of large, complex models is to replace a single model file with multiple linked model files. Linked model files work well when your domain consists of a number of distinct subdomains, with relatively few links across subdomain boundaries. Don’t use linked model files if you have a lot of associations across subdomain boundaries – cross-file associations require quite a bit of maintenance and if you have too many of them you may find they are more trouble than they are worth!
To link a set of model files, you must do two things:
· Set the Name of each model to the same value. (If you are specifying a namespace in the model, then this must be the same for all of the linked models too.)
· Choose one of the models to be the “main” model. For all of the other models, set Is Linked Child to True.
If you use the Paste as Link command to create entity links, or the Refactor > Split Model At Association command to split an existing model into two files, it will set these properties for you.
Code Generation
Each linked model file in a set is code-generated separately. The code generation is done in such a way that the generated files combine to produce a single model using partial classes. For example, you will end up with a single strong-typed unit of work class, with queryable properties for all of the entity types, although the implementation for this class will be spread across three files.
Because LightSpeed depends on partial classes to combine code, all linked model files must be in the same project.
Creating Associations Across Model File Boundaries
You can represent an entity from one file in another file by dragging an Entity on from the Toolbox and setting its Name to the desired linked entity, and settings its Is Link property to True. You can then create associations to the linked entity. You must create the associations in both files.
For example, suppose you have two files, Sales.lsmodel and Logistics.lsmodel. The Sales subdomain contains an entity named PurchaseOrder and the Logistics subdomain contains an entity named Shipment. You want to create a one-to-many association between these entities, so that a PurchaseOrder has a collection of Shipments and a Shipment has a reference to a PurchaseOrder. To do this:
· Open Sales.lsmodel and drag on an Entity from the Toolbox.
· Set the entity’s Name to Shipment and Is Link to True.
· Click OneToManyAssociation in the Toolbox and drag an arrow from PurchaseOrder to Shipment.
· Open Logistics.lsmodel and drag on an Entity from the Toolbox.
· Set the entity’s Name to PurchaseOrder and Is Link to True.
· Click OneToManyAssociation in the Toolbox and drag an arrow from PurchaseOrder to Shipment.
· Save both models and rebuild your project.
Creating and Maintaining Entity Links
A quick way to create a link is to select the “main” entity definition, choose Edit > Copy or press Ctrl+C, switch to the target file, right-click the model background and choose Paste as Link. This will automatically set up the entity link’s properties, and can also link the two model files for you.
Important: An entity link is not aware of the entity it links to. Therefore, you must manually keep properties in sync between the link and the original entity. In particular, if the entity has an Identity Type other than Int32, you must set the Identity Type on the link to be the same as the Identity Type on the entity. If you are using model-first database synchronisation or migrations, and the entity overrides the Table Name, Identity Column Name or Schema, then you must also replicate these settings to the link. You can check that these settings are in sync using the Check Links menu command.
To see the definition of a linked entity, right-click the link and choose Go To Linked Entity.