How to deal with inherited entity in EF and MVC app?

本秂侑毒 提交于 2019-12-13 01:17:05

问题


I am developing MVC application and using razor syntax. I have used model first method.

I have two entities, Customer and Lead. Lead is inherited from Customer.

When IsActive property is true then Customer treated as a Lead, otherwise it will be a customer.

Please check edmx file image.

When I genrated DB from model , I get the two tables Customer Table and Customer_Lead table.

This is Customer table.

and This is Customer_Lead Table.

Now the problem is when I run index view of lead entity , Its give the error.

The model item passed into the dictionary is of type PagedList.PagedList1[CRMEntities.Customer], but this dictionary requires a model item of type PagedList.IPagedList1[CRMEntities.Lead].

I am getting confused abt how to write a view for the entity which is inherited from other entity and how to access the data from these two tables while working with inherited entity.

Index View code is as below...

@model PagedList.IPagedList<CRMEntities.Lead>


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Status
        </th>
        <th>
            IsQualified
        </th>
        <th>
            Name
        </th>
        <th>
            Address
        </th>
        <th>
            OfficePhone1
        </th>
        <th>
            Website
        </th>
        <th>
            Email2
        </th>
        <th>
            Email1
        </th>
        <th>
            FaxNo
        </th>
        <th>
            Remark
        </th>
        <th>
            Rating
        </th>
        <th>
            BusinessType
        </th>
        <th>
            IsActive
        </th>

    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsQualified)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OfficePhone1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Website)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email2)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FaxNo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Remark)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BusinessType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsActive)
        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

I have following code in index...

public ViewResult Index()
{


    var LeadList = (from c in db.Customers
                    where (c.IsActive == true)
                    orderby (c.Id)
                    select c);

   //What should I return ? 

}

回答1:


Your view looks fine, but you're trying to pass a list of Customers to it. Try getting a list of Lead customers from context.

public ActionResult Index()
{
    ...
    var leadList = db.Customers.OfType<CRMEntities.Lead>().Where(c => c.IsActive).OrderBy(c => c.Id);

    return View(leadList);
}


来源:https://stackoverflow.com/questions/12618727/how-to-deal-with-inherited-entity-in-ef-and-mvc-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!