实体框架

时间:2015-12-11 20:05:06

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

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

  

  http://go.microsoft.com/fwlink/?LinkId=301880 - &gt;
            
            
                                    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>
     

使用System;使用System.Collections.Generic;使用System.Linq;   使用System.Web;使用System.Data.Entity;使用LoftBook.Models;

     

命名空间LoftBook.DAL {       public class LBInitializer:System.Data.Entity.DropCreateDatabaseIfModelChanges       {

    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”,},                   新对{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”,},                   新对{Sire =“IHU S 2012 2367”,Dam =“IHU S 2012 2315”,配对= 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();
    }
} }
     

使用System;使用System.Collections.Generic;使用System.Linq;   使用System.Web;使用System.Data.Entity;运用   System.Data.Entity.ModelConfiguration.Conventions;运用   LoftBook.Models;

     

命名空间LoftBook.DAL {       公共类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>();
    }
} }

提前致谢

杰森

1 个答案:

答案 0 :(得分:0)

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

1 。从Nuget Package Manager安装实体优先

2 。右键单击项目中的模型,然后添加&gt;新项,将数据库添加到项目中

以下是Get Start with Entity Framework

的完整指南

本文解释您已经在 sql server 中使用图表设计了一个数据库,并希望从中获取生成模型(最简单的方法是我之前尝试过)