简体   繁体   English

实体框架7 DbContext脚手架

[英]Entity Framework 7 DbContext scaffold

I am trying to generate a DbContext for an existing database structure using ASP.NET 5 and Entity Framework 7. Not surprisingly, there isn't a lot of documentation surrounding how to do this easily. 我正在尝试使用ASP.NET 5和Entity Framework 7为现有数据库结构生成DbContext 。毫不奇怪,没有很多关于如何轻松完成此操作的文档。 Additionally, I want to scaffold ONLY the context; 另外,我想仅仅支持上下文; there are ~900 tables and I only care about a few of them, I don't need a model class for each one. 有大约900个表,我只关心它们中的一些,我不需要每个表的模型类。

I've been using the commands specified here and here with little luck. 我一直在使用指定的命令在这里 ,并在这里与点点运气。

So, I guess I have two questions: 所以,我想我有两个问题:

  1. Where are the generated context files located? 生成的上下文文件位于何处? I run the command in the command prompt with no failure, but nothing else happens. 我在命令提示符下运行命令没有失败,但没有其他事情发生。 I know I'm at least in the right place as I can add the old EF6 model with unsupported properties and it gives me an error that they are not supported. 我知道我至少在正确的位置,因为我可以添加旧的EF6模型具有不受支持的属性,它给我一个错误,他们不支持。

  2. Is it possible to generate just a context with no corresponding model classes? 是否可以仅生成没有相应模型类的上下文?

I had the same problem with my project generating the context and the models. 我的项目生成上下文和模型时遇到了同样的问题。 Here's a few things that I did. 这是我做过的一些事情。

Updates For 1.0 RC1 below 更新对于下面的1.0 RC1

Project.json Project.json

  "dependencies": {
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final"
  },

  "commands": {
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }

DNX Command DNX命令

dnx ef dbcontext scaffold "connectionString" EntityFramework.MicrosoftSqlServer

Original Post Below 原帖在下面

Make sure these are added to your project.json file: 确保将这些文件添加到project.json文件中:

"dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta7",
    "EntityFramework.Commands": "7.0.0-beta7",
    "EntityFramework.SqlServer.Design": "7.0.0-beta7"
},
"commands": {
    "ef": "EntityFramework.Commands"
}

Upgrade dnvm and the dnx runtimes as well using dnvm update-self and dnvm upgrade . 使用dnvm update-selfdnvm升级升级dnvm和dnx运行时。 I ran this in cmd. 我在cmd中运行它。

Opened cmd.exe in the project location (if you are in windows, navigate to the folder and shift + right-click in the folder and click "Open command window here"). 在项目位置打开cmd.exe(如果您在Windows中,导航到该文件夹​​并在文件夹中右键单击并单击“在此打开命令窗口”)。 In my case I had a separate project for my Data Access Layer eg. 就我而言,我的数据访问层有一个单独的项目,例如。

C:\Projects\Stackoverflow Example\src\StackoverflowExample.DAL\

I then simplay ran: 我然后simplay跑了:

dnx ef dbcontext scaffold "Data Source=.;Initial Catalog=database;Integrated Security=True" EntityFramework.SqlServer dnx ef dbcontext scaffold“Data Source = .; Initial Catalog = database; Integrated Security = True”EntityFramework.SqlServer

Make sure your project can build. 确保您的项目可以构建。 If there are errors, the commands probably wont work. 如果有错误,命令可能不起作用。

It generated all the models as well as the context (with the OnModelCreating() setup of each entity). 它生成了所有模型以及上下文(使用每个实体的OnModelCreating()设置)。 If you don't need all the models, just delete the ones you are not using. 如果您不需要所有模型,只需删除不使用的模型。

So to answer you questions: 所以回答你的问题:

  1. It creates the models and context in the folder where you ran the dnx ef dbcontext scaffold in. 它在您运行dnx ef dbcontext scaffold的文件夹中创建模型和上下文。
  2. I cant see any commands that allows you to do this yet. 我看不到任何允许你这样做的命令。 Run dnx ef --help in cmd and look for yourself. 在cmd中运行dnx ef --help并自己查找。 dnx ef dnx ef

I hope this helps. 我希望这有帮助。

scaffold命令存在一个选项--table(-t):

dnx ef dbcontext scaffold connectionstring provider -t dbo.tab1 -t dbo.tab2

For EF 7, you no longer need to initialise with base, it acually results in a compiler error. 对于EF 7,您不再需要使用base初始化,它会导致编译器错误。 what you need to look as it the Microsoft EF7 starting docs. 您需要查看Microsoft EF7启动文档。

You want to pay attention to the new 你想要关注新的

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Visual Studio 2015 | Use the LocalDb 12 instance created by Visual Studio
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");

        // Visual Studio 2013 | Use the LocalDb 11 instance created by Visual Studio
        // optionsBuilder.UseSqlServer(@"Server=(localdb)\v11.0;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");

        // Visual Studio 2012 | Use the SQL Express instance created by Visual Studio
        // optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
    }

I'd try code-first from an existing database. 我会从现有数据库中尝试代码优先。 You can create your context class to look something like this: 您可以创建上下文类,看起来像这样:

public class MyDbContext : DbContext {

        public MyDbContext()
            : base("connectionString") {
            Database.SetInitializer<MyDbContext>(null);
        }

        public virtual DbSet<Table1> Table1s { get; set; }

        public virtual DbSet<Table2> Table2s { get; set; }

        ...
}

You can include DbSets for only those tables you need. 您可以仅为所需的表包含DbSet。 You create the corresponding models you need (Table1, Table2, etc.) and scaffold controllers and views from those models. 您可以根据这些模型创建所需的相应模型(表1,表2等)和脚手架控制器和视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行 Scaffold DBContext 而不覆盖实体框架核心中的自定义代码 - Run Scaffold DBContext without overwriting custom code in Entity Framework core Entity Framework Scaffold-DbContext 用户登录失败 - Entity Framework Scaffold-DbContext Login failed for user 如何在 Entity Framework Core 中使用复数 DbSet 属性名称搭建 DbContext? - How to scaffold DbContext with plural DbSet property names in Entity Framework Core? Entity Framework 5 脚手架 - EntityTypeConfiguration - Entity Framework 5 Scaffold - EntityTypeConfiguration 数据库优先使用 Entity Framework Core、oracle 11g (v11.2) 和 Scaffold-DbContext - Database first with Entity Framework Core ,oracle 11g (v11.2 ) and Scaffold-DbContext 在 Entity Framework Core 中调用 Scaffold-DbContext 时出现“异常调用 'GetFullPath'...”错误 - “Exception calling 'GetFullPath'…” error when calling Scaffold-DbContext in Entity Framework Core 在不使用Scaffold DbContext的情况下将表添加到实体 - Adding A table to the entity without using Scaffold DbContext Scaffold-DbContext的替代方案,用于生成实体类型 - Alternatives to Scaffold-DbContext for generating entity types 实体框架-DBContext平等 - Entity Framework - DBContext Equality 配置实体框架/ DbContext - Configuring Entity framework/DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM