c# - How do you run a select statement in ASP.NET MVC 5 with Entity Framework, using the connection string in web.config? -


i have been searching day , find pieces of need , can't find complete , simple example of model, controller, , view should run simple select statement using database defined in web.config.

could 1 please show me example of model, view, controller should this? thank you!

controller:

public class testcontroller : controller {     private applicationdbcontext db = new applicationdbcontext(); // database connection referenced in web.config        public actionresult test()      {         string sql = @"select count(*) from..."; // finish query here         int count = db.database.sqlquery<int>(sql).single();         return view(count); // pass count view     } } 

view:

@model int <div> @model.tostring() </div> 

in example model int, use custom model came (and should match fields in select statement).

edit

added code database connection:

public class applicationdbcontext : identitydbcontext<applicationuser> {     public applicationdbcontext()     //: base("defaultconnection", throwifv1schema: false)         : base("defaultconnection")     {         ((iobjectcontextadapter)this).objectcontext.commandtimeout = 6000;     }      public static applicationdbcontext create()     {         return new applicationdbcontext();     } } 

put in model file (usually it'll in identitymodels, depending on how project generated).


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -