从程序集创建MetadataWorkspace时加载不同的DataSpaces?

时间:2018-07-18 14:55:08

标签: c# entity-framework

编辑:我发现,如果从上下文(var workspace = ((IObjectContextAdapter) context).ObjectContext.MetadataWorkspace;)创建工作区,则其他数据空间实际上包含项目集合,而不是null。所以我想现在的问题是:如何在不实例化DbContext的情况下访问CSpace,CSSpace等?

我正在尝试从我使用一堆程序集创建的MetaDataWorkSpace加载ItemCollections。为了消除可能的错误,我制作了一个包含模型的简单类库,进行了构建,进行了代码优先的迁移,现在我正在加载在过程中创建的.dll。

可以在以下位置找到模型的代码:https://pastebin.com/sfziddix(虽然不重要)

这是DbContext:

namespace SampleMappingAssembly
{
    class AnimalContext:DbContext
    {
        public virtual DbSet<Person> People { get; set; }
        public virtual DbSet<Animal> Animals { get; set; }
        public AnimalContext()
        {

        }
    }
}

我现在正尝试加载程序集,将其转换为MetadataWorkspace并加载与每个DataSpace相关的ItemCollections。为此的代码如下:

namespace WorkSpaceLoader
{
    class Program
    {
        static void Main(string[] args)
        {
            //The directory where the .dll is
            string directory = @"D:\Zwischenablage\LiterallyCodeFirst";
            //Filter because there's more files in this than just the ones I want to load
            var assemblyPaths = System.IO.Directory.EnumerateFiles(directory)
                .Where(s => s.EndsWith(".dll") && s.Contains("Sample"));
            var assemblies = new List<Assembly>();
            foreach(string path in assemblyPaths)
            {
                try
                {
                    //One of these is an assembly which I can't load due to unrelated reasons so Try-Catch
                    assemblies.Add(Assembly.LoadFile(path));
                }
                catch(Exception ex)
                {
                    Console.WriteLine($"Couldn't load {path.Split('\\').Last()}:\n{ex.Message}\n");
                }
            }
            Console.WriteLine($"Successfully loaded {assemblies.Count} assemblies. Creating workSpace...");
            //Not sure about the res:// part, but according to my book it's the pattern to look at every DataSpace
            var workspace = new MetadataWorkspace(new string[] {@"res://*"}, assemblies); //@"res://*/"
            Console.WriteLine("Successfully converted assemblies to MetadataWorkspace...");
            List<ItemCollection> coll = new List<ItemCollection>();
            //Iterating over DataSpaces
            for (int i = 0; i <= 4 ; i++)
            {
                var collection = GetItemsFromDataSpace(workspace, (DataSpace)i);
                if (collection != null)
                    coll.Add(collection);
            }
            Console.WriteLine("\nDone loading ItemCollections");
            Console.ReadKey();
        }

        public static ItemCollection GetItemsFromDataSpace(MetadataWorkspace workspace, DataSpace dataSpace)
        {
            ItemCollection items;
            try
            {
                //Not using TryGetItemCollection because I wanted to see the exception
                items = workspace.GetItemCollection(dataSpace);
                Console.WriteLine($"\nSuccessfully retrieved ItemCollection from {dataSpace.ToString()}");
                return items;
            }
            catch(Exception ex)
            {
                Console.WriteLine($"\nUnable to retrieve Item collection from {dataSpace.ToString()}");
                Console.WriteLine($"\n{ex.ToString()}\n");
                return null;
            }
        }
    }
}

给出以下结果(在控制台中打印“异常”):

https://i.imgur.com/ImG1H2W.png

我必须承认,我对该主题的理解是相当有限的,但现在我确实处于困境。我究竟做错了什么?

0 个答案:

没有答案