问题描述
我有以下EF code首先code。这是工作的罚款。但是,当我寻找的'club2.Members,它说下面的值:
信息是:
我没有设定值成员属性。没关系。但它不应该在内部引起任何异常。
- 有没有办法解决这个例外?
- 在是否取消这一例外帮助提高性能?
- 为什么这个异常并没有引起终止程序执行?
注:我没有(在这里,我叫点)需要在实体俱乐部会员数据。即使没有会员加入俱乐部在那个时候。我需要的是摆脱例外;不使用贪婪加载数据加载。如何避免在除了?
命名空间LijosEF
{
公共类Person
{
公众诠释PERSONID {获得;组; }
公共字符串PersonName的{获得;组; }
公共虚拟的ICollection<俱乐部GT;俱乐部{获得;组; }
}
公共类俱乐部
{
公众诠释ClubId {获得;组; }
公共字符串ClubName {获得;组; }
公共虚拟的ICollection<人>成员{获得;组; }
}
//System.Data.Entity.DbContext是EntityFramework.dll
公共类NerdDinners:System.Data.Entity.DbContext
{
公共NerdDinners(字符串CONNSTRING):基地(CONNSTRING)
{
}
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
//流利的API - 复数去除
modelbuilder.Conventions.Remove&其中; PluralizingTableNameConvention>();
}
公共DbSet<人>人{获得;组; }
公共DbSet<俱乐部GT;俱乐部{获得;组; }
}
}
静态无效的主要(字串[] args)
{
Database.SetInitializer&其中; NerdDinners>(新MyInitializer());
CreateClubs();
CreatePersons();
}
公共静态无效CreateClubs()
{
字符串的ConnectionString =数据源=;初始目录= NerdDinners;集成安全性= TRUE;连接超时= 30;
使用(VAR DB =新NerdDinners(的ConnectionString))
{
俱乐部club1 =新东家();
club1.ClubName =club1;
俱乐部club2 =新东家();
club2.ClubName =club2;
俱乐部club3 =新东家();
club3.ClubName =club3;
db.Clubs.Add(club1);
db.Clubs.Add(club2);
db.Clubs.Add(club3);
INT recordsAffected = db.SaveChanges();
}
}
公共静态俱乐部GetClubs(字符串clubName)
{
字符串的ConnectionString =数据源=;初始目录= NerdDinners;集成安全性= TRUE;连接超时= 30;
使用(VAR DB =新NerdDinners(的ConnectionString))
{
VAR的查询= db.Clubs.SingleOrDefault(P => p.ClubName == clubName);
返回查询;
}
}
公共静态无效CreatePersons()
{
字符串的ConnectionString =数据源=;初始目录= NerdDinners;集成安全性= TRUE;连接超时= 30;
使用(VAR DB =新NerdDinners(的ConnectionString))
{
俱乐部club1 = GetClubs(club1);
俱乐部club2 = GetClubs(club2);
俱乐部club3 = GetClubs(club3);
//更多code无以复加(使用DB)加人背景和保存
}
}
方法1
没有必要为以下方法。这可以在present上下文来完成。这只是一个选择。
GetClubs(club1);
方法2
附加的实体解决了这个问题:
俱乐部club1 = GetClubs(club1);
俱乐部club2 = GetClubs(club2);
俱乐部club3 = GetClubs(club3);
((IObjectContextAdapter)DB).ObjectContext.Attach((IEntityWithKey)club1);
((IObjectContextAdapter)DB).ObjectContext.Attach((IEntityWithKey)club2);
((IObjectContextAdapter)DB).ObjectContext.Attach((IEntityWithKey)club3);
VAR测试= club2.Members;
我不应该试图参阅club2.Members(俱乐部类),因为它不需要用于创建Person对象
会员相关的俱乐部级的数据。既然是虚拟的,它会做延迟加载。
在不同的情况下,如果我不得不在不同的上下文中检索成员对象(这是一个相关的数据),我应该依靠预先加载(其中为您提供俱乐部对象的地方)。
另请参阅以下内容:
-
Entity框架:重复记录中的许多一对多的关系
-
System.ObjectDisposedException: ObjectContext中实例已被释放,并可以不再被用于需要的一个连接操作
-
The ObjectContext的实例已经被设置并且不再能够用于需要的一个连接操作
-
ObjectContext例如已被释放,同时结合
I have following EF code first code. It is working fine. But when I look for the value of 'club2.Members', it says following:
Message is:
I have not set value for Members property. That’s okay. However it should not cause any exception internally.
- Is there a way to overcome this exception?
- Does removal of this exception help in improving performance?
- Why this exception did not cause termination of program execution?
Note: I don't need the Members data in club entity (at the point where I have called). Even there is no members added to the club at that time. All i need is to get rid of the exception; not to load data using eager load. How to avoid the exception?
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
}
}
APPROACH 1
There is no need for the following method. This can be done in the present context. It is only a select.
GetClubs("club1");
APPROACH 2
Attaching the entities resolved the problem:
Club club1 = GetClubs("club1");
Club club2 = GetClubs("club2");
Club club3 = GetClubs("club3");
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)club1);
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)club2);
((IObjectContextAdapter)db).ObjectContext.Attach((IEntityWithKey)club3);
var test = club2.Members;
I should not have tried to refer club2.Members (of Club class), since it is not needed for creating Person object.
Members is related data in Club class. Since it is virtual, it will do lazy loading.
In a different scenario, if I had to retrieve Members object (which is a related data) from a different context, I should rely on eager loading (in the place which provides you with Club object).
Refer following also:
这篇关于类型的异常“System.ObjectDisposedException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!