ASP.NET Проблемы MVC и Entity Framework

#asp.net-mvc #vb.net #entity-framework

Вопрос:

Я создал небольшой учебный проект для чтения и записи в CRM с помощью утилит Cdata.

Я создал модель, контроллер и представление, но при попытке просмотреть страницу индекса я продолжаю получать сообщение об ошибке

new_accountactive не является допустимым полем

Я проверил и дважды проверил в CRM-системе и в базе данных SQL. Ищу указания на то, где я ошибся.

 ' GET: OriginatingAccounts (controller ) 
Function Index() As ActionResult
Private db As New DynamicsCRMContext
Dim _list = db.OriginatingAccounts.SqlQuery("SELECT id, new_accountserial, new_sun, new_sortcode, new_accountnumber, new_accountname  
                                             FROM new_oaccounts  
                                             WHERE new_accountactive = true").ToList

 Return View(_list)
 End Function
 

Модель

 Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema

<Table("new_oaccounts")>
Public Class OriginatingAccounts
<Key>
<Display(Name:="Account ID")>
Public Property Id As String
'<Display(Name:="Licence")>
'Public Property new_licenceid As String
<Display(Name:="Account Serial")>
Public Property new_accountserial As String
<Display(Name:="SUN")>
Public Property new_sun As String
<Display(Name:="Sort Code")>
Public Property new_sortcode As String
<Display(Name:="Account Number")>
Public Property new_accountnumber As String
<Display(Name:="Account Name")>
Public Property new_accountname As String

<Display(Name:="Active")>
Public Property new_accountactive As Nullable(Of Boolean)

<Display(Name:="Credit Account")>
Public Property new_creditingaccount As Boolean

<Display(Name:="Debit Account")>
Public Property new_debitingaccount As Boolean

<Display(Name:="SUN Validated")>
Public Property new_validated As Boolean
End Class
 

Представление индекса

 ModelType IEnumerable(Of TestProject.OriginatingAccounts)
@Code
ViewData("Title") = "Index"
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code

<h2>Originating Accounts</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<h4>Active Originating Accounts</h4>

<tr>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_accountserial)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_sun)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_sortcode)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_accountnumber)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_accountname)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_accountactive)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_creditingaccount)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_debitingaccount)
    </th>
    <th>
        @Html.DisplayNameFor(Function(model) model.new_validated)
    </th>
    <th></th>
</tr>

@For Each item In (Model.Where(Function(w) w.new_validated = True))
    @<tr>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_accountserial)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_sun)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_sortcode)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_accountnumber)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_accountname)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_accountactive)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_creditingaccount)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_debitingaccount)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) item.new_validated)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", New With {.id = item.Id}) |
            @Html.ActionLink("Details", "Details", New With {.id = item.Id}) |
            @Html.ActionLink("Delete", "Delete", New With {.id = item.Id})
        </td>
    </tr>
Next
</table>
 

Комментарии:

1. Ваш SQL-запрос не возвращает столбцы, соответствующие всем свойствам вашей сущности.

2. Попробуйте использовать 1 вместо True в своем запросе, так как в sql server значение бита равно 1 или 0. Поскольку вы пишете необработанный запрос SQL, поэтому используйте new_accountactive = 1

3. Почему вы используете SQL-запрос в первую очередь, а не запрос LINQ? Почему бы не использовать Dim _list = db.OriginatingAccounts.Where(Function(oa) oa.new_accountactive = True).ToList() ? На самом деле, поскольку модель вашего представления удалена как IEnumerable , вам даже не нужно ToList .