c# - MVC Entity Framework 连接字符串不起作用

标签 c# asp.net-mvc entity-framework asp.net-mvc-4

当我用谷歌搜索“MVC 连接字符串”或类似的内容时,我收到了一堆与此相关的问题/文章,并且我读过的所有内容都说要编辑 Web.config 文件。有什么原因导致这在我的项目中不起作用吗?

我的项目使用 Entity Framework ,现在它在本地数据库上运行。我编辑了 2 个 Web.config 文件(一个在项目文件夹中,另一个在 Views 文件夹中 - 我真的不知道有什么区别)。这些文件最初没有任何 <connectionStrings>标签,所以我手动将其添加到两个文件中:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=mysite.database.windows.net;Initial Catalog=Developer;User ID=user;Password=pass" providerName=".NET Framework Data Provider for SQL Server"/></connectionStrings>

..但它仍然使用我的本地主机数据库。我缺少任何明显的东西吗??

编辑:

删除调试和重建不起作用。

这是我的上下文类

public class TrackerContext : DbContext
{
    public TrackerContext() : base() {}
    public DbSet<Toll> Tolls { get; set; }
    public DbSet<TollRecord> TollRecords { get; set; }
    public DbSet<Trip> Trips { get; set; }
    public DbSet<TripRecord> TripRecords { get; set; }
}

没有连接字符串。我可以以某种方式强制它吗?

最佳答案

将构造函数更改为:

 public TrackerContext() : base("Name=DefaultConnection") {}

关于c# - MVC Entity Framework 连接字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35701503/

相关文章:

c# - TreeView 替代方案

c# - 如何通过 ADO.NET 加快向表中插入多行的速度?

asp.net-mvc - 过滤 EnumDropDownListFor ASP.NET MVC5

c# - Entity Framework 中没有显式外键的急切加载相关实体

c# - 如何将 Entity Framework 中的数据绑定(bind)到数据网格?

c# - linq 按 2 个条件计数

c# - 在C#中, `?`类型中的 `DateTime?`是什么

c# - 如何从 View 页面中的/controller/action/id 获取 id?

asp.net-mvc - 创建等同于 nopcommerce @T() 助手的 html 助手

entity-framework - Code First Entity Framework 模型中的一对多 - 关系如何工作?