实体框架类型错误

时间:2013-06-18 05:00:04

标签: asp.net-mvc entity-framework

我以这种方式使用EF获取两个表数据并将其转换为列表以将其传递给视图,我在mvc4中工作,但我收到此错误:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.List<EFwithMVC.Models.dc_tp_personnel>' to 'System.Collections.Generic.List<EFwithMVC.Models.bc_limsEntities>'   C:\Users\Administrator\Documents\Visual Studio 2010\Projects\EFwithMVC\EFwithMVC\Controllers\PersonellController.cs 119 34  EFwithMVC

这是我的代码:

public  List<bc_limsEntities> GetTwoTableData()
    {
        using (var db = new bc_limsEntities())
        {
            return (from x in db.dc_tp_personnel.Include("dc_toutside_test")
                    where x.PersonId == 1
                    select x).ToList(); 
        }

    }

请帮助我,不知道为什么会出现这个错误

1 个答案:

答案 0 :(得分:1)

只是通过查看你的问题很明显,你要归还的是.dc_tp_personnel对象的集合

 return (from x in db.dc_tp_personnel.Include("dc_toutside_test")
                        where x.PersonId == 1
                        select x).ToList(); 

但是在您的返回类型中,您定义了List<bc_limsEntities>这是上下文btw,因此您获得的异常是因为您正在重新编辑dc_tp_personnel但返回类型为bc_limsEntities如果您返回类型dc_tp_personne l你不应该得到那个例外