ASP.NET MVC - Switching from SQL Server Express to Compact Edition - Connection Issue

China☆狼群 提交于 2021-02-08 05:24:14

问题


I have a ASP.NET MVC 4 project - CodeFirst with Entity Framework - that was connected to a SQL Server Express database generated by EF and it was working just fine.

However, I am trying to deploy to AppHarbor and having some issues with the version of SQL running there connecting with my app.

So, I noticed that they have an example project that is utilizing SQL Server Compact Edition 4. Here is the web.config file for that project: https://github.com/friism/CodeFirstMigrations/blob/master/Web/Web.config

Also, I found a tutorial about how to include a SQL Server CE database in your project on Microsoft's ASP.NET site: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4

I decided to create a FranchiseManagerData.sdf file in my App_Data folder and modify my connection string and app settings to match theirs. Here's what my Web.config looks like:

<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <connectionStrings>
    <add name="FranchiseManagerEntities"
         connectionString="Data Source=|DataDirectory|FranchiseManagerData.sdf"
         providerName="System.Data.SqlServerCe.4.0"/>
  </connectionStrings>

When I run my application, I get a DataException error that says: The underlying provider failed on Open.

It looks like what is happening is that the application is still looking for the old database that was generated by Entity Framework that had the name: FranchiseManagerTest.Models.FranchiseManagerContext - EF was naming it after my data context class.

Here is the error that is displayed after stopping debugging:

 Cannot open database "FranchiseManagerTest.Models.FranchiseManagerContext" requested by the login. The login failed.
Login failed for user 'COMPANY\UserName'.

I have tried cleaning and rebuilding the solution. Also, I've deleted the contents of the bin folder and did a clean rebuild again, however, it still seems to be looking for the old database connection.

What am I doing wrong? How can I get my project to look for the local SQL Server CE database I made and not the previous database?

If I need to supply any further details please let me know.

Thanks in advance.


EDIT

Here is a portion of the System.Web section of my Web.config file:

<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"></customErrors>
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

回答1:


FormsAuthentication doesn't by default have the ability to use Sql CE (to my knowledge).
I have used EJ's SQL Server Compact 4.0 ASP.NET Membership provider without any issues.

Also, it sounds like you'll have to update your Entity Framework connection to use the SQL Server CE database correctly.

Update

As of .Net 4.5, the new SimpleMemembership Providers and Univerasl Providers naively support Sql CE.



来源:https://stackoverflow.com/questions/11421733/asp-net-mvc-switching-from-sql-server-express-to-compact-edition-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!