MVC4建立DBContext的EF6数据
MVC4建立DBContext的EF6數據時
1.需要using System.Data.Entity;命名空間
2.此命名空間需要安裝EntityFromwork.dll,此dll可以在項目——》管理NuGet程序包里聯機搜索EntityFromwork并安裝即可
3.建立類wenbingDBContext并繼承DbContext,此時就需要上面的命名空間了
4.程序代碼如下:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace wenbing.Models
{
? ? public class?wenbingDBContext : DbContext
? ? {
? ? ? ? public wenbingDBContext()
? ? ? ? ? ? : base("name=at_advertisement")
? ? ? ? { }
? ? ? ? public DbSet accounts { get; set; }
? ? }
}
5,在2中安裝EntityFromwork成功后會在項目根目錄下的web.config里面出現以下xml文件,如下紅色xml
<configuration>
? <configSections>
? ? <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
? ? <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
? </configSections>
? <appSettings>
? ? <add key="webpages:Version" value="2.0.0.0" />
? ? <add key="webpages:Enabled" value="false" />
? ? <add key="PreserveLoginUrl" value="true" />
? ? <add key="ClientValidationEnabled" value="true" />
? ? <add key="UnobtrusiveJavaScriptEnabled" value="true" />
? </appSettings>
? <connectionStrings>
? ? <add name="at_advertisement" connectionString="Server=mysql55.cloudsapp.cn;database=at_advertisement;port=3312;uid=root;password=159357;" />
? </connectionStrings>
? <system.web>
? ? <compilation debug="true" targetFramework="4.0" />
? ? <pages>
? ? ? <namespaces>
? ? ? ? <add namespace="System.Web.Helpers" />
? ? ? ? <add namespace="System.Web.Mvc" />
? ? ? ? <add namespace="System.Web.Mvc.Ajax" />
? ? ? ? <add namespace="System.Web.Mvc.Html" />
? ? ? ? <add namespace="System.Web.Routing" />
? ? ? ? <add namespace="System.Web.WebPages" />
? ? ? </namespaces>
? ? </pages>
? </system.web>
? <system.webServer>
? ? <validation validateIntegratedModeConfiguration="false" />
? ? <modules runAllManagedModulesForAllRequests="true" />
? ? <handlers>
? ? ? <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
? ? ? <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
? ? ? <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
? ? ? <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
? ? ? <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
? ? ? <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
? ? </handlers>
? </system.webServer>
? <entityFramework>
? ? <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
? ? ? <parameters>
? ? ? ? <parameter value="v11.0" />
? ? ? </parameters>
? ? </defaultConnectionFactory>
? ? <providers>
? ? ? <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
? ? </providers>
? </entityFramework>
</configuration>
轉載于:https://blog.51cto.com/hww9011/1541227
總結
以上是生活随笔為你收集整理的MVC4建立DBContext的EF6数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dynamic Web TWAIN扫描识
- 下一篇: s3c2440移植MQTT