This is a migrated thread and some comments may be shown as answers.

Howto Query from FluentMetadataSource

1 Answer 66 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Richard Koslik
Top achievements
Rank 1
Richard Koslik asked on 03 May 2012, 02:34 PM
Hello,

i want to extend my existing Model (rlinq) at runtime with new tables.
For that i have a tabledefinition-file where i can define the needed structure of the existing tables.
For example like following:
# Example of a table definition file used for generic xml-export
TABLESTART(com_generic_head,TestNamespace)
PROPERTY(field1,STRING,KEY)
PROPERTY(field2,DECIMAL)
PROPERTY(field3,DATE)
PROPERTY(field4,INT)
TABLEEND
TABLESTART(com_generic_child1,TestNamespace)
PROPERTY(child_s_key,STRING,KEY)
PROPERTY(child_s_head_key,STRING)
PROPERTY(field1,INT)
PROPERTY(field2,STRING)
TABLEEND
This File is parsed at runtime and should extend my modell so that i can access this tables in the database.
Something like the following...
public class MyFluentMetadataSource : FluentMetadataSource
    {
        private string sTDFile = "";
  
        public MyFluentMetadataSource()
        {
        }
  
        public MyFluentMetadataSource(string sTableDefinitionFile, MetadataContainer existingModel) : base(existingModel)
        {
            sTDFile = sTableDefinitionFile;
        }
  
        protected override IList<MappingConfiguration> PrepareMapping()
        {
            IList<MappingConfiguration> preparedConfigurations = new List<MappingConfiguration>();
  
            if (sTDFile != "")
            {
                using (StreamReader r = new StreamReader(sTDFile))
                {
  
...Parsing File and create mappings...
  
  
                        MappingConfiguration newConfiguration = null;
            newConfiguration = new MappingConfiguration(tableName, nameSpace);
            newConfiguration.HasArtificialStringProperty(fieldName);
....
  
                        preparedConfigurations.Add(newConfiguration);
                }
            }
            return preparedConfigurations;
        }

The Modell is expanded and i can see under the metadata.PersistentTypes my new defined Tables.
But if i want to query it i get an error: No persistent type with the given name found.
The value of the used sHeadTable variable is for example: TestNamespace.com_generic_head
This value is also shown under the metadata.PersistentTypes.
IQueryable<PersistenceCapable> query = fluentContext.Scope.ExtentByName(theDefinitionSet.sHeadTable) as IQueryable<PersistenceCapable>;
            foreach (PersistenceCapable instance in query)
            {
                    string s = instance.FieldValue<string>("field1");
...

If i exchange the name of the table in the fluentContext.Scope.ExtentByName function with a tablename which was allready in the modell at designtime, the function succeeds.

The Table property under the metadata.PersistentType does not show a name...only   MetaTable: "" 4 columns.
The PersistentTypes which are exists since designtime show for example   MetaTable "'COM'.'PDHEAD'" 109 columns
What i am missing to bind the mapping to the real table in the database?
Is there any smal example code how to do?

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 04 May 2012, 03:52 PM
Hello Richard,

This exception would occur if you have already instantiated a context with the metadata from this metadata source. This is because the metadata is cached internally in order not to be recalculated for each context instance, and even if you pass a new metadata container to the context's constructor, the old metadata will still be used.

When you need to refresh the cached metadata, for example to extend the model, there are two possible approaches:
1. Use the newly introduced ReplaceMetadata method of the OpenAccessContextBase class, as described in this documentation article. Please note that this API is only available in the latest internal build of OpenAccess (version 2012.1.427.1)
2. If you do not want to upgrade at this time, you can clear the cached metadata by calling:
Database.Get("YourConnectionString").Dispose();
before instantiating a context with the updated metadata. However, this action is more time-consuming than 1) and that is why I recommend you to switch to the new version and use ReplaceMetadata.
Hope that helps.

All the best,
Alexander
the Telerik team
Follow @OpenAccessORM Twitter channel to get first the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Tags
General Discussions
Asked by
Richard Koslik
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or