How to: Merge RLINQ MetadataContainers
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This topic demonstrates how to merge multiple RLINQ metadata containers runtime and produce a new MetadataContainer instance that contains the combined metadata.
Suppose you have a project containing two or more RLINQ files. The domain models use the same database. However the different domain models include different tables from the database. The end goal is to create a new MetadataContainer instance that contains the metadata from all RLINQ files and use it runtime to perform CRUD operations.
Loading MetadataContainers by Using the XmlMetadataSource Class
The XmlMetadataSource class allows you to create (load) a MetadataContainer from a XML based source. Or in other words, you will use the XmlMetadataSource class to load a MetadataContainer from an existing RLINQ. So, the first step is to load all metadata containers you want to merge.
MetadataContainer model1 = XmlMetadataSource.FromAssemblyResource( "EntityDiagrams1.rlinq" ).GetModel();
model1.Name = null;
MetadataContainer model2 = XmlMetadataSource.FromAssemblyResource( "EntityDiagrams2.rlinq" ).GetModel();
model2.Name = null;
Dim model1 As MetadataContainer = XmlMetadataSource.FromAssemblyResource("EntityDiagrams1.rlinq").GetModel()
model1.Name = Nothing
Dim model2 As MetadataContainer = XmlMetadataSource.FromAssemblyResource("EntityDiagrams2.rlinq").GetModel()
model2.Name = Nothing
Merging MetadataContainers by Using the AggregateMetadataSource Class
With the containers in hand, the next step is to use the AggregateMetadataSource class. The AggregateMetadataSource class can be used to combine MetadataContainer instances and produce a new MetadataContainer that contains the combined metadata.
AggregateMetadataSource newSource = new AggregateMetadataSource( model1, model2 );
Dim newSource As New AggregateMetadataSource(model1, model2)
Controlling Reference Resolution
The AggregateMetadataSource class uses a special algorithm internally to resolve references when merging objects. Suppose you have a class A and a class B, which classes you want to merge. Class A has a reference to class C. In this case, when you merge class A and class B, the AggregateMetadataSource class cannot resolve the reference from A to C out of the box. Reference resolution can be controlled by specifying AggregationOptions in the AggregateMetadataSource. For more information, check out the How to: Control Reference Resolution topic.
Passing the Metadata to the OpenAccessContext
The final step is to initialize the OpenAccessContext with the new metadata.
EntitiesModel1 context1 = new EntitiesModel1( "SofiaCarRental21Connection", newSource );
Car cars = context1.GetAll<Car>().FirstOrDefault();
RentalOrder rentalOrder = context1.GetAll<RentalOrder>().FirstOrDefault();
Dim context1 As New EntitiesModel1("SofiaCarRental21Connection", newSource)
Dim cars As Car = context1.GetAll(Of Car)().FirstOrDefault()
Dim _rentalOrder As RentalOrder = context1.GetAll(Of RentalOrder)().FirstOrDefault()
The first parameter in the constructor is the connection string name from the configuration file.