Web API和EF导致InvalidOperationException [英] Web API and EF cause InvalidOperationException

查看:178
本文介绍了Web API和EF导致InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有3个项目:DataModel(EF),DAL,可与DataModel和MVC Web API中的实体一起使用.

I have 3 projects in my solution: DataModel (EF), DAL which works with Entities from DataModel and MVC Web API.

只有2个非常简单的实体:人",地址"中每个都有3个简单字段,人"具有地址"字段(因此这2个实体是链接的)

There are only 2 very simple entities: Person, Address with 3 simple fields in each of them and Person has the Address field (so these 2 entities are linked)

在我的DAL中,我有一个方法可以向我返回所有人员的列表,内容非常简单:return context.Person.ToList();

In my DAL I have a method that returns me the list of all the Persons, the content is very simple: return context.Person.ToList();

现在在我的Web API中,我只是调用GetPersons()方法并尝试返回它. 在这里,我收到一条奇怪的错误消息:

Now In my Web API I'm simply calling for the GetPersons() method and trying to return it. And here I get a strange error message:

发生错误.","ExceptionMessage":'ObjectContent`1' 类型未能序列化内容类型的响应正文 'application/json; charset = utf-8'.," ExceptionType:" System.InvalidOperationException," StackTrace:null," InnerException:{" $ id:" 2," Message:" An 错误发生了.,...

"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"An error has occurred.",...

当我调试时,我可以看到我确实有GetPersons方法中的数据. 我还用谷歌搜索并找到了唯一的解决方案:我应该在我的启动配置中添加以下几行:

When I debug I can see that I do have the data from my GetPersons method. I also googled and found the only solution: I should have added the following lines into my start config:

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

但这没有帮助.

我还尝试不使用数据库手动填充列表:在这种情况下,它可以正常工作.

I also tried to populate the list manually without using the database: and in this case it works.

我有一个很强烈的印象,那就是它与EF有关,但我不知道那到底是什么.

I have a strong impression that it has something to do with EF but I don't know exactly what's that.

推荐答案

  • 分步指南...
  • 步骤1:转到您的实体项目,然后找到从DbContext类继承的类的名称.

    Step 1: Go to your Entity Project and then find the name of the class which inherits from DbContext class.

    例如:如果类名称为MyProjectEntities,如下所示:

    For Example: If the class name is MyProjectEntities as follows:

    public partial class MyProjectEntities : DbContext
    {
       //Auto Generated statements if EF is used.
    }
    

    步骤2:转到您的WebApi项目,找到继承自ApiController的控制器,然后在该控制器中为MyProjectEntities类创建实例.

    Step 2: Go to your WebApi Project find the controller which inherits from ApiController and then create the instance for the MyProjectEntities class within that controller.

    例如:如果Api控制器的名称为PersonController,则为MyProjectEntities创建实例,如下所示:

    For Example: If the Api Controller name is PersonController then create the instance for MyProjectEntities as follows:

    public class PersonController : ApiController
    {
       MyProjectEntities DB = new MyProjectEntities();
    }
    

    步骤3:,为PersonController类创建构造函数,并将ProxyCreationEnabled属性设置为false,如下所示:

    Step 3: Create constructor for the PersonController class and set false to the ProxyCreationEnabled Property as follows:

    public PersonController()
    {
       DB.Configuration.ProxyCreationEnabled = false;
    }
    

    Web api控制器中的最终代码类似于以下内容:

    The final code within the web api controller looks similar to this:

    public class PersonController : ApiController
    {
       MyProjectEntities DB = new MyProjectEntities();
       public PersonController()
       {
          DB.Configuration.ProxyCreationEnabled = false;
       }
    ...
    ...
    

    这篇关于Web API和EF导致InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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