c# - Disadvantages of using Entity Framework in cshtml view -


sometimes put of c# code in view (.cshtml) not of code. example have table named lawcategory , this:

@{   var db = new contracts.models.datacontext();   var lawcat = db.lawcategory.tolist();     } 

and use this:

 <div class="col-md-6">             <label>category:</label>             <select name="type" id="type" class="form-control">                 <option value="0">all</option>                 @foreach (var in lawcat)                 {                     <option value="@i.id" @(request.querystring["id"] == i.id.tostring() ? "selected" : "")>@i.name</option>                 }             </select>         </div> 

i want know disadvantages of using datacontext in view , connect database instead of send view model controller?

is true doing this, may cause set multi connection database? (entity framework)

you can use dbcontext (entityframework) code directly in view allowed , execute perfectly.

now problem comes design , mvc pattern itself.

  1. in mvc pattern , use separation of concern. here view has more 1 concern apart managing view. directly call entityframework , break point.

  2. it make code less testable. here want @ each view view might contains code , not specified in controller looking @ controller can not identify dependency.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -