Problem with entity instantiated form XML

Posts   
 
    
juliom
User
Posts: 17
Joined: 13-Jun-2005
# Posted on: 13-Jun-2005 22:40:32   

Hi!!! I'm having a bit of a problem. I'm trying to use the writexml and readxml funtionality and it works fine. The problem is when I interact with the entity instantiated form the XML. What I'm trying to do is to move records form a table in Database A to a table (with the same structure) in Database B in different physical locations. Once I instantiate the entity in the target location I just try to do TheAdapter.SaveEntity(TheSourceEntity) but it does nothing. Any ideas???

juliom
User
Posts: 17
Joined: 13-Jun-2005
# Posted on: 13-Jun-2005 22:41:31   

By the way.. I'm using SQLServer 2000....

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Jun-2005 11:38:30   

When you load an entity from the DB in A, it has nothing changed on it, no fields are modified. So if you would save it on A, nothing would happen as well.

After you've deserialized it on B, do:


foreach(EntityField2 field in entity.Fields)
{
field.IsChanged = true;
}

entity.IsNew = true;
entity.Fields.IsDirty = true;

then save it. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
juliom
User
Posts: 17
Joined: 13-Jun-2005
# Posted on: 14-Jun-2005 20:51:41   

Hi Franz!!!!

Thanks for your help... I had to chage your code a little (had to use a for loop with a counter). Some how the fields collection doesn't seem to be a collection, at least not in the entities I'm using. I don't think this has any thing to do with the fact that I'm using VB instead of C#.... Any ways.. I still have problem... Now I'm getting an error... Perhaps if I explain what I'm trying to do you could help me out a little... I want to be able to persist any entity in this DB ( as long as that entity exists in the DB) using a single web method. Furthermore, I don't want to reference the LLBLGen generated DLL's containing those entities (except for 3 entities that I use to control the whole thing.. I mean they containg some "metadata"). So I'm trying to use reflection.... This is the code I've come up with...

     <WebMethod(Description:="Recieves an IEntity2 Object as an XML String and its type and persits it to the Sicronization Repository")> _
    Public Sub SinchronizeEntity(ByVal x As String, ByVal TheType As String)

        Dim TheDynamicClass As New DC_DYNAMIC_CLASSESEntity

        TheDynamicClass.DC_Type = TheType

        Dim TheAdapter As DataAccessAdapter
        TheAdapter = New DataAccessAdapter("data source=JMURILLO;initial catalog=server;User ID=xxxxxxxxx;Password=xxxxxxxxx;persist security info=False;packet size=4096", True, CatalogNameUsage.ForceName, "CISCRMSinc")

        TheAdapter.FetchEntityUsingUniqueConstraint(TheDynamicClass, TheDynamicClass.ConstructFilterForUCDC_Type())

        Dim AssemblyContainingEntity As [Assembly] = _
            [Assembly].LoadFrom(TheDynamicClass.DC_Location)
        Dim TypeToLoad As Type = AssemblyContainingEntity.GetType(TheDynamicClass.DC_Type)

        Dim TheGenericEntity As Object

        TheGenericEntity = Activator.CreateInstance(TypeToLoad)

        Dim TheSourceEntity As IEntity2 = CType(TheGenericEntity, IEntity2)

        TheSourceEntity.ReadXml(x)

        Dim i As Int16

        For i = 0 To TheSourceEntity.Fields.Count - 1
            TheSourceEntity.Fields(0).IsChanged = True
        Next

        TheSourceEntity.IsNew = True
        TheSourceEntity.Fields.IsDirty = True

        Try
            If TheAdapter.SaveEntity(TheSourceEntity) Then
                TheAdapter.CloseConnection()
            Else
                TheAdapter.CloseConnection()
            End If
        Catch ex As Exception
        End Try

It seems to work fine, but, when I save the entity, I get a Null Reference Exception

The Try Catch block at the end is just to be able to view the exception when I'm debugging.

