C# 在asp.net mvc应用程序中重新启动应用程序时删除数据
使用localdb,应用程序成功运行,我将数据填入表中。一切都会顺利进行,直到您关闭应用程序并再次重新启动,发现您输入数据库的所有数据都丢失 下面是上下文类:C# 在asp.net mvc应用程序中重新启动应用程序时删除数据,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,使用localdb,应用程序成功运行,我将数据填入表中。一切都会顺利进行,直到您关闭应用程序并再次重新启动,发现您输入数据库的所有数据都丢失 下面是上下文类: using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using OnlineStore.Models; namespace OnlineStore.Models {
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using OnlineStore.Models;
namespace OnlineStore.Models
{
public class OnlineStoreDB : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Brand> Brands { get; set; }
}
}
Web.config的代码是:
<?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 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="OnlineStoreDB" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=OnlineStoreDB-20160923124929; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|OnlineStoreDB-20160923124929.mdf" 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>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
<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" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<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.5.2.14234" newVersion="1.5.2.14234" />
</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.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.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>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
实体框架
可以在每次应用程序启动时重新创建数据库。
也许,你有:
Database.SetInitializer<OnlineStoreDB>(new DropCreateDatabaseAlways<OnlineStoreDB>());
Database.SetInitializer(新的DropCreateDatabaseAlways());
在你的应用程序中的某个地方。将其删除或替换为默认的
CreateDatabaseIfNotExists
初始值设定项 “关闭应用程序并重新启动”…我想你是说在Visual Studio中,在调试模式下?可能每次都会给你创建一个空白的数据库文件。我忘了你是如何改变这一点的,但这应该是可能的,使之持续谢谢你的答复。我已经关闭应用程序并重新启动了好几次,但都没有用。每次重新启动时,它都会重新创建一个空白数据库文件,我无法找到这些设置在应用程序中的编码位置,以便我可以修改。请从内存中检查数据库文件的“复制到输出目录”属性。我想应该是在“请勿复制”上。在Global.cs中,从Database.SetInitializer(new DropCreateDatabaseAlways())更改;到Database.SetInitializer(新的CreateDatabaseIfNotExists());解决了这个问题。
Database.SetInitializer<OnlineStoreDB>(new DropCreateDatabaseAlways<OnlineStoreDB>());