【问题标题】:Web.config problems with Entity Framework实体框架的 Web.config 问题
【发布时间】:2015-12-11 20:05:05
【问题描述】:

当我进入web.config 时,我是asp.net 的新手,我无法在其中找到<entityframework> 部分来更改某些设置。我目前正在使用本指南http://www.asp.net/mvc/overview/getting-started/introduction/getting-started 来帮助我完成我的项目。不知道我做错了什么,因为我已经完成了每一步。

<?xml version="1.0" encoding="utf-8"?> <!--   For more information on how to configure your ASP.NET application, please visit   http://go.microsoft.com/fwlink/?LinkId=301880   --> <configuration>   <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->   <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>   <connectionStrings>
    <add name="LoftBook" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-LoftBook-20151211033930.mdf;Initial Catalog=aspnet-LoftBook-20151211033930;Integrated Security=True" providerName="System.Data.SqlClient" />   </connectionStrings>   <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />   </appSettings>   <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5.1" />
      </system.Web>   -->   <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />   </system.web>   <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>   </system.webServer>   <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
    </assemblyBinding>   </runtime> </configuration>
using System; 
using System.Collections.Generic; 
using System.Linq; using System.Web; 
using System.Data.Entity; 
using LoftBook.Models;

namespace LoftBook.DAL {
    public class LBInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<LBContext>
    {

        protected override void Seed(LBContext context)
        {
            var sires = new List<Sire>
            {
                new Sire {RingNo ="IHU S 2009 1143", Colour = "Blue", Breed = "Jassen",},
                new Sire {RingNo ="IHU S 2011 1467", Colour = "Red", Breed = "Jan Arden",},
                new Sire {RingNo ="IHU S 2012 2367", Colour = "Blue", Breed = "Jassen",},
            
            };

            sires.ForEach(s => context.Sires.Add(s));
            context.SaveChanges();

            var dams = new List<Dam>
            {
                new Dam {RingNo ="IHU S 2009 5362", Colour = "Blue", Breed = "Jassen",},
                new Dam {RingNo ="IHU S 2011 9203", Colour = "Red", Breed = "Jan Arden",},
                new Dam {RingNo ="IHU S 2012 2315", Colour = "Blue", Breed = "Jassen",},
            
            };

            dams.ForEach(s => context.Dams.Add(s));
            context.SaveChanges();


            var youngsters = new List<Youngster>
            {
                new Youngster {RingNo ="IHU S 2015 001", Colour = "Blue", Breed = "Jassen",},
                new Youngster {RingNo ="IHU S 2015 002", Colour = "Red", Breed = "Jan Arden",},
                new Youngster {RingNo ="IHU S 2015 003", Colour = "Blue", Breed = "Jassen",},
            
            };

            youngsters.ForEach(s => context.Youngsters.Add(s));
            context.SaveChanges();

            var pairs = new List<Pair>
            {
                new Pair {Sire = "IHU S 2009 1143", Dam = "IHU S 2009 5362", Paired = DateTime.Parse("2015-12-01"), Hatched = DateTime.Parse("2015-12-20"), Youngster = "IHU S 2015 001",},
                new Pair {Sire = "IHU S 2011 1467", Dam = "IHU S 2011 9203", Paired = DateTime.Parse("2015-12-01"), Hatched = DateTime.Parse("2015-12-19"), Youngster = "IHU S 2015 002",},
                new Pair {Sire = "IHU S 2012 2367", Dam = "IHU S 2012 2315", Paired = DateTime.Parse("2015-12-01"), Hatched = DateTime.Parse("2015-12-22"), Youngster = "IHU S 2015 003",},
            };

            pairs.ForEach(s => context.Pairs.Add(s));
            context.SaveChanges();
        }
    } }
using System;
using System.Collections.Generic; 
using System.Linq; using System.Web; 
using System.Data.Entity; 
using System.Data.Entity.ModelConfiguration.Conventions; 
using LoftBook.Models;

namespace LoftBook.DAL {
    public class LBContext : DbContext
    {

        public LBContext()
            : base("LBContext")
        {

        }


        public DbSet<Dam> Dams { get; set; }
        public DbSet<Sire> Sires { get; set; }
        public DbSet<Pair> Pairs { get; set; }
        public DbSet<Youngster> Youngsters { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    } }

提前致谢

杰森

【问题讨论】:

  • 您要更改哪些设置?观察到的结果与您的预期结果是什么?截取您遇到问题的 web.config 部分怎么样?
  • 如果您的解决方案中有多个项目,请确保您正在查看正确的配置文件。如果您在 MVC 项目中工作,请确保您查看的是根目录中的 web.config,而不是 MVC 区域或其他文件夹中的。
  • 感谢大家的回复。我正在尝试“设置 EF 以使用 SQL Server Express LocalDB 数据库”,这是我在 asp.net 网站上关注的指南的标题。我确定我检查了根文件夹而不是另一个,但它并没有很混乱!
  • 您的数据模型和 DbContext 是否与您的 mvc Web 项目在同一个项目中,或者您是否有 DataAccessLayer 或 BusinessLocicLayer?

标签: asp.net-mvc ef-code-first entity-framework-6


【解决方案1】:

首先,我认为您没有将实体框架添加到您的项目中。

1.先从 Nuget 包管理器安装实体

2.右键单击项目中的模型,然后添加>新项目将数据库添加到项目中

这是Get Start with Entity Framework的完整指南

本文说明您在 sql server 中使用 diagrams 设计了一个数据库,并且您希望从中生成模型(最简单的方法是我以前试过)

【讨论】:

  • 您好,感谢您的回复。我已经安装了我检查了 Nuget 包管理器,它已安装在我的项目中。它仍然没有出现在 web.config 中。我正在尝试执行此“设置 EF 以使用 SQL Server Express LocalDB 数据库”,这是我正在遵循的指南的标题。
  • 请检查您的链接,您的链接无效。您使用的是哪个版本的 Visual Studio?
  • 好的。首先在解决方案浏览器中检查 'Reference' 以找出您有 'EntityFramework' 、 'EntityFramework.SqlServer' 等有关键字 'entity' 。如果您没有找到它们,请右键单击参考并通过单击“添加参考”添加并搜索“实体”关键字并添加它们。然后在您的项目上单击鼠标右键,然后单击“Propertise”。在“打包/发布 SQL”选项卡中,您可以为您的 EF 添加连接字符串。
  • 我的参考资料中有 EntityFramework 和 EntityFramework.SqlServer
  • 您是否检查了“打包/发布 SQL”选项卡?
猜你喜欢
  • 2011-03-09
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2015-02-24
  • 2011-11-16
  • 2017-05-06
  • 2013-06-16
相关资源
最近更新 更多