简体   繁体   English

实体框架,ASP.NET,上下文缺失

[英]Entity Framework, ASP.NET, Missing context

I'm new here (and still rookie coding), so sorry, if I asking wrong or stupid... I'm trying to go through with Troelsen's book Pro C# 7 with .NET and .NET Core, but I stuck at the begining of chapter 29. The main goal is to create an ASP.NET MVC (named CarLotMVC) solution. 我是这里的新手(并且仍然是菜鸟编码),很抱歉,如果我问错还是愚蠢...我正在尝试阅读Troelsen的书Pro C#7,带有.NET和.NET Core,但我坚持第29章开始。主要目标是创建一个ASP.NET MVC(名为CarLotMVC)解决方案。 I'm created the ASP project, as the book said, and then I try to insert two other projects (AutoLotDAL and AutoLotDAL.Models). 正如书中所述,我已经创建了ASP项目,然后尝试插入其他两个项目(AutoLotDAL和AutoLotDAL.Models)。 I'm doing exactly like it is written in the book, but when I try to add a new Controller to the ASP project, I can't see the context, and only seeing the ASP projects model classes. 我的工作和书中写的完全一样,但是当我尝试向ASP项目中添加新的Controller时,看不到上下文,只能看到ASP项目模型类。 I downloaded the source code, added the AutoLotDAL and AutoLotDAL.Models projects to the solution, modified the connection strings, the namespaces, but still can't see the context, or the model classes. 我下载了源代码,将AutoLotDAL和AutoLotDAL.Models项目添加到解决方案中,修改了连接字符串,名称空间,但仍然看不到上下文或模型类。

Scaffolds menu - missing models 脚手架菜单-缺少模型

What could be wrong? 有什么事吗 In the AutoLotDAL project there are the migration classes, I tried to delete the database and create again, but that did'n help. 在AutoLotDAL项目中,有迁移类,我试图删除数据库并再次创建,但这没有帮助。 Try to run Visual Studio with administrator, clean the solution and build again. 尝试以管理员身份运行Visual Studio,清理解决方案,然后重新构建。 I saw only once the context, there were multiple errors in the solution, the VS created the Controller class without my help, but when I reopened later the solution all was gone. 我只看到一次上下文,解决方案中有多个错误,VS在没有我帮助的情况下创建了Controller类,但是当我稍后重新打开解决方案时,一切都消失了。

this is the context: 这是上下文:

public partial class AutoLotEntities : DbContext
{
    public AutoLotEntities()
        : base("name=AutoLot")
    {
    }

    protected override void Dispose(bool disposing)
    {
    }

    private void OnSavingChanges(object sender, EventArgs eventArgs)
    {
        var context = sender as ObjectContext;
        if (context == null) return;
        foreach (ObjectStateEntry item in
            context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added))
        {
            if ((item.Entity as Inventory) != null)
            {
                var entity = (Inventory)item.Entity;
                if (entity.Color == "Red")
                {
                    item.RejectPropertyChanges(nameof(entity.Color));
                }
            }
        }

    }

    private void OnObjectMaterialized(object sender, ObjectMaterializedEventArgs e)
    {
    }

    public virtual DbSet<CreditRisk> CreditRisks { get; set; }
    public virtual DbSet<Customer> Customers { get; set; }
    public virtual DbSet<Inventory> Cars { get; set; }
    public virtual DbSet<Order> Orders { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Inventory>()
            .HasMany(e => e.Orders)
            .WithRequired(e => e.Car)
            .WillCascadeOnDelete(false);
    }
}

This is the connectionString: 这是connectionString:

<connectionStrings> <add name="AutoLot" connectionString="data source=(local)\SQLEXPRESS;initial catalog=AutoLot;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> </connectionStrings>    

I'm using VS Community 2017, .NET Framework 4.6.1, EF 6.2.0, updated all NuGet packages. 我正在使用VS Community 2017,.NET Framework 4.6.1,EF 6.2.0,更新了所有NuGet软件包。 But still can't reproduce the solution in the book... Please help. 但是仍然无法复制本书中的解决方案...请帮助。

The references list: References branch 引用列表: 引用分支

I found a way around this by copying a section in the CarLotMVC.csproj file from the book's solution into my project. 通过将CarLotMVC.csproj文件中的一部分从本书的解决方案复制到我的项目中,我找到了解决此问题的方法。

Start by navigating to the ..\\pro-csharp-7-master\\Chapter_29\\CarLotMVC\\CarLotMVC directory in the book's solution code and opening the CarLotMVC.csproj file in a text editor. 首先,导航到本书解决方案代码中的.. \\ pro-csharp-7-master \\ Chapter_29 \\ CarLotMVC \\ CarLotMVC目录,然后在文本编辑器中打开CarLotMVC.csproj文件。

