How do I modify an existing OpenAccess MappingConfiguration?

Posted by Community Admin on 04-Aug-2018 18:42

How do I modify an existing OpenAccess MappingConfiguration?

All Replies

Posted by Community Admin on 22-Dec-2011 00:00

I created a module that uses OpenAccess to create a couple of tables.  At the time I wrote the module, I wasn't able to get openaccess to create the tables automatically. They were fairly simple so I created the tables and added the columns manually from within SQL Server Management Studio. Now I need to modify the table.  So I add the additonal column to my MappingConfiguration for the table like so:

myTableMapping.HasProperty(v => v.LastModifiedDate).HasColumnType("datetime");

When I try to retrieve items from the table I get the following exception:

 
Server Error in '/NLGSitefinityWebApp' Application.
--------------------------------------------------------------------------------
 
Invalid column name 'last_modified_date'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: Telerik.OpenAccess.RT.sql.SQLException: Invalid column name 'last_modified_date'.
 
Source Error:
 
 
Line 23:            VideoOnDemandGrid.ItemDataBound += new GridItemEventHandler(OnVideoOnDemandGrid_ItemDataBound);
Line 24:            VideoOnDemandGrid.DataSource = context.VideosOnDemand;
Line 25:            VideoOnDemandGrid.DataBind();
Line 26:       
Line 27:
  
 
Source File: C:\Program Files (x86)\Telerik\Sitefinity 4.3\Projects\NLG Sitefinity Site\Modules\VideoOnDemand\Admin\VideoOnDemandAdminView.ascx.cs    Line: 25
 
Stack Trace:
 
 
[SQLException: Invalid column name 'last_modified_date'.]
   Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery() +681
   OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery() +566
   OpenAccessRuntime.Relational.fetch.FetchResultImp.Execute() +93
 
