【问题标题】:how to use ef code first with sql server ce in asp.net mvc 4 visual studio 2012如何在 asp.net mvc 4 Visual Studio 2012 中首先使用 ef 代码和 sql server ce
【发布时间】:2012-09-04 16:40:35
【问题描述】:

我正在尝试使用 SQL Server 精简版 4.0 创建一个简单的 EF 代码第一个示例。 使用的技术包括:Visual Studio 2012 Ultimate。

所以我创建了一个简单的 poco 类:

namespace MvcApplication2.Models
{
    public class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
    }
}

还有一个简单的上下文类:

namespace MvcApplication2.Models
{
    public class CustomerEntity : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }
}

然后我尝试使用模板创建一个控制器类:MVC Controller with read/write actions and views using entity framework.

  • 模型类 -> 客户
  • 数据上下文类 -> CustomerEntity

我收到以下错误:

无法检索元数据... 使用相同的 dbcompiledmodel 不针对不同类型的数据库服务器创建上下文 支持的。 ...

我应该说这个完全相同的代码使用 LocalDB 没有问题。

我认为唯一需要的区别是在 web.config 文件中添加了以下内容。

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

我还错过了什么吗?

我知道我不可能是唯一遇到此问题的人。

【问题讨论】:

    标签: asp.net-mvc entity-framework


    【解决方案1】:

    您在 EF 缓存中重复数据库和 App_Data 中的数据库的问题。解决此问题的最简单方法是注释您的默认构造函数 CustomerEntity,从模板创建控制器,然后取消注释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2015-04-12
      • 2013-11-11
      • 1970-01-01
      • 2013-01-17
      • 2014-05-28
      • 1970-01-01
      相关资源
      最近更新 更多