【发布时间】: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