2017-04-13 113 views
1

我是ASP.NET MVC的初學者,並嘗試使用Entity Framework Code-First訪問數據庫,但出現錯誤。我在stackoverflow上看到了很多問題,但它們與我的情況無關。我的錯誤是。C#:使用實體框架代碼訪問數據庫 - 第一

「System.InvalidOperationException」類型的異常出現在EntityFramework.dll但在用戶代碼中沒有處理

其他信息:無法完成操作。提供的SqlConnection不指定初始目錄或AttachDBFileName。

在LensStoreController.cs

var brands = lensStoreDB.Brands.ToList();

LensStoreController.cs

public ActionResult Index() 
{ 
    var brands = lensStoreDB.Brands.ToList(); 
    return View(brands); 
} 

Brands.cs,Lenses.cs和Manufacturer.cs是我的模型類

Brand.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace EyeContactLens.Models 
{ 
    public class Brands 
    { 
     [Key] 
     public int BrandId { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public List<Lenses> Lenses { get; set; } 

    } 

} 

個Lenses.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace EyeContactLens.Models 
{ 
    public class Lenses 
    { 
     [Key] 
     public int LensesId { get; set; } 
     public int BrandId { get; set; } 
     public int ManufacturerId { get; set; } 
     public string Title { get; set; } 
     public decimal Price { get; set; } 
     public string LensManuUrl { get; set; } 
     public Brands Brand { get; set; } 
     public Manufacturer Manufacturer { get; set; } 
    } 
} 

Manufacturer.cs

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

namespace EyeContactLens.Models 
{ 
    public class Manufacturer 
    { 
     public int ManufacturerId { get; set; } 
     public string Name { get; set; } 
    } 
} 

我有一個SampleData.cs多個類,我想顯示在瀏覽器,其數據。

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

namespace EyeContactLens.Models 
{ 
    public class SampleData : DropCreateDatabaseIfModelChanges<EyeContactLensEntities> 
    { 
     protected override void Seed(EyeContactLensEntities context) 
     { 
      var brands = new List<Brands> 
      { 
       new Brands { Name = "Cooper Vision"}, 
       new Brands { Name = "Fresh Kon"}, 
       new Brands { Name = "Flexcon"}, 
       new Brands { Name = "Avaira"}, 
      }; 

      var manufacturer = new List<Manufacturer> 
      { 
       new Manufacturer { Name = "Oculus"}, 
       new Manufacturer { Name = "Alcon (CIBA Vision)"} 
      }; 

      new List<Lenses> 
      { 
       new Lenses { Title = "Biofinity Contact Lens", Brand = brands.Single(b => b.Name == "Cooper Vision"), Price = 8.99M, Manufacturer = manufacturer.Single(a => a.Name == "Alcon (CIBA Vision)"), LensManuUrl = "/Content/Images/placeholder.gif" }, 
       new Lenses { Title = "FreshKon A55", Brand = brands.Single(b => b.Name == "Fresh Kon"), Price = 18.99M, Manufacturer = manufacturer.Single(a => a.Name == "Oculus"), LensManuUrl = "/Content/Images/placeholder.gif" }, 
       new Lenses { Title = "Flexcon Blue Tint UV Prolong wear (BUPW) (45%)", Brand = brands.Single(b => b.Name == "Flexcon"), Price = 81.99M, Manufacturer = manufacturer.Single(a => a.Name == "Oculus"), LensManuUrl = "/Content/Images/placeholder.gif" }, 
       new Lenses { Title = "Frequency 55 Toric Contact Lens", Brand = brands.Single(b => b.Name == "Cooper Vision"), Price = 10.99M, Manufacturer = manufacturer.Single(a => a.Name == "Alcon (CIBA Vision)"), LensManuUrl = "/Content/Images/placeholder.gif" }, 
       new Lenses { Title = "Freshkon N-Hance Toric", Brand = brands.Single(b => b.Name == "Fresh Kon"), Price = 11.99M, Manufacturer = manufacturer.Single(a => a.Name == "Oculus"), LensManuUrl = "/Content/Images/placeholder.gif" } 
      }.ForEach(a => context.Lenses.Add(a)); 
     } 
    } 
} 

我也使用EyeContactLensEntities.csDbContext

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

namespace EyeContactLens.Models 
{ 
    public class EyeContactLensEntities : DbContext 
    { 
     public DbSet<Lenses> Lenses { get; set; } 
     public DbSet<Brands> Brands { get; set; } 
    } 
} 

web.config

<connectionStrings> 
    <add name="EyeContactLensEntities" connectionString="Data Source=|DataDirectory|EyeContactLens.sdf" providerName="System.Data.SqlClient"/> 
</connectionStrings> 

的Global.asax.cs

protected void Application_Start() 
{ 
    System.Data.Entity.Database.SetInitializer(new Models.SampleData()); 

    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
} 
+0

爲什麼使用Access數據庫?爲什麼不把SQL Server Developer Edition放到你的電腦上? – mason

+0

因爲我想通過我的問題中提到的SampleData在瀏覽器上顯示數據。 –

+0

這不能回答我的問題。 – mason

回答

0

您的合作連接字符串錯誤。 您需要在配置字符串中提供的內容是數據源(例如SQL Server實例)和初始目錄(例如該服務器上的數據庫)。

.sdf可以用Sql Server Management Studio打開,從那裏你應該能夠看到你需要連接到什麼。

編輯: 如何做到這一點與SQL Server Management Studio中的鏈接: How do you open an SDF file (SQL Server Compact Edition)?

0

您需要更改的providerName在連接字符串中:

<connectionStrings> 
    <add name="EyeContactLensEntities" 
     connectionString="Data Source=|DataDirectory|EyeContactLens.sdf" 
     providerName="System.Data.SqlSqlServerCe.4.0"/> 
</connectionStrings> 

而且你需要安裝SQL Server Compact EF Nuget包:

Install-Package EntityFramework.SqlServerCompact 
+0

我改變了它...但它仍然顯示我相同的錯誤 –

+0

我已更新我的回覆! – ErikEJ

相關問題