我正在使用SpecFlow进行集成测试。
在下面的“Then”-step中,我在第expectedPersons.ShouldAllBeEquivalentTo(actualPersons);
行得到一个ObjectDisposedException:
ObjectContext实例已被释放,不能再用于需要连接的操作。
[Then(@"the following persons should be in the database")]
public void ThenTheFollowingPersonShouldBeInTheDatabase(Table table)
{
var expectedPersons = table.Rows.Select(expected => new Person
{
Id = int.Parse(expected["Id"]),
Name = expected["Name"],
Vorname = expected["Vorname"],
Adressen = specflowPersonenAdressen.Where(p => p.PersonId == int.Parse(expected["Id"])).ToList()
}).ToList();
var actualPersons = new List<Person>();
foreach (var row in table.Rows)
{
var queryCriteria = new PersonQueryCriteria
{
NameFilter = row["Name"],
VornameFilter = row["Vorname"]
};
actualPersons.AddRange(personWorkUnit.GetPersonen(queryCriteria));
}
expectedPersons.ShouldAllBeEquivalentTo(actualPersons);
}
}
方法personWorkUnit.GetPersonen(...)
如下所示:
public ICollection<Person> GetPersonen(PersonQueryCriteria queryCriteria)
{
using (var context = this.unitOfWorkContextFactory.Create())
{
var personRepository = this.repositoryFactory.Create<IPersonRepository>(context);
return personRepository.GetPersons(queryCriteria).ToList();
}
}
方法personRepository.GetPersons(...)
如下所示:
public ICollection<Person> GetPersons(PersonQueryCriteria personQueryCriteria)
{
var query = this.Context.GetQuery<Person>();
query
.Include(q => q.Adressen)
.Include("Adressen.Adresse");
if (!string.IsNullOrEmpty(personQueryCriteria.NameFilter))
{
query = query.Where(p => p.Name == personQueryCriteria.NameFilter);
}
if (!string.IsNullOrEmpty(personQueryCriteria.VornameFilter))
{
query = query.Where(p => p.Vorname == personQueryCriteria.VornameFilter);
}
return query.ToList();
}
我已经明确地包含了导航属性,因为据我所知,EF正在使用延迟加载。
数据库如下所示:
人1 ----- * PersonAdresse * ----- 1 Adresse
模型看起来像这样:
public partial class Person
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Person()
{
this.Adressen = new HashSet<PersonAdresse>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Vorname { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PersonAdresse> Adressen { get; set; }
}
public partial class Adresse
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Adresse()
{
this.Personen = new HashSet<PersonAdresse>();
}
public int Id { get; set; }
public string Strasse { get; set; }
public string Ort { get; set; }
public int Plz { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PersonAdresse> Personen { get; set; }
}
public partial class PersonAdresse
{
public int Id { get; set; }
public int PersonId { get; set; }
public int AdresseId { get; set; }
public virtual Adresse Adresse { get; set; }
public virtual Person Person { get; set; }
}
提前致谢
答案 0 :(得分:1)
这是因为激活延迟加载。请尝试在您正在使用上下文的位置禁用LazyLoadingEnabled,如下所示
this.ContextOptions.LazyLoadingEnabled = false