[DataStoreException: Error executing query: Telerik.OpenAccess.RT.sql.SQLException: Invalid column name 'last_modified_date'.
   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery()
   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery()
   at OpenAccessRuntime.Relational.fetch.FetchResultImp.Execute()
SQL:
SELECT a.[id] AS COL1, a.[can_be_purchased] AS COL2, a.[can_be_rented] AS COL3, a.[date_posted] AS COL4, a.[description] AS COL5, a.[expiration_hours] AS COL6, a.[last_modified_date] AS COL7, a.[nme] AS COL8, a.[preview_url] AS COL9, a.[published] AS COL10, a.[purchase_price] AS COL11, a.[qualifies_for_ceu] AS COL12, a.[rental_price] AS COL13, a.[running_time_seconds] AS COL14, a.[secure_url] AS COL15, a.[segment_count] AS COL16, a.[segment_number] AS COL17, a.[sku] AS COL18, a.[test_master_content_xml] AS COL19, a.[thumbnail] AS COL20, a.[title] AS COL21 FROM [sf_nlg_video_on_demand] a ORDER BY a.[id]  Telerik.OpenAccess.RT.sql.SQLException: Invalid column name 'last_modified_date'.
   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery()
   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery()
   at OpenAccessRuntime.Relational.fetch.FetchResultImp.Execute()]
   Telerik.OpenAccess.RT.ExceptionWrapper.Throw() +13
   OpenAccessRuntime.storagemanager.logging.LoggingStorageManager.fetchNextQueryResult(ApplicationContext context, RunningQuery runningQuery, Int32 skipAmount) +692
   OpenAccessRuntime.DataObjects.ForwardQueryResult.Initialize(Int32 indexParam) +273
   OpenAccessRuntime.DataObjects.ForwardQueryResult.get_Item(Int32 indexParam) +92
   Telerik.OpenAccess.RT.ListEnumerator.setCurrent(Int32 _pos) +58
   Telerik.OpenAccess.RT.ListEnumerator.Move(Int32 relative) +97
   Telerik.Web.UI.GridResolveEnumerable.GetCollectionItemType(Boolean noItemsInEnumerator, Type& collectionItemType, Object& collectionFirstObject) +595
   Telerik.Web.UI.GridResolveEnumerable.ParseProperties() +200
   Telerik.Web.UI.GridResolveEnumerable.Initialize() +16
   Telerik.Web.UI.GridResolveEnumerable.EnsureInitialized() +29
   Telerik.Web.UI.GridEnumerableFromDataView..ctor(GridTableView owner, IEnumerable enumerable, Boolean CaseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +265
   Telerik.Web.UI.GridDataSourceHelper.CreateGridEnumerable(GridTableView owner, IEnumerable enumerable, Boolean caseSensitive, Boolean autoGenerateColumns, GridColumnCollection presentColumns, String[] additionalField, Boolean retrieveAllFields, Boolean enableSplitHeaderText) +225
   Telerik.Web.UI.GridTableView.get_ResolvedDataSource() +411
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +46
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +94
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +182
   Telerik.Web.UI.GridTableView.PerformSelect() +206
   Telerik.Web.UI.GridTableView.DataBind() +432
   SitefinityWebApp.Modules.VideoOnDemand.Admin.VideoOnDemandAdminView.Page_Load(Object sender, EventArgs e) in C:\Program Files (x86)\Telerik\Sitefinity 4.3\Projects\NLG Sitefinity Site\Modules\VideoOnDemand\Admin\VideoOnDemandAdminView.ascx.cs:25
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064
 
  
 
 
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.237


I've had a look at the docs and couldn't find anything regarding modifying existing mappings.  However, in watching the "Getting Started with Fluent Mapping for OpenAccess ORM" video series, the Adjusting Mapping Configuration for the Fluent Mapping API video shows some code that appears to be responsible for updating the database schema.  Specifically:

var schemaHandler = VideoOnDemandContext.GetSchemaHandler();
string script = null;
 
if (schemaHandler.DatabaseExists())
    script = schemaHandler.CreateUpdateDDLScript(null);
 
else
    schemaHandler.CreateDatabase();
    script = schemaHandler.CreateDDLScript();
 
if (!string.IsNullOrEmpty(script))
    schemaHandler.ExecuteDDLScript(script);

Is this the part that I'm missing?  I tried running this code after creating the table mapping and getting a VideoOnDemandContext but I get an InvalidOperationException: There are open object scopes.

Any suggestions would be greatly appreciated!

Thanks--Steve

Posted by Community Admin on 27-Dec-2011 00:00

Hi Steve,

I'm not sure how you defined your mappings but generally if you have a property of type DateTime in your model class defining its mapping is defined like this:

myMapping.HasProperty(p => p.LastModified).IsNullable();

(no need to specify HasColumnType("datetime")). Creating the table by hand and them adding the mapping is not recommended as OpenAccess will try to name the columns in its own way and there might occur problems.

Greetings,
Lubomir Velkov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 27-Dec-2011 00:00

Hi Lubomir,

The docs aren't very specific on when HasColumnType should be specified and the examples aren't all that consistant in its use so I figured it couldn't hurt. 

<EDIT>: I just realized that my original post was a bit unclear.  The issue isn't with the mapping of the datatype--it doesn't matter what datatype I'm trying to map.  It's that the mapping changes don't result in the table schema being updated so the column added to the OpenAccess mapping doesn't result in the column being added to the table. </EDIT>

Of course you're right, creating the tables by hand isn't the recommended way.  But as I indicated in my original post, I'm unable to edit the mapping once it's created.  So, if I create a mapping with say 6 columns and later need to a column, I'm unable to do so without the error listed in my original post.  The only workaround I could find was to create the column manually.  OpenAccess' column naming convention is pretty easy to determine; caps are prepended with an underscore replaced by lowercase.  So mySpecalColumnName becomes my_special_column_name.

Anyway, back to the original question: How do I modify an existing mapping so changes to the mapping automatically modify the table schema?

Thanks--Steve

Posted by Community Admin on 30-Dec-2011 00:00

Hi Steve,

Changing the mapping should get reflected immediately to the table when you try to perform any actions on the model that is mapped. I am not sure but it looks like there is some other problem with your database and mapping definitions. It would help us a lot if you could send us the database and the source code so that we could investigate the problem more closely.

All the best,
Lubomir Velkov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed