Click here to Skip to main content
15,883,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

How to resolve "Unable To retrieve metadata for MVCApplication.Models.StudentList.The specified named connection is either not found in the configuration, not intended to be used with the EntiyClient provider, or not valid" during add controller in c# asp.net mvc 4?

I cant add controller in my application.

i am using Empty MVC Application, i have not choose internet application.

Please help me.

Thanks in advance.

Ankit Agarwal
Software Engineer
Posted
Updated 6-Nov-14 23:10pm
v2

This is because you need to create a context object for your data that will inherit from DbContext and add any needed connection details into your web.config. Alternatively, you can create a controller as a class instead and use non-EF data access.

If you want to use EF:
C#
public class StudentContext : DbContext
{
    DbSet<student> { get; set; }
}


Then in the controller creation, select Student as your Model and StudentContext as your context.

Alternatively, if you want to use StudentList, populated from wherever it's populated from, you can create new class and:

C#
public class StudentController : Controller
{

    public StudentContext()
    {
        var list = new StudentList(); // Assuming this is a self-initialized object
    }

    public ActionResult ListStudents()
    {
        return View(list);
    }

    //Other data access methods here
}


And so on.

Edit:
I also would like to direct you to a learner resource Learn MVC (Model View Controller) step by step in 7 days – Day 1[^]
 
Share this answer
 
v3
Comments
[no name] 7-Nov-14 7:43am    
I m using,

this code in a model class:-

public class StudentListModel
{
public List<studentlist> StudentListCollction { get; set; }
public StudentList StudentListDetail { get; set; }
}
public class StudentList
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Section { get; set; }
public int Marks { get; set; }
}

how can i resolve this error?
Nathan Minier 7-Nov-14 7:47am    
You naming conventions are a bit odd. Are you using entity Framework or a different persistence mechanism?
[no name] 7-Nov-14 7:49am    
I am using entity framework.
Nathan Minier 7-Nov-14 7:57am    
Okay, let's make the syntax more closely match reality:

public class Student
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Section { get; set; }
public int Marks { get; set; }
}

public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
}

Now in your main (project) web.config:

<configuration>
...
<connectionStrings>
<!-- Fix connection string and provider as needed -->
<add name="StudentContext" connectionString="Your Connection String" providerName="System.Data.SqlClient" />
</connectionStrings>

</configuration>

Then try to add your controller.
You should add the below code in the model :
public int ID { get; set; }

// below given an example :

public class Example

{

public int ID { get; set; } // this code ID helps to retrieve meta data.. And connecting to database

public string Name { get; set;}
}
// or try public int id { get; set; }
// or try public string ID { get ; set ; }
// or try public string id { get ; set ; }
// you should also mention
connectionStrings properly.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900