C#:使用实体框架代码优先访问数据库
Posted
技术标签:
【中文标题】C#:使用实体框架代码优先访问数据库【英文标题】:C# : Access database with Entity Framework code-First 【发布时间】:2017-04-13 13:08:04 【问题描述】:我是 ASP.NET MVC 的初学者,并尝试使用 Entity Framework Code-First 进行数据库访问,但出现错误。我在 *** 上看到了很多问题,但它们与我的情况无关。我的错误是。
EntityFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理
附加信息:无法完成操作。提供的 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 是我的模型类
品牌.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;
镜头.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;
制造商.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.cs
作为DbContext
:
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);
【问题讨论】:
为什么要使用 Access 数据库?为什么不在您的计算机上安装 SQL Server Developer Edition? 因为我想通过我的问题中提到的 SampleData 在浏览器上显示数据。 这不能回答我的问题。 你的意思是,我必须为此目的使用 SQL 服务器? 我不确定将实体框架与 Access 一起使用。从未尝试过。但是 Access 作为网站数据库使用是一个糟糕的选择。访问并不意味着同时在多个上下文中使用。你最好使用 SQL Server,或者迁移到一些免费的等价物,如 SQLite、mysql、Postgres 等。不过,欢迎你尝试。该错误表明它不知道要使用哪个数据库。您需要在连接字符串中指定初始目录或 AttachDBFileName。 【参考方案1】:您的连接字符串错误。 您需要在配置字符串中提供的内容是数据源(例如 SQL Server 实例)和初始目录(例如该服务器上的数据库)。
可以使用 Sql Server Management Studio 打开一个 .sdf,从那里您应该能够看到您需要连接的具体内容。
编辑: 有关如何使用 SQL Server Management Studio 执行此操作的链接: How do you open an SDF file (SQL Server Compact Edition)?
【讨论】:
【参考方案2】:您需要更改连接字符串中的 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
【讨论】:
我已经更改了它...但仍然显示相同的错误以上是关于C#:使用实体框架代码优先访问数据库的主要内容,如果未能解决你的问题,请参考以下文章