简体   繁体   English

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

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

The bottom line is that I cannot connect my EF model to the database, but I'll give some background information: 最重要的是,我无法将EF模型连接到数据库,但是我将提供一些背景信息:

The solution is laid out as so (working from the silverlight business application template): 解决方案的布局是如此(使用silverlight业务应用程序模板工作):

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

solution.domain solution.domain

I created the model using the EF designer. 我使用EF设计器创建了模型。 It's 5 classes laid out like so: 这样布置了5个班级:

Account * - * Contact 1 - * PhoneNumber
   1
   |
   *
Location * - 1 Address
  • Account has a list of contacts and a list of locations 客户有一个联系人列表和一个位置列表
  • Contact has firstname/lastname and a list of phonenumbers 联系人具有名字/姓氏和电话号码列表
  • PhoneNumber has the usual areacode/trunk/number/description PhoneNumber具有通常的区号/中继/号码/说明
  • Location has nothing but a list of addresses 位置仅是地址列表
  • Address has the usual StreetAddress/City/ProvinceState/PostalZip 地址具有通常的StreetAddress / City / ProvinceState / PostalZip
  • Each entity has the Id field 每个实体都有ID字段

In this project there is also an IRepository which generally is like this one (with the modifications for that entries followup), but its listed here as well: 在这个项目中,还有一个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;
}

The constructor for its implementation is: 其实现的构造函数为:

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

solution.Web 解决方案

I haven't touched this project, except to add a DomainService called "AccountService" including all of the tables in the model. 除了添加一个名为“ AccountService”的DomainService(包括模型中的所有表)外,我没有涉及到这个项目。 I created it to verify that I can get this going end-to-end; 我创建它是为了验证我是否可以端到端进行此操作。 I'll be removing it once I've figured out how to get it running in favour of more focused services. 一旦弄清楚如何使它运行以支持更具针对性的服务,就将其删除。 That generated a class, which is prototyped here: 生成了一个类,该类在此处原型化:

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

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

I changed the implementation of this class to include an instance of my repository, and to return queries from there in the service methods like so: 我更改了此类的实现,以包括我的存储库的一个实例,并通过类似的服务方法从那里返回查询:

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>();
}

both the Web.Debug.config and the Web.Release.config have connection strings defined as so: 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" />

where DatabaseName is a database, in SQLExpress that does exist (and was created using the model.edmx.sql). 其中DatabaseName是数据库,在SQLExpress中确实存在(并使用model.edmx.sql创建)。

solution

Now to view the results, in the silverlight project, I added another View called "Accounts" with a datagrid on it, added a button to the navigation to go to that page, and added this to the codebehind: 现在要查看结果,在silverlight项目中,我添加了另一个名为“ Accounts”的视图,上面带有一个数据网格,在导航中添加了一个按钮以转到该页面,并将其添加到后面的代码中:

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

result 结果

When I run the silverlight site, it loads fine, I can navigate to the home/about pages but when I navigate to the Accounts page, I have gotten a number of exceptions. 当我运行silverlight网站时,它可以很好地加载,我可以导航到主页/关于页面,但是当我导航到“帐户”页面时,我有很多例外。 Currently the one I get the most is: 目前我得到最多的是:

Load operation failed for query 'GetAccounts'. 查询“ GetAccounts”的加载操作失败。 Keyword not supported: 'data source'. 不支持的关键字:“数据源”。

That one got me for a bit, but when I made a breakpoint on 'this.context = new ObjectContext(connString);' 那让我有些困惑,但是当我在'this.context = new ObjectContext(connString);'上设置断点时 I noticed that the connection string it is getting is: 我注意到它得到的连接字符串是:

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

which is not anywhere in my solution at all... 这根本不在我的解决方案中的任何地方...

even so, if I manually enter the connection string that I've got in my web.config, instead I get: 即使这样,如果我手动输入在web.config中获得的连接字符串,那么我得到:

Load operation failed for query 'GetAccounts'. 查询“ GetAccounts”的加载操作失败。 The specified named connection is either not found in the configuration, not intended to be used with teh EntityClient provider, or not valid. 在配置中找不到指定的命名连接,或者不打算与EntityClient提供程序一起使用,或者无效。

In case anyone needs to know it, it's here: 如果有人需要知道它,它在这里:

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

I know that this was a lot to read, but I just can't think of anything else to do in order to get this thing workign end-to-end. 我知道要阅读很多东西,但是我想不出什么其他事情来使这东西端到端地起作用。 I could use some help. 我可以帮忙。

"LocalSqlServer" is the default connection string name for the authentication, role, and profile providers included in the business application template. “ LocalSqlServer”是业务应用程序模板中包括的身份验证,角色和配置文件提供程序的默认连接字符串名称。 Unless you've merged databases, I recommend using a different connection string name for your EF model. 除非您已合并数据库,否则建议为EF模型使用其他连接字符串名称。

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

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