gpt4 book ai didi

c# - 异常类型 'System.ObjectDisposedException'

转载 作者:行者123 更新时间:2023-11-30 14:00:13 25 4
gpt4 key购买 nike

我有以下 EF 代码优先代码。它工作正常。但是当我查找“club2.Members”的值时,它显示如下:

'club2.Members' threw an exception of type 'System.ObjectDisposedException'.

消息是:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

我没有为 Members 属性设置值。没关系。但是,它不应在内部引起任何异常。

  1. 有没有办法克服这个异常?
  2. 移除此异常是否有助于提高性能?
  3. 为什么这个异常没有导致程序执行终止?

注意:我不需要俱乐部实体中的成员(member)数据(在我调用的地方)。甚至当时还没有成员加入俱乐部。我所需要的只是摆脱异常;不要使用 eager load 加载数据。如何避免异常

enter image description here

namespace LijosEF
{

public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
public virtual ICollection<Club> Clubs { get; set; }
}

public class Club
{
public int ClubId { get; set; }
public string ClubName { get; set; }
public virtual ICollection<Person> Members { get; set; }

}




//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{

public NerdDinners(string connString): base(connString)
{

}

protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
//Fluent API - Plural Removal
modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

public DbSet<Person> Persons { get; set; }
public DbSet<Club> Clubs { get; set; }



}
}




static void Main(string[] args)
{
Database.SetInitializer<NerdDinners>(new MyInitializer());

CreateClubs();
CreatePersons();

}

public static void CreateClubs()
{

string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{

Club club1 = new Club();
club1.ClubName = "club1";

Club club2 = new Club();
club2.ClubName = "club2";

Club club3 = new Club();
club3.ClubName = "club3";

db.Clubs.Add(club1);
db.Clubs.Add(club2);
db.Clubs.Add(club3);

int recordsAffected = db.SaveChanges();


}
}

public static Club GetClubs(string clubName)
{
string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{

var query = db.Clubs.SingleOrDefault(p => p.ClubName == clubName);
return query;
}
}

public static void CreatePersons()
{
string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{

Club club1 = GetClubs("club1");
Club club2 = GetClubs("club2");
Club club3 = GetClubs("club3");

//More code to be added (using db) to add person to context and save
}
}

最佳答案

您收到此错误是因为您在执行查询后处理了 dbcontext。如果您想使用“成员”中的数据,请使用查询加载它: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

如果您不需要延迟加载,这意味着只加载您在查询中加载的数据(这将消除异常),请设置 LazyLoadingEnabled = false。看这里: Disable lazy loading by default in Entity Framework 4

关于c# - 异常类型 'System.ObjectDisposedException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11648504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com