locked
Adding items by using the Resource Manager (no relations) RRS feed

  • Question

  • Hi,

    I have another problem with Zentity. I want to add an item via Resource Manager to my own datamodel. After pressing the save button, I can't see this item in Resource Manager. Only when I execute the command "Create-ZentityPivotCollectionFromResourceType Test.Model Book" (PublishingService.ps1), I can see it in Resource Manager. Is this the normal procedure to add an item?

    But my main problem is, that there is no relation between the item I added with Resource Manager and the items, which already exist (added via XML and C#). Visual Explorer only displays the item without any connections. After I added an item with the Resource Manager I tried to build the relations by executing the following C#-code:

    foreach (var zenBook in zenContext.Book)
    {
        var isReferenceOf = (from book in zenContext.Book
                            where book.reference == zenBook.Title_Book
                            select book).FirstOrDefault();
        if (isReferenceOf != null)
        {
            zenBook.isReferenceOf_Book.Add(isReferenceOf);
            zenContext.AddLink(zenBook, "isReferenceOf_Book", isReferenceOf);
        }
    }

    zenContext.SaveChanges();

    I get an "UNIQUE KEY"-Error.

    By adding items via XML, this code-snippet works perfect.

    Where is the problem?

    Best regards

    Wednesday, January 9, 2013 11:16 PM

All replies

  • The calling of the function Create-ZentityPivotCollectionFromResourceType is a good practice and ensures that the collections are created and deep zooms images are created. When you do not do so, the Zentity services automatically picks up the unprocessed items and creates the collection. It depends upon the machine load.

    You can relate between two resources in the Resource Manager. See the user guide of the Resource Manager. Your code seems correct. Can you provide me more details. xml schema and the code sample.

    -Regards

    Monday, January 14, 2013 7:38 AM
  • Did you get this issue resolved?

    Monday, January 21, 2013 9:13 AM
  • Sorry for my long absence.

    No, I didn't resolve this issue. I found out, that it crashes by executing this line: zenContext.SaveChanges().

    Now I create the relations via Resource Manager.

    Best regards

    Wednesday, January 23, 2013 4:54 PM
  • np, can you provide me the sample xml rdf files. Also, the code part where you are creating and initializing the context and the model object is required.

    -Regards

    Thursday, January 24, 2013 7:45 AM
  • Thanks for helping me!

    Here are my files:

    PublicationAdmin.xml (Rdf)

    <?xml version="1.0"?>
    <!DOCTYPE rdf:RDF [
      <!ENTITY sch "http://PublicationAdmin.example.org" >
    ]>
    
    <rdf:RDF
       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
       xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
       xmlns:sch="http://PublicationAdmin.example.org"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xml:base="http://PublicationAdmin.example.org"> 
      
      <!-- Class -->
      <rdfs:Class rdf:about="Publication">    
      </rdfs:Class>   
      
      <!-- Attributes Publication-->
      <rdf:Property rdf:about="Title_Publication">
        <rdfs:range rdf:resource="&sch;string"/>
        <rdfs:domain rdf:resource="Publication"/>
      </rdf:Property>  
      
      <rdf:Property rdf:about="references">
        <rdfs:range rdf:resource="&sch;string"/>
        <rdfs:domain rdf:resource="Publication"/>
      </rdf:Property>
      
      <!-- Relationship -->
      <rdf:Property rdf:about="isReferenceOf">
        <rdfs:range rdf:resource="Publication"/>
        <rdfs:domain rdf:resource="Publication"/>
      </rdf:Property>   
      
    </rdf:RDF>  

    DataLoader.cs

    namespace Zentity_PublicationAdmin
    {
        using System;
        using System.Collections.Generic;
        using System.Configuration;
        using System.IO;
        using System.Linq;
        using System.Text;
        using System.Xml.Serialization;
        using ZenDataService;
        using PublicationAdmin = Zentity_PublicationAdmin.ZenDataService.PublicationAdmin.Model;
       
    
    
    
        enum ConfigKeys
        {
            DataServiceUri,        
            PublicationDemoXmlPath,     //Sample        
            PublicationImagePath,       //Publication images
            
        }
    
        class DataLoader
        {
            private Dictionary<string, string> configValues;
            private Uri dataServiceUri;        
            private Publications publicationsDemoData;
    
            internal DataLoader()
            {
                configValues = new Dictionary<string, string>();
                // Load the AppSettings
                LoadConfigValues();
                
                publicationsDemoData = Deserialize<Publicationen>(configValues[ConfigKeys.PublicationDemoXmlPath.ToString()]);
                dataServiceUri = new Uri(configValues[ConfigKeys.DataServiceUri.ToString()]);
            }
            
            private void LoadConfigValues()
            {
                foreach (var configKey in Enum.GetNames(typeof(ConfigKeys)))
                {
                    string configValue = ConfigurationManager.AppSettings[configKey];
                    if (string.IsNullOrWhiteSpace(configValue))
                    {
                        throw new ConfigurationErrorsException(string.Format(Messages.Error_NoConfig, configKey));
                    }
                    configValues.Add(configKey, configValue);
                }
            }
            
            private static T Deserialize<T>(string xmlPath)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (StreamReader xmlStream = new StreamReader(xmlPath))
                {
                    T tempObject = (T)xmlSerializer.Deserialize(xmlStream);
                    return tempObject;
                }
            }
    
            internal void LoadDataToZentity()
            {
                ZenDataService.ZentityContext zenContext = new ZenDataService.ZentityContext(this.dataServiceUri);
                zenContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
                
                string publicationImageFolder = configValues[ConfigKeys.PublicationImagePath.ToString()];
    
    
                /*Adding samples*/          
    
                Console.WriteLine(Messages.AddingMessage + " Publications...");
                foreach (var publicationItem in publicationsDemoData.Items)
                {                
                    var existingPublication = (from pub in zenContext.Publication
                                               where pub.Title_Publication.Equals(publicationItem.Title_Publication, StringComparison.OrdinalIgnoreCase)
                                               select pub).FirstOrDefault();
    
                    if (existingPublication != null)
                    {
                        continue;
                    }
    
                    PublicationAdmin.Publication publication = PublicationAdmin.Publication.CreatePublication(Guid.NewGuid());
    
                    // Setting Scalar Properties
                    publication.Title = publicationItem.Title_Publication;
                    publication.Title_Publication = publicationItem.Title_Publication;                
                    publication.references = publikationItem.references;
    
                    // Adding Publication item to the context, so that it is tracked.
                    zenContext.AddToPublication(publication);                
    
                    if (!string.IsNullOrWhiteSpace(publicationImageFolder))
                    {
                        string[] publicationImagesInFolder = Directory.GetFiles(publicationImageFolder, publication.Title_Publication + ".*");
                        if (publicationImagesInFolder != null && publicationImagesInFolder.Length > 0)
                        {
                            FileInfo publicationImageFile = new FileInfo(publicationImagesInFolder[0]);
                            FlattenedFile publicationFileResource = FlattenedFile.CreateFlattenedFile(Guid.NewGuid());
    
                            publicationFileResource.Title = publicationImageFile.Name;
                            publicationFileResource.Size = publicationImageFile.Length;
                            publicationFileResource.DateAdded = publicationImageFile.CreationTime;
                            publicationFileResource.DateModified = publicationImageFile.LastWriteTime;
                            publicationFileResource.FileExtension = publicationImageFile.Extension;
                            publicationFileResource.MimeType = "image/" + publicationFileResource.FileExtension.Replace(".", string.Empty);
    
                            zenContext.AddRelatedObject(publication, "Files", publicationFileResource);
                            publication.Files.Add(publicationFileResource);
    
                            FileStream fStream = new FileStream(publicationImageFile.FullName, FileMode.Open, FileAccess.Read);
                            zenContext.SetSaveStream(publicationFileResource, fStream, true, publicationFileResource.MimeType, publicationFileResource.Id.ToString());
                        }
                    }
                    Console.WriteLine(Messages.Publication + publication.Title_Publication);
                }
                Console.WriteLine(Messages.AddImagesToPublication);
                zenContext.SaveChanges();
                Console.WriteLine(Messages.AddingCompletedMessage);        
    
                
    
                // Creating relation Publication - Publication -> Reference  
                Console.WriteLine("Start creating relation Publication - Publication (Reference)");
               foreach (var zenPublication in zenContext.Publication)
               {              
                   var isReferenceOfPublication = (from publication in zenContext.Publication
                                                             where zenPublication.references.Contains(publication.Title_Publication) 
                                                             select publication).FirstOrDefault();
                   if (isReferenceOfPublication != null)
                   {
                       zenPublication.isReferenceOf_Publication.Add(isReferenceOfPublication);
                       zenContext.AddLink(zenPublication, "isReferenceOf_Publication", isReferenceOfPublication);
                   }
               }
               Console.WriteLine("Creating relation Publication - Publication (Reference) completed");
    	   zenContext.SaveChanges();           
            }
        }
    }

    PublicationDemoData.xml (sample)

    <?xml version="1.0"?>
    <Publications xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PublicationDemoData.xsd">
    	<Publication>
    		<Title_Publication>Book A</Title_Publication>		
    		<references>Book C</references>
    		<!--Relationship-->		
    		<isReferenceOf></isReferenceOf>		
    	</Publication>
    
    	<Publication>
    		<Title_Publication>Book B</Title_Publication>		
    		<references>Book A, Book C</references>
    		<!--Relationship-->		
    		<isReferenceOf></isReferenceOf>		
    	</Publication>
    	
    	<Publication>
    		<Title_Publication>Book C</Title_Publication>		
    		<references>-</references>
    		<!--Relationship-->		
    		<isReferenceOf></isReferenceOf>		
    	</Publication>
      
    </Publications>

    To clarify my problem, I show you the steps I accomplished:

    1. creating a new model (PublicationAdmin)
    2. adding demo data (PublicationDemoData.xml) into the model via DataLoader.cs
    3. Updating the model

    Up to here, all works perfect.

    Now, I want to add a few more data with Resource Manager. The attribute "references" contains all book-titles the new item refers to. I don't want to create the relation "isReferenceOf" with the Resource Manager, but with executing the following C#-snippet:

    Create relations

    foreach (var zenPublication in zenContext.Publication)
               {              
                   var isReferenceOfPublication = (from publication in zenContext.Publication
                                                             where zenPublication.references.Contains(publication.Title_Publication) 
                                                             select publication).FirstOrDefault();
                   if (isReferenceOfPublication != null)
                   {
                       zenPublication.isReferenceOf_Publication.Add(isReferenceOfPublication);
                       zenContext.AddLink(zenPublication, "isReferenceOf_Publication", isReferenceOfPublication);
                   }
               }
               zenContext.SaveChanges(); 

    The program crashes with an "UNIQUE-KEY" error by executing this: "zenContext.SaveChanges()".

    Note: I translated the content of the files into English. So, maybe there are a few mistakes. But the original code works (expect for the "UNIQUE-KEY" error).

    Best regards


    Thursday, January 24, 2013 10:40 PM
  • wc, thanks for providing me the code. I will work on this by the weekend and let you know on the updates by Monday. Good luck!
    Friday, January 25, 2013 10:03 AM
  • Hi Eric,

    I am still investigating above the above issue. The code is correct and their seems no error if the following checks are done. Publication’s title should be unique, and no publication should refer to its own self, that is, in reference, no publication title should match its own publication title. The issue is related with how entity framework manages the temporary changes and keys before updating the db. Please check this link

    http://stackoverflow.com/questions/7778771/entity-framework-objectcontext-savechanges-fails-on-unique-key-column-update

    I will be looking into this till solved, let me know if you find any solution.

    -Regards


    http://stackoverflow.com/questions/9861043/ef-objectcontext-behavior-on-sql-insert-error
    • Edited by Kalnemi Monday, January 28, 2013 1:30 PM one more link
    Monday, January 28, 2013 1:17 PM