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

时间:2022-07-31 21:54:31

I have a very basic code using Entity Framework to get a model in my MVC C# .NET Controller.

我有一个非常基本的代码使用Entity Framework来获取我的MVC C#.NET控制器中的模型。

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获取值时出错。

... If I press Continue, the view says:

...如果按继续,视图显示:

[InvalidOperationException: Invalid operation. The connection is closed.]

[InvalidOperationException:无效的操作。连接已关闭。]

What's wrong? What does SQL have to do with this? The same error comes if I do this in the View instead of the Controller.

怎么了? SQL与此有什么关系?如果我在View而不是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; }
  }
}

2 个解决方案

#1


1  

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

您的业​​务类必须“持久无知”,才能重用,Entity Framework以这种方式完美运行

#2


1  

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

#1


1  

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

您的业​​务类必须“持久无知”,才能重用,Entity Framework以这种方式完美运行

#2


1  

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