【问题标题】:How to programmatically generate Entity Framework Model DbFirst based on DbContext如何基于 DbContext 以编程方式生成实体框架模型 DbFirst
【发布时间】:2021-09-23 12:44:15
【问题描述】:

我正在寻找一种方法来以编程方式为基于 DbContext 的给定数据库生成实体框架模型 Database First。

现在,我一直在尝试使用 System.Data.Entity.Design.EntityCodeGenerator 类,但似乎只是基于 ObjectContext 生成它们。

http://msdn.microsoft.com/en-us/library/system.data.entity.design.entitycodegenerator(v=vs.110).aspx

在这里我找到了有关如何使用它的信息: http://blogs.msdn.com/b/adonet/archive/2008/06/20/edm-tools-options-part-1-of-4.aspx

但仍然只生成基于 ObjectContext 的模型。

更新:我需要从 Visual Studio 扩展中执行此操作(因此它将 Visual Studio 作为沙箱)。

【问题讨论】:

    标签: .net entity-framework


    【解决方案1】:

    好的,我找到了一种方法,在这里记录它以防其他人遇到相同的问题:而不是使用 System.Data.Entity.Design.EntityCodeGenerator 为模型后端(例如 Model.Designer.cs)生成代码,现在我只是将 .edmx 添加到 Visual Studio 项目中,设置一些属性,Visual Studio 会处理所有事情:

    private void AddToProject( string edmxPath)
        {
          string edmxCodePath;
          ProjectItem pi = _vsProj.Project.ProjectItems.AddFromFile(edmxPath);
          // this little magic replaces having to use System.Data.Entity.Design.EntityCodeGenerator
          pi.Properties.Item("ItemType").Value = "EntityDeploy";
          pi.Properties.Item("CustomTool").Value = "EntityModelCodeGenerator";
          if( efVersion == BaseWizard<BaseWizardForm, BaseCodeGeneratorStrategy>.ENTITY_FRAMEWORK_VERSION_6)
          {
            // For EF6 we use DbContext instead of ObjectContext based context.
            _vsProj.DTE.SuppressUI = true;
            EnvDTE80.Solution2 sol = (EnvDTE80.Solution2)_vsProj.DTE.Solution;
            string itemPath = "";
            if( this.Language == LanguageGenerator.CSharp )
            {
              itemPath = sol.GetProjectItemTemplate("DbCtxCSEF6", "CSharp" );
            } else {
              itemPath = sol.GetProjectItemTemplate("DbCtxVBEF6", "VisualBasic");
            }
            pi.ProjectItems.AddFromTemplate(itemPath, this._modelName);
            // update $edmxInputFile$
            string path = Path.GetDirectoryName(edmxPath);
            string templateName = Path.Combine(path, _modelName + ".tt");
            string contents = File.ReadAllText(templateName);
            File.WriteAllText(templateName, contents.Replace("$edmxInputFile$", _modelName + ".edmx"));
            templateName = Path.Combine(path, _modelName + ".Context.tt");
            contents = File.ReadAllText(templateName);
            File.WriteAllText(templateName, contents.Replace("$edmxInputFile$", _modelName + ".edmx"));
          }
        }
    

    说明:该方法接收生成的.edmx文件的路径,如果EF版本为6,则实例化模板“DbCtxCSEF6”(VS2013始终安装),则只需替换$edmxInputFile$即可带有 edmx 文件名; Visual Studio 将自动添加 .tt 模板并生成后端代码。

    有关上下文的更多详细信息,请参阅 MySql for Visual Studio 1.2.1 或更高版本中 MySql.Data.VisualStudio.Wizards.EntityFrameworkGenerator 类的代码。

    安装程序和源代码(其开源)可在:

    http://dev.mysql.com/downloads/windows/visualstudio/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2017-12-02
      • 2023-03-07
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多