简体   繁体   English

尝试JsonConvert.SerializeObject时从“ ServerVersion”获取值时出错

[英]Error getting value from 'ServerVersion' when trying to JsonConvert.SerializeObject

I have a very basic code using Entity Framework to get a model in my MVC C# .NET Controller. 我有一个非常基本的代码,使用实体框架在MVC C#.NET Controller中获取模型。

var myModel = myContext.MyData
                .Where(m => m.ID == 1)
                .FirstOrDefault();

string json = Newtonsoft.Json.JsonConvert.SerializeObject(myModel);

When I try to run this code, I get an error: 当我尝试运行此代码时,出现错误:

Error getting value from ServerVersion on System.Data.SqlClient . System.Data.SqlClient ServerVersion获取值时ServerVersion

... If I press Continue , the view says: ...如果按Continue ,则视图显示:

[InvalidOperationException: Invalid operation. [InvalidOperationException:无效的操作。 The connection is closed.] 连接已关闭。]

What's wrong? 怎么了? What does SQL have to do with this? SQL与这有什么关系? The same error comes if I do this in the View instead of the Controller. 如果我在视图而不是控制器中执行此操作,则会出现相同的错误。

Edit: 编辑:

My class (model) 我的班级(模特)

namespace TrackerEntityFrameworks.Models
{
  public class MyData: DbContext
  {
    [Key]
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public DateTime StartTime { get; set; }
    [Required]
    public DateTime EndTime { get; set; }

    public ICollection<TripRecord> TripRecords { get; set; }
    public ICollection<TollRecord> TollRecords { get; set; }
  }
}

You're Trying to serialize the entire Dbcontext , don't do that! 您正在尝试序列化整个Dbcontext ,请不要这样做!

Remove the inheritance of MyData from DbContext and you should be fine. DbContext删除MyData的继承,您应该会很好。

Your business class must be "persistance ignorant", in order to be reusable and Entity Framework works perfectly in this way 您的业​​务类必须是“对持久性无知的”,以便可重用,并且实体框架可以通过这种方式完美地工作

Seperate the DbContext from your entity then do the following: 将DbContext与您的实体分开,然后执行以下操作:

namespace TrackerEntityFrameworks.Structure
{
  public class MyContext: DbContext
  {
    public DbSet<TripRecord> TripRecords { get; set; }
    public DbSet<TollRecord> TollRecords { get; set; }
    public DbSet<MyData> MyDatas { get; set; }
  }
}

namespace TrackerEntityFrameworks.Models
{
  public class MyData
  {
    [Key]
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public DateTime StartTime { get; set; }
    [Required]
    public DateTime EndTime { get; set; }

    // navigation properties: check how to implement relationships in EF Code First
    public ICollection<TripRecord> TripRecords { get; set; }
    public ICollection<TollRecord> TollRecords { get; set; }
  }
}


using(var myContext = new TrackerEntityFrameworks.Structure.MyContext())
{
  var result = myContext.MyDatas
                  .Where(m => m.ID == 1)
                  .FirstOrDefault();
  string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
}

Entity framework tutorial: http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx 实体框架教程: http : //www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM