1

我有以下问题:

当我运行我的代码时:

 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; }
    }
}

任何帮助将不胜感激。

4

1 回答 1

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" />
于 2012-12-31T05:59:06.627 回答