1

I'm using EntityFramework 6.1.3. I have generated models from an existing database. Now, when trying to query the database, datacontext returns null. The database appears to be empty.

But the database is not empty as I can connect and query it using LINQPad. When googling the problem I came across a similar situation. In that case the EF generated an empty database on the local server and the solution was to tweak the connection string, although without clear details what exactly should be changed.

However, I'm not sure if that is the case in my situation.

  1. My connection string is pointing to the remote server.
  2. It is not possible that the database was created on the remote server, as we don't have authorisation.
  3. The database was not created on my local machine as I dont even have local sql server installed. And my AppData folder is empty.

Any suggestions? Generated connection string below

     <add name="MAST_DEV" connectionString="metadata=res://*/Models.Mast_DevModel.csdl|res://*/Models.Mast_DevModel.ssdl|res://*/Models.Mast_DevModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxxxx;initial catalog=MASTER_DEV;user id=xxxxx;password=xxxxxx;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
Sentry
  • 4,102
  • 2
  • 30
  • 38
mirosz
  • 407
  • 7
  • 14
  • 1
    A first step could be: try to connect with Sql Server Management Studio, and see whether that connection string matches the one that you are using. – L-Four Jan 16 '17 at 10:14
  • Yep, I can connect. I'm absolutely sure all credentials and serve name are correct. I also use the same queries , so it is not en error of missing enumerator or something like that – mirosz Jan 16 '17 at 10:41

2 Answers2

1

I don't really know what I was doing wrong. I have created simple project from scratch , generated models again and it does work fine now. Possibly, previously I selected 'Code first from the database' from Entity Data Model Wizard. Whereas this time 'EF Designer from the database'. Anyway, thanks for trying to help. It was my mistake

mirosz
  • 407
  • 7
  • 14
0

This is the exact solution for my problem (the answer is copied from here Entity Framework cant use the DbContext, model being created)

"I see you are using the EDMX with Templates (.tt) for generating the classes. But if you are getting the information from a existing database, the wizard will create a ConnectionString compatible with ObjectContext (metadata informations and provider of entityframework).

The problem is that the connectionstring you are using is for ObjectContext (Database First and Model First). For the DbContext you should use the connectionstring without the metadata informations"

Community
  • 1
  • 1
mirosz
  • 407
  • 7
  • 14