Search   

Friday, February 10, 2012

Support » Support Forum

Register  Login

 Menu  
 
    

 Welcome!  
 


Welcome to the NDO support forum!

kreis.gif Please register/login at the site if you want to post a thread. We sometimes copy support mails of common interest to the forum. They appear with anonymous sender.
kreis.gif Please post your messages in English language if possible. You can do your posts also in German, we will translate your post and our answer so that all users of the forum can read your posts.
kreis.gif The forum software requires that javascript is enabled.
kreis.gif Please do not insert licence information or licence keys in forum posts.
kreis.gif If you know a good answer to a post of another user, feel free to reply!
kreis.gif The forum is monitored. We reserve the right to remove posts we consider not to be useful for other forum users.

   Print   

      
 NDO Support Forum  
 
SearchForum Home
  NDO-Forum postings in english please, if possible.  Developing with NDO  Mystic Exceptio...
 Mystic Exception when persisting
 
 3/26/2007 10:06:33 AM
schoetbi
15 posts


Mystic Exception when persisting
Hello,

i just try to persist a collection class for parameters called parameterGroup. But allways get an exception when persisting.

Parametergroup has a composite relation to parameters

[NDORelation(RelationInfo.Composite)]
private List lstParameter = new List();


TestCode:

pm.BuildDatabase("Test.ndo.sql"); // this is done correctly

// add this parameter to a ParameterGroup
ParameterGroup pg = new ParameterGroup();
pg.Id = parameterGroupId;
pg.Name = parameterGroupName;


Parameter p = new Parameter(parameterName);
p.Id = parameterId;
p.Value = parameterValue;
pg.AddParameter(p); // works fine as well

pm.MakePersistent(pg); // throws a AccessViolationException
pm.Save();


StackTrace:
mscorlib.dll!System.Collections.Hashtable.GetHash(object key) + 0x19 bytes
mscorlib.dll!System.Collections.Hashtable.ContainsKey(object key = {neb.Common.NDO.Parameter}) + 0x44 bytes
mscorlib.dll!System.Collections.Hashtable.Contains(object key) + 0x5 bytes
NDO.dll!NDO.g.c(NDO.IPersistenceCapable A_0 = {neb.Common.NDO.Parameter}) + 0x2b bytes
NDO.dll!NDO.PersistenceManager.InternalAddRelatedObject(NDO.IPersistenceCapable pc = {neb.Common.NDO.ParameterGroup}, NDO.Mapping.Relation r = {Relation for field lstParameter of class neb.Common.NDO.ParameterGroup:
Type: System.Collections.Generic.List`1[neb.Common.NDO.Parameter] [List] RelationType: Composition, Directed to class neb.Common.NDO.Parameter}, NDO.IPersistenceCapable relObj = {neb.Common.NDO.Parameter}, bool isMerging = false) + 0x71 bytes
NDO.dll!NDO.PersistenceManager.AddRelatedObject(NDO.IPersistenceCapable pc = {neb.Common.NDO.ParameterGroup}, NDO.Mapping.Relation r = {Relation for field lstParameter of class neb.Common.NDO.ParameterGroup:
Type: System.Collections.Generic.List`1[neb.Common.NDO.Parameter] [List] RelationType: Composition, Directed to class neb.Common.NDO.Parameter}, NDO.IPersistenceCapable relObj = {neb.Common.NDO.Parameter}) + 0x478 bytes
NDO.dll!NDO.PersistenceManager.e(NDO.IPersistenceCapable A_0 = {neb.Common.NDO.ParameterGroup}, bool A_1 = true) + 0x790 bytes
NDO.dll!NDO.PersistenceManager.MakePersistent(object o = {neb.Common.NDO.ParameterGroup}) + 0xf0 bytes
> neb.CommonTest.EXE!neb.CommonTest.Parameters.ParameterGroupTest.MakePersistent() Line 174 + 0xd bytes C#


Maybe I do something fundamental wrong, but its very hard to debug this. Maybe it helps to have the mapping information also:
<Class AssemblyName="neb.Common.NDO.Parameters" FullName="neb.Common.NDO.ParameterGroup" TableName="ParameterGroup" ConnectionId="C0">
      <Oid ColumnName="Id" FieldName="id" />
      <Fields>
        <Field Name="id" ColumnName="Id" />
        <Field Name="lastChanged" ColumnName="LastChanged" />
      </Fields>
      <Relations>
        <Relation FieldName="lstParameter" ForeignKeyColumnName="IDParameterGroup" ReferencedTypeName="neb.Common.NDO.Parameter" RelationName="" />
      </Relations>
    </Class>
<Class AssemblyName="neb.Common.NDO.Parameters" FullName="neb.Common.NDO.Parameter" TableName="Parameter" ConnectionId="C0">
      <Oid ColumnName="Id" FieldName="id" />
      <Fields>
        <Field Name="description" ColumnName="Description" />
        <Field Name="id" ColumnName="Id" />
        <Field Name="name" ColumnName="Name" />
      </Fields>
      <Relations>
        <Relation FieldName="value" ForeignKeyColumnName="IDMultiValue" ReferencedTypeName="neb.Common.NDO.MultiValue" RelationName="" />
      </Relations>
    </Class>

Thank you for your help.
Regards,
Tobias
 3/26/2007 10:30:00 AM
schoetbi
15 posts


Re: Mystic Exception when persisting
Maybe it also helps to say that the same exception also occours when calling parameter1.GetHashCode. I implement IEqualityComparer

Parameter:
 public override int GetHashCode()
        {
            int hc = 0;
           
            if(this.id != null)
                hc ^= this.id.GetHashCode();
            if(this.name != null)
                hc ^= this.name.GetHashCode();           

            return hc;
        }

        public int GetHashCode(object obj)
        {
            Parameter p = (Parameter)obj;
            return p.GetHashCode();
        }


ParameterGroup:

        public override int GetHashCode()
        {
            RebuildDictionaryIfNeccessary();

            int hc = this.childs.GetHashCode();
            hc ^= this.lastChanged.GetHashCode();
            hc ^= this.name.GetHashCode();
            hc ^= this.dictParametersByName.GetHashCode();
            hc ^= this.dictParametersById.GetHashCode();
            hc ^= this.ParentGroup.GetHashCode();
           
            return hc;
        }

        public int GetHashCode(object obj)
        {
            ParameterGroup pg = (ParameterGroup)obj;
            return pg.GetHashCode();
        }

Regards,
Tobias
 3/26/2007 10:57:06 AM
Mirko
109 posts
5th


Re: Mystic Exception when persisting

Hi Tobias,

the call stack information points you exactly to the code causing the exception. So all you have to do is setting a breakpoint into the GetHashCode methods and step through until the exception occurs. If VS refuses to debug your assembly just make a little change to the source file (inserting a blank and deleting it) and immediately hit F5. This makes VS remember that it is able to debug the business classes code.

BTW: Why do you generate the Guids of your objects by yourself? If you have good reasons to do that, I'd recommend to use the IdGenerationEvent of the pm:

void OnIdGenerationEvent(Type t, ObjectId oid)
{
   Guid g = GetNewGuidForType(t);
   ((GuidKey)oid.Id).Key = g;
}

That you don't have to insert the id field. If you need access to the id, just use NDOObjectId.Id.Value.

As for your GetHashCode method also note, that Guid is a value type, so it will never have the value "null". An uninitialized Guid always has the value Guid.Null.

I'd recommend further not to include the id into your HashCode algorithm because the HashCode of your object changes if your object is getting persistent. That might cause all kinds of trouble. Or you make sure, that your objects always have an id when the Hash Code is computed: Debug.Assert(id != Guid.Null).

Best Regards
Mirko

 

 3/26/2007 10:57:18 AM
Mirko
109 posts
5th


Re: Mystic Exception when persisting
 Modified By Mirko  on 3/26/2007 10:58:58 AM)

Hi Tobias,

the call stack information points you exactly to the code causing the exception. So all you have to do is setting a breakpoint into the GetHashCode methods and step through until the exception occurs. If VS refuses to debug your assembly just make a little change to the source file (inserting a character and delete it) and immediately hit F5. This makes VS remember that it is able to debug the business classes code.

BTW: Why do you generate the Guids of your objects by yourself? If you have good reasons to do that, I'd recommend to use the IdGenerationEvent of the pm:

void OnIdGenerationEvent(Type t, ObjectId oid)
{
   Guid g = GetNewGuidForType(t);
   ((GuidKey)oid.Id).Key = g;
}

That you don't have to insert the id field. If you need access to the id, just use NDOObjectId.Id.Value.

As for your GetHashCode method also note, that Guid is a value type, so it will never have the value "null". An uninitialized Guid always has the value Guid.Null.

I'd recommend further not to include the id into your HashCode algorithm because the HashCode of your object changes if your object is getting persistent. That might cause all kinds of trouble. Or you make sure, that your objects always have an id when the Hash Code is computed: Debug.Assert(id != Guid.Null).

Best Regards
Mirko

 

 3/26/2007 2:10:54 PM
schoetbi
15 posts


Re: Mystic Exception when persisting
Hi Mirko,

thak you for your hints I indeed had the problem that I couldn't debug the GetHashcode function. I changed it pressed F5 but also debugging was not possible. So I removed severeal ^= statements inside the hashcode and found the one that inhibits debuging it was:

// doesn't work:
public override int GetHashCode()
{
int hc = 0;
//if(childs != null)
// hc ^= this.childs.GetHashCode();
if(lastChanged != null)
hc ^= this.lastChanged.GetHashCode();
if(name != null)
hc ^= this.name.GetHashCode();
if(lstParameter !=null)
hc ^= this.lstParameter.GetHashCode();
if (ParentGroup != null)
hc ^= this.ParentGroup.Id.GetHashCode();

return hc;
}

this code works if I remove
if(lastChanged != null)
hc ^= this.lastChanged.GetHashCode();

wich is a DateTime field: private DateTime lastChanged;

What could this be?

Regards,
Tobias
  NDO-Forum postings in english please, if possible.  Developing with NDO  Mystic Exceptio...
  SearchSearch  Forum HomeForum Home