1

的底线是,我无法将EF模型连接到数据库,但我给一些背景资料:WCF RIA服务+实体框架4无法连接到数据库

解决方案布局为使(从Silverlight业务应用程序模板)工作:

  • 解决方案
    • 溶液(Silverlight的用户界面)
    • solution.domain(包含model.edmx并显示如下信息库)
    • solution.Web(RIA服务)

solution.domain

我创建使用EF设计模型。它的布局,像这样5类:

Account * - * Contact 1 - * PhoneNumber 
    1 
    | 
    * 
Location * - 1 Address 
  • 帐户有联系人列表和位置列表
  • 联系有名字/姓氏还有联系电话清单
  • ******中国有一般AREACODE /行李箱/号/描述
  • 位置无关,但地址列表
  • 地址有通常的StreetAddress /市/ ProvinceState/PostalZip
  • 每个实体都有Id字段

在这个项目中还有一个IRepository它一般是这样this one(与该项目后续的修改),但它在这里列出来:

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; */ 
} 

solution.Web

我还没有触及这个项目,除了添加一个名为“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" /> 

其中数据库名是一个数据库,在的SQLExpress确实存在(以及是使用model.edmx.sql创建的)。

解决方案

我们的结果,在Silverlight项目,我添加了另一个浏览所谓的“账户”上有一个数据网格,添加一个按钮,导航到那个页面,并添加这对代码隐藏:

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

结果

当我运行Silverlight的网站,它加载很好,我可以导航到首页/ p年龄,但是当我导航到帐户页面时,我收到了一些例外情况。目前我收到的最多的是:

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

那一个让我有点,但是当我在'this.context = new ObjectContext(connString);'上创建断点时'我注意到,它是获得连接字符串:

数据源= \ SQLEXPRESS;集成安全性= SSPI; AttachDBFilename = | DataDirectory目录| ASPNETDB.MDF;用户实例=真

这是在我的解决方案没有在任何地方都...

即使这样,如果我手动输入我在web.config中已经得到了连接字符串,而不是我得到:

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

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

“元数据= 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 \”;“

我知道这有很多需要阅读的内容,但我无法想象为了让这件事端对端地完成工作而做的任何事情。我可以使用一些帮助。

回答

0

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