首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#:使用实体框架代码访问数据库-优先

C#:使用实体框架代码访问数据库-优先
EN

Stack Overflow用户
提问于 2017-04-13 13:08:04
回答 2查看 2.8K关注 0票数 1

我是ASP.NET MVC的初学者,并且尝试使用实体框架代码访问数据库--首先,但是我遇到了一个错误。我看到了很多关于堆叠溢出的问题,但它们与我的情况无关。我的错误是。

“System.InvalidOperationException”类型的异常发生在EntityFramework.dll中,但未在用户代码中处理 其他信息:无法完成操作。提供的SqlConnection没有指定初始目录或AttachDBFileName。 在LensStoreController.cs中的var brands = lensStoreDB.Brands.ToList();

LensStoreController.cs

代码语言:javascript
复制
public ActionResult Index()
{
    var brands = lensStoreDB.Brands.ToList();
    return View(brands);
}

Brands.cs、Lenses.cs和Manufacturer.cs是我的模型类

Brand.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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,我想在浏览器上显示它的数据。

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<connectionStrings>
    <add name="EyeContactLensEntities" connectionString="Data Source=|DataDirectory|EyeContactLens.sdf" providerName="System.Data.SqlClient"/>
</connectionStrings>

Global.asax.cs

代码语言:javascript
复制
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);
}
EN

回答 2

Stack Overflow用户

发布于 2017-04-13 14:30:17

你的连接字符串错了。您需要在配置字符串中提供的东西是数据源(例如Server实例)和初始目录(例如,服务器上的数据库)。

可以使用打开.sdf,从那里您应该能够看到需要连接到的确切内容。

编辑:关于如何使用:How do you open an SDF file (SQL Server Compact Edition)?执行此操作的链接

票数 0
EN

Stack Overflow用户

发布于 2017-04-14 07:05:40

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

代码语言:javascript
复制
<connectionStrings>
    <add name="EyeContactLensEntities" 
         connectionString="Data Source=|DataDirectory|EyeContactLens.sdf" 
         providerName="System.Data.SqlSqlServerCe.4.0"/>
</connectionStrings>

您需要安装包:

代码语言:javascript
复制
Install-Package EntityFramework.SqlServerCompact
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43393215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档