Any ideas????

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Jun-2005 11:01:10   

juliom wrote:

Hi Franz!!!!

Thanks for your help... I had to chage your code a little (had to use a for loop with a counter). Some how the fields collection doesn't seem to be a collection, at least not in the entities I'm using.

Ah, yes v1.0.2004.1 didn't have an enumerator on the Fields object. An index should be fine though.

Any ways.. I still have problem... Now I'm getting an error... Perhaps if I explain what I'm trying to do you could help me out a little... I want to be able to persist any entity in this DB ( as long as that entity exists in the DB) using a single web method. Furthermore, I don't want to reference the LLBLGen generated DLL's containing those entities (except for 3 entities that I use to control the whole thing.. I mean they containg some "metadata"). So I'm trying to use reflection.... This is the code I've come up with...

     <WebMethod(Description:="Recieves an IEntity2 Object as an XML String and its type and persits it to the Sicronization Repository")> _
    Public Sub SinchronizeEntity(ByVal x As String, ByVal TheType As String)

        Dim TheDynamicClass As New DC_DYNAMIC_CLASSESEntity

        TheDynamicClass.DC_Type = TheType

        Dim TheAdapter As DataAccessAdapter
        TheAdapter = New DataAccessAdapter("data source=JMURILLO;initial catalog=server;User ID=xxxxxx;Password=xxxxxxxx;persist security info=False;packet size=4096", True, CatalogNameUsage.ForceName, "CISCRMSinc")

        TheAdapter.FetchEntityUsingUniqueConstraint(TheDynamicClass, TheDynamicClass.ConstructFilterForUCDC_Type())

        Dim AssemblyContainingEntity As [Assembly] = _
            [Assembly].LoadFrom(TheDynamicClass.DC_Location)
        Dim TypeToLoad As Type = AssemblyContainingEntity.GetType(TheDynamicClass.DC_Type)

        Dim TheGenericEntity As Object

        TheGenericEntity = Activator.CreateInstance(TypeToLoad)

        Dim TheSourceEntity As IEntity2 = CType(TheGenericEntity, IEntity2)

        TheSourceEntity.ReadXml(x)

        Dim i As Int16

        For i = 0 To TheSourceEntity.Fields.Count - 1
            TheSourceEntity.Fields(0).IsChanged = True
        Next

        TheSourceEntity.IsNew = True
        TheSourceEntity.Fields.IsDirty = True

        Try
            If TheAdapter.SaveEntity(TheSourceEntity) Then
                TheAdapter.CloseConnection()
            Else
                TheAdapter.CloseConnection()
            End If
        Catch ex As Exception
        End Try

It seems to work fine, but, when I save the entity, I get a Null Reference Exception

The Try Catch block at the end is just to be able to view the exception when I'm debugging. Any ideas????

In the debugger, can you assure that the entity TheSourceEntity has any fields set as primary key? You can see that by checking TheSourceEntity.Fields.PrimaryKeyFields. It's probably empty.

Frans Bouma | Lead developer LLBLGen Pro
juliom
User
Posts: 17
Joined: 13-Jun-2005
# Posted on: 15-Jun-2005 21:58:55   

Franz!!!

Thanks for your help...

The problem has nothing to do with the Primary Key, thats working fine. The problem was entirely mine. The thing is that I divided the DAL in two separate projects. I created an LLBLGen project containing only the control tables I'm using. The DLL's generated by this project would be referenced in a normal way (no reflection involved here). The other project includes the entities I'm going to sincronize. The DLL's geneated by this second project would be the ones loaded using reflection. The idea is to be able to replace only the requiered DLL's in case of changes (structure changes in the tables being sincronized or new tables to sincronize) and not have to recompile and redeploy the solution. In this scenario I have to create two different DataAdapter objects, one to interact with the control tables and one to interact with the tables being sincronized but... I was using just one...

Thanks a lot for your help...