繁体   English   中英

WCF RIA服务+实体框架4无法连接到数据库

[英]WCF RIA Services + Entity Framework 4 cannot connect to database

最重要的是,我无法将EF模型连接到数据库,但是我将提供一些背景信息:

解决方案的布局是如此(使用silverlight业务应用程序模板工作):

    • 解决方案(silverlight ui)
    • solution.domain(包含model.edmx和存储库,如下所示)
    • solution.Web(RIA服务)

solution.domain

我使用EF设计器创建了模型。 这样布置了5个班级:

Account * - * Contact 1 - * PhoneNumber
   1
   |
   *
Location * - 1 Address
  • 客户有一个联系人列表和一个位置列表
  • 联系人具有名字/姓氏和电话号码列表
  • PhoneNumber具有通常的区号/中继/号码/说明
  • 位置仅是地址列表
  • 地址具有通常的StreetAddress / City / ProvinceState / PostalZip
  • 每个实体都有ID字段

在这个项目中,还有一个IRepository,它通常像这样 (对条目后续内容进行了修改),但这里也列出了它:

public interface IRepository : IDisposable
{
    T GetByKey<T>(object key) where T : class;
    IQueryable<T> GetQuery<T>() where T : class;
    IQueryable<T> GetQuery<T>(Expression<Func<T, bool>> predicate) where T : class;
    IQueryable<T> GetQuery<T>(ISpecification<T> criteria) where T : class;
    T Single<T>(Expression<Func<T, bool>> predicate) where T : class;
    T Single<T>(ISpecification<T> criteria) where T : class;
    T First<T>(Expression<Func<T, bool>> predicate) where T : class;
    T First<T>(ISpecification<T> criteria) where T : class;
    void Add<T>(T entity) where T : class;
    void AddRange<T>(IEnumerable<T> entities) where T : class;
    void Attach<T>(T entity) where T : class;
    void Delete<T>(T entity) where T : class;
    void Delete<T>(Expression<Func<T, bool>> predicate) where T : class;
    void Delete<T>(ISpecification<T> criteria) where T : class;
    void SaveChanges();
    void SaveChanges(SaveOptions options);
    T Save<T>(T entity) where T : class;
    T Update<T>(T entity) where T : class;
    IQueryable<T> Find<T>(Expression<Func<T, bool>> predicate) where T : class;
    IQueryable<T> Find<T>(ISpecification<T> criteria) where T : class;
    T FindOne<T>(Expression<Func<T, bool>> predicate) where T : class;
    T FindOne<T>(ISpecification<T> criteria) where T : class;
    IQueryable<T> GetAll<T>() where T : class;
    IQueryable<T> Get<T>(int pageIndex, int pageSize) where T : class;
    IQueryable<T> Get<T>(Expression<Func<T, bool>> predicate, int pageIndex, int pageSize) where T : class;
    IQueryable<T> Get<T>(ISpecification<T> criteria, int pageIndex, int pageSize) where T : class;
    int Count<T>(Expression<Func<T, bool>> predicate) where T : class;
    int Count<T>(ISpecification<T> criteria) where T : class;
}

其实现的构造函数为:

public GenericRepository(string connString)
{
    /* ... */
    this.context = new ObjectContext(connString); 
    /* Defined in the class: private ObjectContext context; */
}

解决方案

除了添加一个名为“ AccountService”的DomainService(包括模型中的所有表)外,我没有涉及到这个项目。 我创建它是为了验证我是否可以端到端进行此操作。 一旦弄清楚如何使它运行以支持更具针对性的服务,就将其删除。 生成了一个类,该类在此处原型化:

[EnableClientAccess()]
public class AccountService : LinqToEntitiesDomainService<ModelContainer>
{
    [Query(IsDefault=true)]
    public IQueryable<Account> GetAccounts();

    /* repeat above for all types in the model */
}

我更改了此类的实现,以包括我的存储库的一个实例,并通过类似的服务方法从那里返回查询:

private IRepository repo;
public AccountService()
{
    this.repo = new GenericRepository(
        ConfigurationManager
        .ConnectionStrings["LocalSQLServer"]
        .ConnectionString);
}

[Query(IsDefault(true)]
public IQueryable<Account> GetAccounts()
{
    return this.repo.GetAll<Account>();
}

Web.Debug.config和Web.Release.config都具有如下定义的连接字符串:

<add name="LocalSQLServer"
     connectionString="metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True&quot;"
     providerName="System.Data.EntityClient" />

其中DatabaseName是数据库,在SQLExpress中确实存在(并使用model.edmx.sql创建)。

现在要查看结果,在silverlight项目中,我添加了另一个名为“ Accounts”的视图,上面带有一个数据网格,在导航中添加了一个按钮以转到该页面,并将其添加到后面的代码中:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    AccountContext context = new AccountContext();
    dataGrid1.ItemsSource = context.Accounts;
    context.Load(context.GetAccountsQuery());
}

结果

当我运行silverlight网站时,它可以很好地加载,我可以导航到主页/关于页面,但是当我导航到“帐户”页面时,我有很多例外。 目前我得到最多的是:

查询“ GetAccounts”的加载操作失败。 不支持的关键字:“数据源”。

那让我有些困惑,但是当我在'this.context = new ObjectContext(connString);'上设置断点时 我注意到它得到的连接字符串是:

数据源=。\\ SQLEXPRESS;集成安全性= SSPI; AttachDBFilename = | DataDirectory | aspnetdb.mdf;用户实例= true

这根本不在我的解决方案中的任何地方...

即使这样,如果我手动输入在web.config中获得的连接字符串,那么我得到:

查询“ GetAccounts”的加载操作失败。 在配置中找不到指定的命名连接,或者不打算与EntityClient提供程序一起使用,或者无效。

如果有人需要知道它,它在这里:

“ metadata = res:// /Model.Model.csdl|res:// /Model.Model.ssdl|res://*/Model.Model.msl;provider=System.Data.SqlClient;provider连接字符串= \\ “;数据源=。\\ SQLEXPRESS;初始目录=数据库名称;集成安全性=真; MultipleActiveResultSets =真\\”;“

我知道要阅读很多东西,但是我想不出什么其他事情来使这东西端到端地起作用。 我可以帮忙。

“ LocalSqlServer”是业务应用程序模板中包括的身份验证,角色和配置文件提供程序的默认连接字符串名称。 除非您已合并数据库,否则建议为EF模型使用其他连接字符串名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM