Runtime artificial field creation via XML

3 posts, 1 answers
  1. Toshiyuki Tega
    Toshiyuki Tega avatar
    13 posts
    Member since:
    Sep 2012

    Posted 10 Oct 2009 Link to this post

    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. Answer
    Semo
    Admin
    Semo avatar
    6 posts

    Posted 12 Oct 2009 Link to this post

    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.
  3. DevCraft banner
  4. Toshiyuki Tega
    Toshiyuki Tega avatar
    13 posts
    Member since:
    Sep 2012

    Posted 12 Oct 2009 Link to this post

    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.


Back to Top