3

I am using the Entity framework (code first) first time. I have created a context class with the following code.

public class ContactContext : DbContext
{
    public ContactContext()
        : base("DBConnectionString")
    {
        Database.SetInitializer<ContactContext>(new DropCreateDatabaseIfModelChanges<ContactContext>());
    }

    public DbSet<Contact> Contacts { get; set; }

}

Web.config file:

<add name="ContactMgrDBContext" connectionString="Data Source=(Local);Initial Catalog=ContactsDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

Controller class:

public class ContactController : Controller
{

    ContactContext db = new ContactContext();

// // GET: /Contact/

    public JsonResult ContactList(int? selectedContact)
    {

        IQueryable<Contact> contacts = db.Contacts;

        //contacts.ToList()

        var contactsJson = JsonConvert.SerializeObject(contacts.ToList());

        return Json(contactsJson, JsonRequestBehavior.AllowGet);

}

When I run the application in debug mode, I get the following exception at this statement.

var contactsJson = JsonConvert.SerializeObject(contacts.ToList());

An exception of type 'System.Data.ProviderIncompatibleException' occurred in EntityFramework.dll but was not handled in user code Additional information: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

Contact class code:

public class Contact
 {
    [Key]
    public int ContactId { get; set; }
    [Required, MaxLength(100)]
    public string FirstName { get; set; }
    [Required, MaxLength(100)]
    public string LastName { get; set; }
    public string EMail { get; set; }
    public string Phone { get; set; }
    public string BusinessName { get; set; }

 }
2
  • I am closing this question as the original problem of database connection is resolved. I am now facing an issue with rendering data (json format) into jqGrid. I will create a separate question for that. Thank you all for your help! Commented May 11, 2015 at 3:16
  • Here is the link to my other question related to Data not getting rendered by jqGrid. Even though data is being returned by the action method in JSON format. stackoverflow.com/q/30159205/1490250 Commented May 11, 2015 at 3:54

1 Answer 1

3

Your web.config declares a connection string of name "ContactMgrDBContext" while your context class references a connection string name "DBConnectionString".

Try updating your context class constructor to use the correct name and syntax:

public ContactContext()
    : base("name=ContactMgrDBContext")
{
    Database.SetInitializer<ContactContext>(new DropCreateDatabaseIfModelChanges<ContactContext>());
}

It also appears that you are trying to use localdb to work on your database. I tried running with your Data Source=(Local) name and got similar errors. You may want to try updating it to use the Data Source=(localdb)\v11.0 like so:

<add name="ContactMgrDBContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ContactsDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
Sign up to request clarification or add additional context in comments.

6 Comments

My bad! But even after making this correction, I am getting the same exception. Any other issue you see with the code?
@raringsunny I've updated my answer. See if that gets you any further.
I have been able to resolve database connection exception by correcting the localdb connection info. The v11.0 has to be mentioned as advised in @cmcquillan's response. However, my jqGrid control still does not display any data. Even when I populate data in the newly created database / table by entity framework, no records are listed. Pls refer to my code, especially public function ContactList in the controller. I'm returning JsonResult through this method.
@raringsunny Can you move the contacts.ToList() call outside of the serialization method and set the result to a variable. Then run your solution in the debugger and check whether or not there are any records in that variable on the server? That would help you figure out if the problem is with your database code or with the serialization step.
@raringsunny It might also help if you post the code for your Contact class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.