Asked by:
Data Model Extensibility User Guide Exception

Question
-
Hi,
I am currently working on the Data Extensibility User Guide walkthrough and currently it throws an exception:System.Data.SqlClient.SqlException was not treated.
HResult=-2146232060
Message=The stored procedure 'Core.GetDataModelModules' not found.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=2812
Procedure=""
Server=.
State=62
StackTrace:
bei System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
bei System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
bei System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
bei System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
bei System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
bei System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
bei System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
bei System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
bei Zentity.Core.DataModel.DataModelLoader.Refresh(DataModel model, SqlConnection storeConnection)
bei Zentity.Core.DataModel.Refresh()
bei Zentity.Core.DataModel.get_Modules()
bei ZentitySamples.Program.Main(String[] args) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\ConsoleApplication4\ConsoleApplication4\Program.cs:Zeile 25.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
InnerException:
Code:
using Zentity.Core;
using System.Xml;
using System.Reflection;
using System;
using System.Collections;
using System.Linq;
namespace ZentitySamples
{
class Program
{
const string extensionsNamespace = "Zentity.Extensions.ScholarlyWorks";
const string connectionStringFormat = @"provider=System.Data.SqlClient;
metadata=res://{0}; provider connection string='Data Source=.;
Initial Catalog=Zentity;Integrated Security=True;MultipleActiveResultSets=True'";
const string extensionsAssemblyName = "Zentity.Extensions.ScholarlyWorks";
static void Main(string[] args)
{
using (ZentityContext context = new ZentityContext(
string.Format(connectionStringFormat, "Zentity.Core")))
{
// Create a new module.
DataModelModule module = new DataModelModule { NameSpace = extensionsNamespace };
context.DataModel.Modules.Add(module);
// Create the resource type.
ResourceType resourceTypeResource = context.DataModel.Modules["Zentity.Core"].ResourceTypes["Resource"];
ResourceType resourceTypeScholarlyWork = new ResourceType { Name = "ScholarlyWork", BaseType = resourceTypeResource };
module.ResourceTypes.Add(resourceTypeScholarlyWork);
// Create some Scalar Properties.
ScalarProperty copyright = new ScalarProperty { Name = "Copyright", DataType = DataTypes.String, MaxLength = 4000 };
resourceTypeScholarlyWork.ScalarProperties.Add(copyright);
// Synchronize to alter the database schema.
context.DataModel.Synchronize();
// Generate Extensions Assembly.
// NOTE: We have embedded the generated Entity Framework artifacts in the assembly itself.
byte[] rawAssembly = context.DataModel.GenerateExtensionsAssembly(
extensionsAssemblyName, true, new string[] { extensionsNamespace },
new string[] { extensionsNamespace }, null);
Assembly extensions = Assembly.Load(rawAssembly);
// Create some repository items using the generated assembly.
CreateRepositoryItems(extensions);
// Retrieve the created repository items.
FetchRepositoryItems(extensions);
}
}
private static void FetchRepositoryItems(Assembly extensionsAssembly)
{
using (ZentityContext context = new ZentityContext(
string.Format(connectionStringFormat, extensionsAssemblyName)))
{
Console.WriteLine("Getting ScholarlyWorks...");
Type resourceTypeScholarlyWork = extensionsAssembly.GetType(extensionsNamespace + ".ScholarlyWork");
PropertyInfo pi = resourceTypeScholarlyWork.GetProperty("Copyright");
MethodInfo ofTypeMethod = context.Resources.GetType().GetMethod("OfType").
MakeGenericMethod(resourceTypeScholarlyWork);
var customTypeInstances = ofTypeMethod.Invoke(context.Resources, null);
foreach (Resource scholarlyWork in (IEnumerable)customTypeInstances)
{
Console.WriteLine("Id:[{0}], Copyright:[{1}]", scholarlyWork.Id,
pi.GetValue(scholarlyWork, null));
}
}
}
private static void CreateRepositoryItems(Assembly extensionsAssembly)
{
using (ZentityContext context = new ZentityContext(
string.Format(connectionStringFormat, extensionsAssemblyName)))
{
Type resourceTypeScholarlyWork = extensionsAssembly.GetType(extensionsNamespace + ".ScholarlyWork");
var aScholarlyWork = Activator.CreateInstance(resourceTypeScholarlyWork);
PropertyInfo pi = resourceTypeScholarlyWork.GetProperty("Copyright");
pi.SetValue(aScholarlyWork, "A copyright value.", null);
// Save the items to repository.
context.AddToResources((Resource)aScholarlyWork);
context.SaveChanges();
}
}
}
}Tuesday, December 18, 2012 3:08 PM
All replies
-
I am working on this and update you once i trace the bug.
-Regards
Thursday, December 20, 2012 6:31 AM -
The exception Message=The stored procedure 'Core.GetDataModelModules' not found. This is a stored procedure, I guess the stored procedure might be deleted. please check the Zentity db and let me know.
- Proposed as answer by Kalnemi Thursday, December 20, 2012 1:13 PM
Thursday, December 20, 2012 1:13 PM -
Hi Kalnemi,
thanks for your reply. I tried to implement the walkthrough of the “Zentity (Version 2.0) Data Model Extensibility User Guide“ (page 35). My first step was to recreate the Zentity Database with the SQL-Code like it is described in the guide (page 35). I think that here is the problem. The complete Zentity Database is reset with this SQL-Code (no stored procedure). But without resetting the database, I get an error while executing the C#-program of page 35-36 (“ScholarlyWorks already exists”).
The next problem I have, is the “Zentity.Core”-import in a C#-program. Which Zentity.Core-file I have to import, to create my own database?
I created the RDF and installed it like you described here: http://social.microsoft.com/Forums/en-US/zentity/thread/b9749d71-a47a-4349-a63c-881649166c50
Now, I want to create a database and load data into it. For that I have to write a C#-program that imports “Zentity.Core” (using Zentity.Core). But where can I find that file?
I hope that you understand my problems.
Best regards
- Edited by mikolajewski Thursday, December 20, 2012 5:20 PM
Thursday, December 20, 2012 5:13 PM -
You can re-create the Zentity database in 2 ways. Ether manually invoking the Database.sql script placed under (Zentity installation folder)\script\ or by calling this through C#. Re-creating Zentity db is given in the Data Model Extensibility user guide, page 35.
Let me know if this is helpful.
-Regards
Friday, December 21, 2012 3:00 AM -
Hi
the SQL-Statements on page 35 remove the stored procedure from the database. After we have invoked these statements (page 35) we got the SQL-Exception because of the missing procedure.
Now we have switched the plan to create the data model to the way it’s done by the USPresidentSample in the “PresidentDataImport” folder. We’ve already created the new model with new xml/xsd files in the “RDF” folder. The Problem with this way is the creation of the relationships.
In the opinion of my teammate, we have to change the .edmx files to specify the relationships for Zentity. But we’ve no idea how to auto creation works to generate the .cs and the “Reference” (DATASVCMAP).
-greetings
Friday, December 21, 2012 1:55 PM -
Hi,
I am sorry, I not very clear with your query. I guess, there is some misconception. Follow the link,
http://social.microsoft.com/Forums/en-US/zentity/thread/fad2b14d-9e9c-43c1-9d96-d3df13d2ead3 ,
http://social.microsoft.com/Forums/en-US/zentity/thread/29bcc756-aeef-475c-a2a2-049e76240707
Let me know if this helps. Also, I am putting below some text of a conversation which may help you.
- Edited by Kalnemi Wednesday, December 26, 2012 1:01 PM Link updates
Wednesday, December 26, 2012 12:32 PM -
QUERY
A background of our exploration
Referring to the query posted on 8<sup>th</sup> September 2011
“Query 3 -> Is there any tool available to ease the creation of the Pivot Model, the uploading of data to Zentity database tables and also the publishing of the Pivot Collection? Currently, these steps are done manually and require custom coding (which we modify from the sample demos provided at Zentity website).”
We are attempting to automate the above steps (as instructed in the President Data Import via XML in the Getting Started Guide.docx) via C# code (using APIs from Zentity.Core.dll and Zentity.Rdf.dll).
What has been achieved
- Managed to successfully create a custom zentity data model (ZDM) via .XML and .XSD file by using DataModelModule.CreateFromRdfs() method.
- Save ZDM to Zentity database.
- Read in data from XML files.
- Reflecting on the in-memory assembly to create new resource and populate resource scalar property with data read from XML files.
- Save new resources to Zentity database.
Queries
- How can we create pivot collection via C# code? Similar to calling Create-ZentityPivotCollectionFromResourceType <ModelNameSpace> <ResourceType> in Powershell
- How can we change the Visual Type of a resource type to Associated Image (similar to that in USPresident example) via C# code?
RESPONSE
Certainly, you can automate the Create-ZentityPivotCollectionFromResourceTypein sequence with the automation which you have already achieved so far. Writing a custom code to achieve the automation of the above function can be lengthy. It will also be a case of code duplication if the sole purpose is automation of the common tasks.
What I want to suggest is either
- Write a custom PowerShell script which calls the required tasks or functions in a given sequence, or
- You can build a custom application or a simple GUI and call the PowerShell functions directly from the scripted file in your C# code.
The second option is recommend if you plan to do some other customizations. If the function is already implemented in PowerShell I recommend to directly call that function through C# then writing it from the scratch. And, if you want to do all the customization using C# only then use PowerShell scripts as reference. The script file functions provides direct hints about the API functions you are looking for.
The answers for your queries are given below in the order they come in data model creation.
Query 2: How can we change the Visual Type of a resource type to Associated Image (similar to that in USPresident example) via C# code?
Answer -> PowerShell script file for the function Get-ZentityResourceTypeConfiguration is PublishingConfigService.ps1. It returns an object ($x) of type Zentity.Services.Configuration.Pivot.ResourceTypeSetting object to set its visual type property. You can directly instantiate an object and set the required value.
Query 1: How can we create pivot collection via C# code? Similar to calling Create-ZentityPivotCollectionFromResourceType <ModelNameSpace> <ResourceType> in PowerShell
Answer -> PowerShell script file for the above function is PublishingService.ps1. If you are on the server side you can directly call the Zentity.Services.Web.Pivot.PublishingService.CreateCollection(string modelNamespace, string resourceType). For the client side, the Create-ZentityPivotCollectionFromResourceType first gets the proxy for the Zentity.Services.Web and then calls the create collection method stated above. The query 2 can also be solved this way and vice-versa according to your convenience.
Please note that when you are using these function directly you need to provide your credentials and roles accordingly.
DISCUSSION
I have followed the guide you given
“-> PowerShell script file for the above function is PublishingService.ps1. If you are on the server side you can directly call the Zentity.Services.Web.Pivot.PublishingService.CreateCollection(string modelNamespace, string resourceType). For the client side, the Create-ZentityPivotCollectionFromResourceType first gets the proxy for the Zentity.Services.Web and then calls the create collection method stated above. The query 2 can also be solved this way and vice-versa according to your convenience.”
I have created a Windows Console program that will be executed via server-side. For a start, we expect the executable to perform the following in a single execution:
- Create data model (returns in-memory assembly)
- Do reflection on the in-memory assembly and create resources, populating resources with data read from XML files
- Create pivot collection
This allow us to create models, import data and publish the collection without having to write custom code for each and every model we wish to create. After performing the tasks above, users should be able to view resources of create models via Zentity Pivot Viewer and Zentity Visual Explorer.
Queries
- I have tried calling Zentity.Services.Web.Pivot.PublishingService.CreateCollection(string modelNamespace, string resourceType)via C# code but am faced with error in the Zentity.Services.Web.PublishQueueProcessor.ProcessPublishRequest(PublishRequest currentPublishRequest)as logged in the trace logs. Am I missing some steps in between?
- In order to perform data model creation, data import programmatically without declaratively adding the generated assemblies, it seems to me (as far as I know) that doing reflection on the in-memory assembly is the only way to go about achieving the goal. Is there an alternative way to achieving this goal i.e. not doing reflection and not adding assembly declaratively? (True)
- Edited by Kalnemi Wednesday, December 26, 2012 12:45 PM
Wednesday, December 26, 2012 12:44 PM