2

I am following this tutorial: http://www.w3schools.com/aspnet/mvc_models.asp, to get data from SQL CE using Entity Framework. There doesn't seem to be much code. The tutorial is for MVC3 but my project is MVC4, but I think major steps should be same.

I created a SQL CE database, and a table with needed columns. Added data to the table. On the C# side I have

namespace MvcDemoApp.Models
{
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }

    }

    public class EmployeeContext : DbContext
    {
        public DbSet<Employee> employees { get; set; }
    }
}

And code in respective controller

public ActionResult Index()
{
    var c = db.employees.ToList();
    return View(c);
}

That sample is for movies database, I changed all relevant info to employees. I added the connection string as in that tutorial too. But when I navigate to the Index page of above controller, there is exception, please see exception details at bottom.

It seems I gave correct meaning to table columns too (e.g. similar to Employee class properties)

PS. This is how the DB looks after I stop running the app enter image description here

So what is the correct way to load data from SQL Compact using entiry framework in ASP MVC 4 application?


This is list of data providers on my machine, I printed it using some code I found:

enter image description here

this is my connection string

 <connectionStrings> <add name="EmployeeContext" connectionString="Data Source=c:\users\g\documents\visual studio 2012\Projects\TestMVCApp\TestMVCApp\App_Data\EmployeeDB.sdf" providerName="System.Data.SqlServerCe.4.0" /> </connectionStrings> 

UPDATE: This is the exception I get now:

Exception Details: System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.

Source Error:

Line 17: Line 18: EmployeeContext e = new EmployeeContext(); Line 19: var list =e.Employeeslist.ToList();//exception here

  • (I disagree with the teaching of the linked W3Schools tutorial as it demonstrates using a single Model class as both the DB Entity object and ViewModel simultaneously, this is incorrect) – Dai Oct 05 '15 at 17:01
  • @Dai: Can you please tell me what is the alternative way? Maybe show a link also? I am learning ASP MVC Now. Actually other links I have seen follow similar approach as above too. Btw. Did you spot anything I have done wrong above? –  Oct 05 '15 at 19:52
  • See here: http://stackoverflow.com/questions/3970763/asp-net-mvc-using-ef-entities-as-viewmodels – Dai Oct 05 '15 at 20:06

3 Answers3

0

Your web app opens an empty database file, check your file system for copies of the file. Try using a full path to the database file rather than |DataDirectory|

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • can you please elaborate what you mean? –  Oct 12 '15 at 20:26
  • This is my connection string: and this is exception I get now: *Exception Details: System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed* Any ideas? How is CE not installed, I added a database of it using new item, etc. –  Oct 13 '15 at 16:48
  • it seems that exception is related to my Employee model class –  Oct 13 '15 at 17:04
  • @quser: Did you try this link http://stackoverflow.com/questions/6865249/unable-to-find-the-requested-net-framework-data-provider-it-may-not-be-install – Mithun Pattankar Oct 13 '15 at 17:18
0

You probably need to reset database. It will launched the script in your Configuration class which create the example rows in database.

See ASP NET MVC 3 - How to reset database in code first, with two tables and Database.Setinitializer?

Shorty

  1. Open Package Manager Console window,
  2. Enter the following command: update-database
Community
  • 1
  • 1
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • This is my connection string: and this is exception I get now: *Exception Details: System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed* Any ideas? How is CE not installed, I added a database of it using new item, etc. –  Oct 13 '15 at 16:50
  • it seems that Exception is related to my Employee model class –  Oct 13 '15 at 17:04
0

Either the value of the providerName attribute on the connection string in your web.config is incorrect or provider on the web server is not installed. If the providername is

System.Data.SqlService.4.0 (Sql Server Compact)

its only used inside Visual Studio for development. If you are running on different machine having no visual studio with SQL compact, than you may need to install on that machine. If it does there on your machine than you can verify whether the version 4.0 is correct or not, means its the right version that your are looking for or not.

Hope this helps!