如何处理EF和MVC应用程序中的继承实体? [英] How to deal with inherited entity in EF and MVC app?

查看:194
本文介绍了如何处理EF和MVC应用程序中的继承实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发MVC应用程序并使用剃刀语法。
我使用了第一种模式。



我有两个实体,客户和负责人。潜在客户继承自客户。



当IsActive属性为true时,客户被视为主角,否则将成为客户。



请检查edmx文件图像。





当我从模型中分解DB时,我得到了两个表Customer Table和Customer_Lead
表。



这是客户表。





这是Customer_Lead表。





现在的问题是当我运行引导实体的索引视图时,它给出错误。


传入到字典的模型项目的类型为
PagedList.PagedList1 [CRMEntities.Customer],但是这个字典
需要一个类型为
的模型项目PagedList.IPagedList1 [CRMEntities.Lead]。


我感到困惑为继承自其他实体的实体写入一个视图,以及如何在使用继承的实体时从这两个表中访问数据。



索引查看代码如下...

  @model PagedList.IPagedList< CRMEntities.Lead> 


@ {
ViewBag.Title =索引;
}

< h2>索引< / h2>

< p>
@ Html.ActionLink(创建新,创建)
< / p>
< table>
< tr>
< th>
状态
< / th>
< th>
IsQualified
< / th>
< th>
名称
< / th>
< th>
地址
< / th>
< th>
OfficePhone1
< / th>
< th>
网站
< / th>
< th>
Email2
< / th>
< th>
Email1
< / th>
< th>
FaxNo
< / th>
< th>
备注
< / th>
< th>
评级
< / th>
< th>
BusinessType
< / th>
< th>
IsActive
< / th>

< / tr>

@foreach(模型中的var item){
< 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(编辑,编辑,新的{id = item.Id})|
@ Html.ActionLink(Details,Details,new {id = item.Id})|
@ Html.ActionLink(Delete,Delete,new {id = item.Id})
< / td>
< / tr>
}

< / table>

我在索引中有以下代码...

  public ViewResult Index()
{


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

//我该怎么办?

}


解决方案

看起来不错,但是你正试图通过一个客户名单。尝试从上下文获取主要客户列表。

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

return View(leadList);
}


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 ? 

}

解决方案

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);
}

这篇关于如何处理EF和MVC应用程序中的继承实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