locked
Querying Resources from code fails RRS feed

  • Question

  • Back after a long silence with new puzzles :o)

    I've tried the code below (adapted from the .chm help file)

       static string connStr = @"provider=System.Data.SqlClient;
    metadata=res://Zentity.Core;
    provider connection string='Data Source=.;
    Initial Catalog=Zentity;Integrated Security=True;
    MultipleActiveResultSets=True'";
    
    private static void AddResourceProp()
    {
                ZentityContext context = new ZentityContext(connStr);
                // Create a predicate.    
                Predicate pred = new Predicate
                {
                    Name = "Ansprechpartner",
                    Uri = "urn:sample-uri:pred:Ansprechpartner"
                };
    
                var resColl = context.Resources;
                foreach (var res in resColl)
                {
                    Console.WriteLine(res.Uri);
                }
    }

    Why are only 3 (yes, three) resources returned? They are File objects. Nothing else, despite having over 1000 entities imported from the PubMed sample and others. What am I doing wrong?

    Cheers

    WizMan

    Wednesday, March 21, 2012 2:59 PM

All replies

  • Welcome!

    The resource collection contains the resources from the resource provided to the medata property of the connection string. use metadata=res://*/ to include all resources presents in the calling assembly, else provide the model name to which all resources belongs to. e.g. metadata=res://Zentity.ScholarlyWorks. 

    Happy coding!

    • Proposed as answer by Kalnemi Thursday, March 22, 2012 11:30 AM
    Thursday, March 22, 2012 11:30 AM
  • Hi Kalnemi,

    I have tried changing the metadata to res://Zentity.ScholarlyWorks, however the following line of code (Linq) now has a problem:

     Resource resColl = context.Resources.Where(tuple => tuple.Uri == "urn:zentity-samples:person:LittleJohn").First();

    InvalidOperationException: Object mapping could not be found for Type with identity 'Zentity.ScholarlyWorks.ScholarlyWorkItem'.

    Secondly, when I attempt to use res://*/ the following exception is thrown:

    Schema specified is not valid. Errors:
    error 0194: All artifacts loaded into an ItemCollection must have the same version. Multiple versions were encountered.
    error 0194: All artifacts loaded into an ItemCollection must have the same version. Multiple versions were encountered.
    Zentity.ScholarlyWorks.ExtendedCoreDataModel.csdl(3,4) : error 0019: The EntityContainer name must be unique. An EntityContainer with the name 'ZentityContext' is already defined.
    Zentity.ScholarlyWorks.ExtendedCoreDataModel.csdl(2415,4) : error 0019: Each type name in a schema must be unique. Type name 'Zentity.Core.Predicate' was already defined.
    Zentity.ScholarlyWorks.ExtendedCoreDataModel.csdl(2448,4) : error 0019: Each type name in a schema must be unique. Type name 'Zentity.Core.Property' was already defined.
    Zentity.ScholarlyWorks.ExtendedCoreDataModel.csdl(2486,4) : error 0019: Each type name in a schema must be unique. Type name 'Zentity.Core.Relationship' was already defined.
    Zentity.ScholarlyWorks.ExtendedCoreDataModel.csdl(2529,4) : error 0019: Each type name in a schema must be unique. Type name 'Zentity.Core.RelationshipProperty' was already defined.

    [...]

    Thursday, March 22, 2012 2:20 PM
  • Hi WizMan,

    Currently working on this, able to produce the exception at my end. I will update you with the solution once done.

    -Regards

    Thursday, March 22, 2012 3:39 PM
  • Hi Wiz,

    Do not know how exactly entity framework is working here but the following code worked perfectly for me. You may find some hint at http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/df770006-43c1-4576-951f-1b2e396202a6 .

    using System; using System.Reflection; using Zentity.ScholarlyWorks; using Zentity.Core; namespace ZentitySamples { public class Program { public static void Main(string[] args) { AddResourceProp(); Console.Read(); } static string connStr = @"provider=System.Data.SqlClient;metadata=res://Zentity.ScholarlyWorks;provider connection string='Data Source=Trident\MSSQLSERVER1; Initial Catalog=Zentity;Integrated Security=True; MultipleActiveResultSets=True'"; private static void AddResourceProp() { ZentityContext context = new ZentityContext(connStr); Assembly asm = Assembly.LoadFile(@"E:\Zentity 2.1\Zentity\Code drop\Product\ZentityTest\bin\Debug\Zentity.ScholarlyWorks.dll"); context.MetadataWorkspace.LoadFromAssembly(asm); var resColl = context.Resources.OfType<ScholarlyWork>(); foreach (var res in resColl) { Console.WriteLine(res.Title); } } } }

    There must be some better of doing this(defining metadata sources). Will share here whatever I understand once I get some thoughts of my own.

    -Regards


    • Proposed as answer by Kalnemi Friday, March 23, 2012 5:53 AM
    • Edited by Kalnemi Friday, March 23, 2012 5:55 AM
    Friday, March 23, 2012 5:44 AM