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.
in mvc pattern , use separation of concern. here view has more 1 concern apart managing view. directly call entityframework , break point.
it make code less testable. here want @ each view view might contains code , not specified in controller looking @ controller can not identify dependency.
Comments
Post a Comment