的底線是,我無法將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="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True""
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 \」;「
我知道這有很多需要閱讀的內容,但我無法想象爲了讓這件事端對端地完成工作而做的任何事情。我可以使用一些幫助。