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

Runtime artificial field creation via XML

2 Answers 79 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Toshiyuki Tega
Top achievements
Rank 1
Toshiyuki Tega asked on 10 Oct 2009, 04:59 PM
Hi, I'm trying to add an artificial field to a persistent class at runtime.
Since the persistent class resides in a class library, I'm using a XML approach to create a Database instance like this:
                String assumedInitialConfiguration = 
                    "<openaccess>" + 
                        "<references>" + 
                            "<reference assemblyname='PLACEHOLDER' configrequired='True'/>" + 
                        "</references>" + 
                        "{0}" + 
                    "</openaccess>"
 
                StringBuilder connection = new StringBuilder(); 
                connection.Append("<artificial>"); 
                connection.Append("<mapping id=\"artificialMapping\">"); 
                connection.Append("<namespace name=\"Softgate.Stratus.Data\">"); 
                connection.Append("<class name=\"BarData\">"); 
                connection.Append("<field name=\"age\" clr=\"System.String\"/>"); 
                connection.Append("</class>"); 
                connection.Append("</namespace>"); 
                connection.Append("</mapping>"); 
                connection.Append("</artificial>"); 
 
                System.Reflection.Assembly dll = theObjectScopeProvider.GetType().Assembly; 
                assumedInitialConfiguration = assumedInitialConfiguration.Replace("PLACEHOLDER", dll.GetName().Name); 
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); 
                xmlDoc.LoadXml(String.Format(assumedInitialConfiguration, connection.ToString())); 
                Database db = Telerik.OpenAccess.Database.Get("StratusConnection", xmlDoc.DocumentElement, new System.Reflection.Assembly[] { dll }); 
 
                string ddlscript = db.GetSchemaHandler().CreateUpdateDDLScript(null); 
                if (!string.IsNullOrEmpty(ddlscript)) 
                    db.GetSchemaHandler().ExecuteDDLScript(ddlscript); 
 

Although I'm expecting that a field "age" of type String is added to Softgate.Stratus.Data.BarData class, it doesn't work like that way. I noticed that CreateUpdateDDLScript method is simply returning a null.

For your information, the above code fragment is basically working fine except the artificial field creation. It seems that those connection.Append lines, that are meant to deal with the artificial field, don't have effect at all.

Any comment and/or example would be very appreciated. Thanks in advance.


2 Answers, 1 is accepted

Sort by
0
Accepted
Semo
Telerik team
answered on 12 Oct 2009, 03:57 PM
Hello Toshiyuki Tega,

You can check whether you have the <extension key="artificial-types" value="" /> tag placed within the <mapping> tag before the opening <artificial>.

Have a look at the following xml fragment:

   <mappings current="mssqlMapping">
    <mapping id="mssqlMapping">
      <extension key="artificial-types" value="" />
      <namespace name="OpenAccessClassLibrary">
        <class name="Users">
          <extension key="db-table-name" value="Users" />
          <field name="_userID" null-value="exception">
            <extension key="db-column">
              <extension key="db-column-name" value="UserID" />
            </extension>
          </field>
          <field name="_name">
            <extension key="db-column">
              <extension key="db-column-name" value="Name" />
              <extension key="db-length" value="50" />
            </extension>
          </field>
          <field name="_family">
            <extension key="db-column">
              <extension key="db-column-name" value="Family" />
              <extension key="db-length" value="50" />
            </extension>
          </field>
        </class>
      </namespace>
    </mapping>
  </mappings>
  <artificial>
    <mapping id="artificialMapping">
      <namespace name="OpenAccessClassLibrary">
        <class name="Users">
          <field name="age" clr="System.Int32" />
        </class>
      </namespace>
    </mapping>
  </artificial>

If this does not help you, please open a support ticket and send us your App.config file.

Sincerely yours,
Semo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Toshiyuki Tega
Top achievements
Rank 1
answered on 13 Oct 2009, 12:25 AM
Semo,

After changing the dynamically created XML as you have pointed out, CreateUpdateDDLScript() worked and the schema got updated.

String assumedInitialConfiguration = 
  "<openaccess xmlns='http://www.telerik.com/OpenAccess'>" + 
    "<references>" + 
      "<reference assemblyname='PLACEHOLDER' configrequired='True'/>" + 
    "</references>" + 
    "<mappings current='mssqlMapping'>" + 
      "<mapping id='mssqlMapping'>" + 
        "<extension key=\"artificial-types\" value=\"\" />" + 
      "</mapping>" + 
    "</mappings>" + 
    "{0}" + 
  "</openaccess>"
 

Thanks for your prompt support.


Tags
Databases and Data Types
Asked by
Toshiyuki Tega
Top achievements
Rank 1
Answers by
Semo
Telerik team
Toshiyuki Tega
Top achievements
Rank 1
Share this question
or