开发者

WCF RIA Services + Entity Framework 4 cannot connect to database

开发者 https://www.devze.com 2023-01-25 09:35 出处:网络
The bottom line is that I cannot connect my EF model to the database, but I\'ll give some background information:

The bottom line is that I cannot connect my EF model to the database, but I'll give some background information:

The solution is laid out as so (working from the silverlight business application template):

  • solution
    • solution (silverlight ui)
    • solution.domain (contains model.edmx and repository shown below)
    • solution.Web (RIA Services)

solution.domain

I created the model using the EF designer. It's 5 classes laid out like so:

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
  • Location has nothing but a list of addresses
  • Address has the usual StreetAddress/City/ProvinceState/PostalZip
  • Each entity has the Id field

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:

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;
开发者_StackOverflow社区    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. 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:

<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).

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:

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. Currently the one I get the most is:

Load operation failed for query '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);' I noticed that the connection string it is getting is:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=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:

Load operation failed for query 'GetAccounts'. The specified named connection is either not found in the configuration, not intended to be used with teh EntityClient provider, or not valid.

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\";"

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. Unless you've merged databases, I recommend using a different connection string name for your EF model.

0

精彩评论

暂无评论...
验证码 换一张
取 消