Locate the following element and copy it to the clipboard: 找到以下元素并将其复制到剪贴板:

  <ItemGroup>
    <ProjectReference Include="..\AutoLotDAL.Models\AutoLotDAL.Models.csproj">
      <Project>{9a917d7c-885e-4d9a-818f-9955871a4fbb}</Project>
      <Name>AutoLotDAL.Models</Name>
    </ProjectReference>
    <ProjectReference Include="..\AutoLotDAL\AutoLotDAL.csproj">
      <Project>{7e5a8812-c303-4ae0-a6e7-4ac96ec11624}</Project>
      <Name>AutoLotDAL</Name>
    </ProjectReference>
  </ItemGroup>

Confirm the project guid's match the guid's in your AutoLotDAL and AutoLotDAL.Models projects by opening the AutoLotDAL.csproj in the AutoLotDAL project and the AutoLotDAL.Models.csproj in the AutoLotDAL.csproj and comparing the ProjectGuid tags with the values specified in the element copied from the book's solution. 通过打开AutoLotDAL项目中的AutoLotDAL.csproj和AutoLotDAL.csproj中的AutoLotDAL.Models.csproj,并将ProjectGuid标记与在复制的元素中指定的值进行比较,确认项目guid与您的AutoLotDAL和AutoLotDAL.Models项目中的guid相匹配。从本书的解决方案中

The following is line #7 from my AutoLotDAL.csproj: 以下是我的AutoLotDAL.csproj的第7行:

<ProjectGuid>{7E5A8812-C303-4AE0-A6E7-4AC96EC11624}</ProjectGuid>

Go to your project's directory with the same file and open it in a text editor. 使用相同的文件转到项目的目录,然后在文本编辑器中将其打开。 Locate the following block (line #185 in my file): 找到以下块(文件中的第185行):

  <ItemGroup>
    <Folder Include="App_Data\" />
    <Folder Include="Models\" />
  </ItemGroup>

and paste the element from the solution's file after it. 并将其粘贴到解决方案的文件中。

Now, locate the following elements in the same file (line #49 in my file): 现在,在同一文件中找到以下元素(我文件中的第49行):

    <Reference Include="AutoLotDAL">
      <HintPath>..\AutoLotDAL\bin\Debug\AutoLotDAL.dll</HintPath>
    </Reference>
    <Reference Include="AutoLotDAL.Models">
      <HintPath>..\AutoLotDAL.Models\bin\Debug\AutoLotDAL.Models.dll</HintPath>
    </Reference>

and comment out or delete the lines. 并注释掉或删除这些行。

Save the file and try adding the controller again. 保存文件,然后尝试再次添加控制器。 The project worked for me with these changes. 这些更改为我的项目工作。

In addition do adding the AutoLotDAL and the AutoLotDAL.Models Project to the solution, you have to add a reference to those projects in the CarLotMVC Project. 除了将AutoLotDAL和AutoLotDAL.Models项目添加到解决方案之外,您还必须在CarLotMVC项目中添加对这些项目的引用。 You can find a documentation on that topic here . 您可以在此处找到有关该主题的文档。

After doing this you have to recompile your project. 完成此操作后,您必须重新编译您的项目。 If it compiles without any errors, you should see the Context and Model classes in the scaffolding dialog. 如果编译时没有任何错误,则应该在脚手架对话框中看到Context和Model类。

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

相关问题 ASP.NET MVC5实体框架应用程序中的单个上下文 - Single Context in ASP.NET MVC5 Entity Framework application ASP.NET核心实体框架-dbModel上下文中的InvalidOperationException - ASP.NET Core Entity Framework- InvalidOperationException in dbModel context 在 LINQ Entity Framework asp.net MVC 5 中切换数据库上下文 - Switching DB Context in LINQ Entity Framework asp.net MVC 5 ASP.NET控制器+实体框架上下文=线程安全吗? - ASP.NET Controllers + Entity Framework Context = thread safe? ASP.net 中的实体框架 - Entity Framework in ASP.net 多层ASP.NET MVC应用程序中的实体框架上下文 - Entity Framework Context in a multi-tier ASP.NET MVC Application 实体框架 ASP.NET MVC 5 中事务中的存储过程和数据库上下文操作 - Stored Procedure and Database Context Operation in a Transaction in Entity Framework ASP.NET MVC 5 ASP.NET Core 2.2实体框架:在不更改上下文的情况下获取数据子集 - ASP.NET Core 2.2 Entity Framework: Get subset of data without altering context 在带有Entity Framework的ASP.NET MVC中,此上下文仅支持基本类型或枚举类型 - Only primitive types or enumeration types are supported in this context in ASP.NET MVC with Entity Framework asp.net Entity Framework 6.0-我应该使用什么上下文生成器? - asp.net Entity Framework 6.0 - What Context Generator should I be using?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM