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

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

问题描述

我开发MVC应用程序,并使用剃刀语法。
我已经使用模型第一种方法。

我有两个实体,客户和铅。铅是从客户继承。

在IsActive属性为true,则客户视为铅,否则这将是一个客户。

请检查EDMX文件映像。

当我genrated DB从模型中,我得到两个表客户表和Customer_Lead
表。

这是客户表。

和这是Customer_Lead表。

现在的问题是,当我运行牵头实体的指标来看,其给出的误差。


  

传递到字典中的模型产品类型
  PagedList.PagedList1 [CRMEntities.Customer],但本词典
  需要类型的模型项目
  PagedList.IPagedList1 [CRMEntities.Lead]


我越来越糊涂ABT如何与实体继承工作时写的是从其他实体以及如何将这两个表访问数据继承了实体的看法。

索引视图code是如下...

  @model PagedList.IPagedList< CRMEntities.Lead>
@ {
    ViewBag.Title =指数;
}< H2>指数< / H>&所述p为H.;
    @ Html.ActionLink(新建,创建)
&所述; / P>
<表>
    &所述; TR>
        <第i个
            状态
        < /第i
        <第i个
            IsQualified
        < /第i
        <第i个
            名称
        < /第i
        <第i个
            地址
        < /第i
        <第i个
            OfficePhone1
        < /第i
        <第i个
            网站
        < /第i
        <第i个
            EMAIL2
        < /第i
        <第i个
            EMAIL1
        < /第i
        <第i个
            FaxNo
        < /第i
        <第i个
            备注
        < /第i
        <第i个
            评分
        < /第i
        <第i个
            业务类型
        < /第i
        <第i个
            IsActive
        < /第i    < / TR>@foreach(以型号VAR项){
    &所述; 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(详细信息,详细信息,新{ID = item.Id})|
            @ Html.ActionLink(删除,删除,新{ID = item.Id})
        < / TD>
    < / TR>
}< /表>

我有以下code在指数...

 公开的ViewResult指数()
{
    VAR LeadList =(从db.CustomersÇ
                    其中(c.IsActive ==真)
                    排序依据(c.Id)
                    选择C);   //我应该回报?}


解决方案

您认为看起来不错,但你要的客户名单传递给它。尝试从上下文中获得主要客户的名单。

 公众的ActionResult指数()
{
    ...
    VAR leadList = db.Customers.OfType< CRMEntities.Lead>()式(C => c.IsActive)。.OrderBy(C => c.Id);    返回查看(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天全站免登陆