【问题标题】:How to generate CSDL and MSL files programmatically?如何以编程方式生成 CSDL 和 MSL 文件?
【发布时间】:2015-08-15 00:59:15
【问题描述】:

我正在尝试使用 C# 语言生成动态 ORM 模型。因为我的工作任务,我需要这个。

我已经实现了如何在 C# 中以编程方式创建 SSDL 文件。我正在使用EntityStoreSchemaGenerator 类,它以这种方式写入数据:

var store = new EntityStoreSchemaGenerator(providerInvariantName, connectionString, "ShoppingModel");
store.GenerateStoreMetadata();
store.WriteStoreSchema(filePath);

我可以向您展示我拥有的示例 SSDL 文件:http://pastebin.com/3U4A6fY9

没关系,但是获取 CSDL 和 MSL 文件怎么样?

据我了解,需要使用 EntityModelSchemaGenerator 类从 System.Data.Entity.Design 命名空间生成 CSDL 和 MSL 文件。

如果要看https://msdn.microsoft.com/en-us/library/system.data.entity.design.entitymodelschemagenerator_methods(v=vs.110).aspx

有几种方法:

  • WriteModelSchema(String) 将生成的概念架构定义语言 (CSDL) 写入指定文件。
  • WriteModelSchema(XmlWriter) 将生成的概念架构定义语言 (CSDL) 写入 XmlWriter 对象。
  • WriteStorageMapping(String) 将生成的映射规范语言 (MSL) 写入指定文件。
  • WriteStorageMapping(XmlWriter) 将生成的映射规范语言 (MSL) 写入 XmlWriter 对象。

可用于生成所需文件(CSDL && MSL)。 但是我很困惑......如果EntityModelSchemaGenerator构造函数需要System.Data.Metadata.Edm.EntityContainer,如何生成它们。

据我了解,我必须使用EntityConnection 类准备一些实体容器。

但是,如果我的目标是创建 *.csdl 和 *.msl 文件,我该如何构建实体连接,这需要已经创建所有元数据文件( *.csdl、*.ssdl、*.msl )以前?

所以在创建所有元数据文件之前我不能使用EntityModelSchemaGenerator?那么创建 *.csdl 和 *.msl 文件的方法是什么(*.ssdl 文件完成)?

【问题讨论】:

    标签: c# .net entity-framework orm edmx


    【解决方案1】:

    我已经解决了我的问题

    Models = new EntityModelSchemaGenerator(Store.StoreItemCollection, "ShoppingModelConceptual", "ShoppingModelConceptualContainer");

    这基于之前准备的商店物品集合。

    http://pastebin.com/B0gbzuin

    using System;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Data.Entity;
    using System.Data.EntityClient;
    using System.Data.Entity.Design;
    using System.Data.Entity.Infrastructure;
    
    namespace DynamicOrm1
    {
        class Generators
        {
            internal EntityModelSchemaGenerator Models { get; set; }
            internal EntityClassGenerator Classes { get; set; }
            internal EntityCodeGenerator Code { get; set; }
            internal EntityViewGenerator Views { get; set; }
            internal EntityStoreSchemaGenerator Store { get; set; }
    
            const string connectionString = "Server = MSSQL2014; Database = test.shop; Trusted_Connection = True;";
            const string providerInvariantName = "System.Data.SqlClient";
    
            public Generators()
            {
                InitializeSettings();
            }
    
            private void InitializeSettings()
            {
                Classes = new EntityClassGenerator(LanguageOption.GenerateCSharpCode);
                Code = new EntityCodeGenerator(LanguageOption.GenerateCSharpCode);
                Views = new EntityViewGenerator(LanguageOption.GenerateCSharpCode);
                Store = new EntityStoreSchemaGenerator(providerInvariantName, connectionString, "ShoppingModelStore");
    
                Store.GenerateForeignKeyProperties = true;
            }
    
            internal void CreateSsdlFile(string filePath)
            {
                if (filePath == String.Empty)
                    throw new Exception("Can't create the SSDL file, because the given file path is an empty string.");
    
                Store.GenerateStoreMetadata();
                Store.WriteStoreSchema(filePath);
            }
    
            internal void CreateCsdlAndMslFile(string csdlFilePath, string mslFilePath)
            {
                if (Store == null)
                    throw new Exception("Can't create the CSDL and MSL files, because the `Store` object is null.");
    
                Models = new EntityModelSchemaGenerator(Store.StoreItemCollection, "ShoppingModelConceptual", "ShoppingModelConceptualContainer");
                Models.GenerateMetadata();
                Models.WriteModelSchema(csdlFilePath);
                Models.WriteStorageMapping(mslFilePath);
            }
        }
    
        class Program
        {
            private static Generators EntityGenerators { get; set; }
    
            [STAThread]
            static void Main()
            {
                try
                {
                    EntityGenerators = new Generators();
                    EntityGenerators.CreateSsdlFile(@"\shopping.ssdl.xml");
                    EntityGenerators.CreateCsdlAndMslFile(@"\shopping.csdl.xml", @"\shopping.msl.xml");
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }
        }
    }
    

    【讨论】:

    • 如果我使用Code 生成我的cs 文件,我怎样才能通过Code.GenerateCode 的edmx? (第一个参数)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2018-01-29
    • 2014-08-10
    相关资源
    最近更新 更多