2017-04-13 10 views
1

ASP.NET MVCで初心者ですが、Entity Framework Code-Firstでデータベースにアクセスしようとしていますが、エラーが発生しています。私はstackoverflowに関する多くの質問を見ましたが、私の状況には関係ありません。私のエラーです。C#:Entity Frameworkコードでデータベースにアクセスする - 最初

「System.InvalidOperationException」種類の例外がEntityFramework.dllで発生したが、ユーザーコード

追加情報で処理されませんでした:操作を完了できません。指定されたSqlConnectionは、初期カタログまたはAttachDBFileNameを指定しません。 var brands = lensStoreDB.Brands.ToList();ライン

LensStoreController.cs

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

Brands.csでLensStoreController.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; } 
    } 
} 

私は、そのデータ私は、ブラウザ上で表示する1つの以上のクラス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)); 
     } 
    } 
} 

私もDbContextとしてEyeContactLensEntities.csを使用しています:

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で開くことができます。そこから、接続する必要のあるものを正確に確認することができます。

EDIT: How do you open an SDF file (SQL Server Compact Edition)?

0

あなたの接続文字列でproviderNameでを変更する必要があります:SQL Server Management Studioをしていることを行う方法について リンク

<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

関連する問題