Why does this object need a DataContext?
This is weird.
regiao C = new regiao();
C.Nome = textBoxNome.Text;
C.Descricao = textBoxDescicao.Text;
C.cidades.AddRange(ListaIN);
mgrRegiao mgr = new mgrRegiao();
mgr.UpdateRegiao(C);
C is a Linq object (code generated by VS2008 in the dbml file) the operation is out of any context. ListaIN in is of type List. That is also the type of C.cidades. The call to mgr.UpdateRegiao creates a context, Copies the contents to objects inside the context and updates the objects. Some of the itens in ListN may have come from a Context (from a query result)
When C.cidades.AddRange(ListaIN)
Executes I get an exception
Cannot access a disposed object.
Object name: 'DataContext accessed after Dispose.'.
at the line
regiao previousValue = this._regiao.Entity;
in the generated code for the entity regiao set opeation.
[Association(Name="regiao_cidade", Storage="_regiao", ThisKey="IDRegiao", IsForeignKey=true)]
public regiao regiao
{
get
{
return this._regiao.Entity;
}
set
{
regiao previousValue = this._regiao.Entity;
if (((previousValue != value)
|| (this._regiao.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
th开发者_JAVA技巧is._regiao.Entity = null;
previousValue.cidades.Remove(this)
...
Whats wrong ? The object C does not have and never had a context. Why it needs a context and how do I solve this ? Is it because some itens in ListaIN came from the database ? If so, is there a way to detach them ?
OK, so that just the way Linq is, i guess.
精彩评论