0

I have looked in to one of the solution you provided for unable to connecting to the SQL Server 2008 in mvc4..I followed your answer but even though i am getting error.

public class videoDBContext : DbContext
{
      static videoDBContext()
      {
        Database.SetInitializer<videoDBContext>(null);
      }

       public videoDBContext()
       : base("Name=videoDBContext")
       {
       }

       public DbSet<Video> Videos { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
         modelBuilder.Configurations.Add(new VideoMap());
       }
      }

domain class

     public class Video
     {
      public int Id { get; set; }
      public string Description { get; set; }
     }

Mapping class as

     public class VideoMap : EntityTypeConfiguration<Video>
     {
      public VideoMap()
      {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Description)
                            .IsRequired()
                            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("Video");
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Description).HasColumnName("Description");
       }
}

Controller action as

  public ActionResult Index()
  {
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    using (videoDBContext context = new videoDBContext())
    {
        var list = **context.Videos.ToList();**
    }

    return View();
  }

I am getting error near the line:

 Line 20:             var s = context.Videos.ToList();

web.config file is

<add name="videoDBContext" 
     connectionString="metadata=res://*/Models.xx.csdl|res://*/Models.xx.ssdl|res://*/Models.xx.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=servername;initial catalog=xx.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Please provide the solution. where am I going wrong? And sorry if it is silly or making you to inconvenience.

4

1 回答 1

0

如果你分析你的连接字符串,你会看到:

connectionString="...provider connection string=&quot;data source=servername;
                  initial catalog=xx.mdf;integrated security=True;

“提供者”连接字符串指向服务器servername(对吗?),并且似乎正在尝试引用.mdf文件。

你不应该生产中这样做!您应该引用服务器上存在的数据库,使用其逻辑数据库名称 - 不要乱用.mdf文件!

因此,将您的连接字符串更改为:

<add name="videoDBContext" 
     connectionString="metadata=res://*/Models.xx.csdl|res://*/Models.xx.ssdl|res://*/Models.xx.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=servername;initial catalog=Vedios;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

将 包含在EF 连接字符串initial catalog=Vedios;provider connection string深处。

于 2012-12-05T12:57:57.370 回答