MVC 4中的Db连接字符串

时间:2012-12-23 08:59:11

标签: string asp.net-mvc-4 connection

我有以下问题:

当我运行我的代码时:

 public ActionResult Index()
    {
        return View(db.Movies.ToList());
    }

我遇到错误说:无法完成操作。提供的SqlConnection未指定初始目录。

你能帮我修复连接字符串这是我的连接字符串:

 <connectionStrings>
      <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-MvcMovie-20121214234752;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  <add name="MovieDBContext" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient" />

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您是否调试过它接收的连接字符串?

以下是我的应用程序的连接字符串,它在开发和生产端运行

  <add name="connectionString" connectionString="server=.\SqlExpress;database=ABC; Connect Timeout=180;Integrated Security=true;" providerName="System.Data.SqlClient" />
    <add name="ApplicationServices" connectionString="data source=.;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />