EntityCollection casting problem

Posts   
 
    
Gibby
User
Posts: 7
Joined: 06-Apr-2006
# Posted on: 19-Apr-2006 14:52:54   

Hi,

I am having this problem and wondered if anyone else has experienced the same problem.

If this situation occurs (this is rough doing it from head as i havent got the code to hand, also assume the GetMemberEntityCollections method returns a populated arraylist)

UserEntity u = new UserEntity(); ArrayList al = u.GetMemberEntityCollections(); EntityCollection entityCollection = (EntityCollection) al[0];

entityCollection.Add(new OrderEntity());

//simplified call to createinstance - assume syntax is correct IEntity2 entity = (IEntity2) Activator.CreateInstance(type);

entityCollection.Add(entity);

first call succeeds but the second call results in a casting error within order entity. The error is on this line

**_users = (UserEntity)relatedEntity;**

in the generated function (in OrdersEntity)

private void SetupSyncUsers(IEntity2 relatedEntity)

The casting error something along the lines of

cannot cast UserEntity to UserEntity

Through debugging I have seen that Activator.CreateInstance and the calling a concrete instance of the Orders class invokes the same constructor. Is there a reason this is happening?

Thanks in advance

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 19-Apr-2006 16:21:46   

1- Please post the exception error and the complete stack trace.

2- What's the runtimeLibrary version that you are using?

Gibby
User
Posts: 7
Joined: 06-Apr-2006
# Posted on: 19-Apr-2006 16:49:51   

Im using the .NET 2 runtime libraries.

I would have provided more concise information but I am at work at the moment. I posted in the hope someone had experienced this problem. I will provide a stack trace later along with the actual source. The UserEntity OrderEntity code I pasted was just an example of the scenario. The exception was invalid cast exception - unable to cast namespacexxx.UserEntity to namespacexxx.UserEntity. I will however provide much better information later when I have access to the code.

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 19-Apr-2006 17:16:28   

The exact RuntimeLibrary version can be found as follows: SD.LLBLGen.Pro.ORMSupportClasses.NET11.dll (right click)-> Properties -> Version -> File version

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 20-Apr-2006 11:03:00   

Very strange error.

First of all, don't use the GetMemberCollections etc. methods in your own code if you don't have to, use the collection properties on the entity itself.

Second, the error can only occur if the types aren't equal which means that the types come from different assemblies: assembly A and assembly B have both the same type but as the assemblies differ they aren't really the same. Although I find it strange that this happens with generated code, as you normally have just 1 assembly of that in your app.

Frans Bouma | Lead developer LLBLGen Pro
Gibby
User
Posts: 7
Joined: 06-Apr-2006
# Posted on: 20-Apr-2006 16:30:41   

Half way through that message when I have seen the problem.

I have a class called typefactory, this class has a dictionary of types to simple names of the types. This is a single and is required for the framework I have designed to work. This is in an assembly called framework. The call below is in a different assebly and causes the error.

coll.Add(IEntity2)(Activator.CreateInstance(TypeFactory.Instance.GetType("ScratchingFileEntity"))));

however this call works

coll.Add( (IEntity2)(Activator.CreateInstance(typeof(ScratchingFileEntity))));

Trouble is the collections are only known by simple name (string) at this point.....

Reply I was in the middle of......

OK here is the error. Its not really possible that I use the collections on the entites themselves as the type isnt know until runtime therefore im using standard interfaces like IEntity2 and know no specific information about the entities themselves. This section of the code is generic.

System.InvalidCastException: Unable to cast object of type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity' to type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity'.

Line 980: if(relatedEntity!=null) Line 981: { Line 982: _members = (UserEntity)relatedEntity; Line 983: _members.ActiveContext = base.ActiveContext; Line 984: _members.AfterSave+=new EventHandler(OnEntityAfterSave);

Source File: C:\temp\ST\DatabaseGeneric\EntityClasses\ScratchingFileEntity.cs Line: 982

Stack Trace:

[InvalidCastException: Unable to cast object of type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity' to type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity'.] ScratchTracks.GeneratedCode.EntityClasses.ScratchingFileEntity.SetupSyncMembers(IEntity2 relatedEntity) in C:\temp\ST\DatabaseGeneric\EntityClasses\ScratchingFileEntity.cs:982 ScratchTracks.GeneratedCode.EntityClasses.ScratchingFileEntity.SetRelatedEntity(IEntity2 relatedEntity, String fieldName) in C:\temp\ST\DatabaseGeneric\EntityClasses\ScratchingFileEntity.cs:328 SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2.Add(IEntity2 entityToAdd) +556 ScratchTracks.UI.Web.Controls.DynamicForm.PersistNewObject() in C:\temp\ST\UIWeb\Controls\DynamicForm.cs:155 ScratchTracks.UI.Web.Controls.InputControl.Submit_Click(Object sender, EventArgs e) in C:\temp\ST\UIWeb\Controls\InputControl.cs:322 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3839

source code

        IEntity2 primaryEntity = persistingEntities[this.PersistedObject];
        persistingEntities.Remove(this.PersistedObject);

        UserEntity u = new UserEntity();

        System.Collections.ArrayList al = u.GetMemberEntityCollections();
        IEntityCollection2 coll = (IEntityCollection2)al[1];
        //works
        coll.Add(new ScratchingFileEntity());
        //causes error  coll.Add(IEntity2)(Activator.CreateInstance(TypeFactory.Instance.GetType("ScratchingFileEntity"))));
Gibby
User
Posts: 7
Joined: 06-Apr-2006
# Posted on: 21-Apr-2006 12:22:12   

So this means that I cant have a factory in a seperate assembly to the one that I want to implement this functionality? In my code the factory is in the framework assembly (middle layer ) and the code that calls it is in the UI layer.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 21-Apr-2006 18:09:16   

You can have that, it's that the factory in assembly A has to produce the same instances (thus the same TYPE) as the factory in assembly B. If factory of assembly A produces UserEntity instances of the type UserEntity from assembly A, they're not compatible with UserEntity instances of type UserEntity from assembly B produced by the factory in assembly B.

Frans Bouma | Lead developer LLBLGen Pro