The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Saving a child

Last post 07-29-2008, 1:44 PM by strattonn. 6 replies.
Sort Posts: Previous Next
  •  07-29-2008, 7:39 AM 10407

    Saving a child

    Using

    EntitySpaces Version # 2008.1.0623.0
    MyGeneration Version # 1.3.0.3 

    Why does this code work?

    Code:
                Test1 t1 = new Test1();
                if (t1.LoadByPrimaryKey(1, 1))
                {
                    Test2 t2 = t1.Test2CollectionBySourceId.AddNew();
                    t1.Save();
                }
    
    But this does not?
    Code:
                Test1 t1a = new Test1();
                t1a.AddNew();
                t1a.ItemId = 1;
                t1a.SourceId = 2;
                Test2 t2a = t1a.Test2CollectionBySourceId.AddNew();
                t1a.Save();
    

    t1a.Save() tells me

    Cannot insert the value NULL into column 'SourceId', table 'SentryFile.dbo.Test2'; column does not allow nulls. INSERT fails.

    Am I missing something simple here? If I change the code and add another Save it works but this seems redundant.

    Code:

    ...

    t1a.SourceId = 2;

    t1a.Save();

    Test2 t2a = t1a.Test2CollectionBySourceId.AddNew();

    SQL Definitions

    Code:

    CREATE TABLE [dbo].[Test1](

    [SourceId] [int] NOT NULL,

    [ItemId] [int] NOT NULL,

    CONSTRAINT [PK_Test1] PRIMARY KEY CLUSTERED

    (

    [SourceId] ASC,

    [ItemId] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

     

    CREATE TABLE [dbo].[Test2](

    [SourceId] [int] NOT NULL,

    [ItemId] [int] NOT NULL,

    [Item] [int] IDENTITY(1,1) NOT NULL,

    CONSTRAINT [PK_Test2] PRIMARY KEY CLUSTERED

    (

    [SourceId] ASC,

    [ItemId] ASC,

    [Item] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    ALTER TABLE [dbo].[Test2] WITH CHECK ADD CONSTRAINT [FK_Test2_Test1] FOREIGN KEY([SourceId], [ItemId])

    REFERENCES [dbo].[Test1] ([SourceId], [ItemId])

    GO

    ALTER TABLE [dbo].[Test2] CHECK CONSTRAINT [FK_Test2_Test1]

  •  07-29-2008, 10:57 AM 10411 in reply to 10407

    Re: Saving a child

    You are not missing anything. In fact, I cannot duplicate your error.

    We have NUnit tests for composite foreign keys that are passing. I noticed that your foreign keys are also part of a composite primary key. Thinking, maybe, that was interfering somehow, I ran your SQL in my temp db (SQL Server 2005) to create the tables. Then I generated both tables using ES 2008.1.0623.0 and MyGen 1.3.0.3, taking the template defaults. I added the Custom and Generated classes to my test app, and copied/pasted your code from under "But this does not?".

    I did not change anything. The code ran without error. When I checked in Management Studio, both Test1 and Test2 had one row added, and the row in Test2 had "SourceId" and "ItemId" correctly filled in, along with an auto-generated (identity) "Item".


    David Neal Parsons
    www.entityspaces.net
  •  07-29-2008, 11:12 AM 10412 in reply to 10411

    Re: Saving a child

    What is also unusual is there was a couple of times when the first code did not work. I would debug and watch the properties on the child. When all was working ItemId and SourceId would populate automatically but a few times they did not and the save would fail. I thought I was seeing things because the next time it would work. The failures on the other hand consistently fail.

    For reference I am using SQL Server 2005. I am going to try this in another database and see if it makes any difference.

    Just to double check do I have anything goofy here?

    Code:
      

    <configSections>

    <sectionGroup name="EntitySpaces" type="EntitySpaces.Interfaces.esConfigSettings, EntitySpaces.Core">

    <section name="connectionInfo" type="EntitySpaces.Interfaces.esConfigSettings, EntitySpaces.Interfaces" allowLocation="true" allowDefinition="Everywhere" restartOnExternalChanges="true"/>

    </sectionGroup>

    </configSections>

    <EntitySpaces>

    <connectionInfo default="SQLDynamic">

    <connections>

    <add name="SQLDynamic"

    providerMetadataKey="esDefault"

    sqlAccessType="DynamicSQL"

    provider="EntitySpaces.SqlClientProvider"

    providerClass="DataProvider"

    connectionString="Data Source=192.168.1.26;Initial Catalog=SentryFile;uid=sa;pwd=xxxx"

    databaseVersion="2005"/>

    </connections>

    </connectionInfo>

    </EntitySpaces>

  •  07-29-2008, 11:55 AM 10413 in reply to 10412

    Re: Saving a child

    Except for the connection string, your config looks identical to mine, right down to the name of the connection.

    You do have to be careful if you observe hierarchical properties in the debugger. You can load a Northwind Employees entity, and navigate almost the entire database just by hovering over the instance name, and expanding various hierarchical sub-objects. The debugger will lazy-load whatever you are observing, which can sometimes cause unexpected behavior further down in code. It's similar to a Heisenbug, in which the very act of observing, changes the thing being observed.

    EDIT: "Another reason is that debuggers commonly provide watches or other user interfaces that cause code (such as property accessors) to be executed, which can, in turn, change the state of the program."


    David Neal Parsons
    www.entityspaces.net
  •  07-29-2008, 11:58 AM 10414 in reply to 10411

    Re: Saving a child

    I created a new database on my local machine with a new project and the same code is working for me. Have to work out what is going wrong with the database I am using.
  •  07-29-2008, 12:05 PM 10415 in reply to 10414

    Re: Saving a child

    Interesting. I can't imagine what it might be. Let me know if you turn up anything. Maybe someone else has some ideas that they will share.
    David Neal Parsons
    www.entityspaces.net
  •  07-29-2008, 1:44 PM 10417 in reply to 10415

    Re: Saving a child

    Appears to have been solved by the mysterious power of a reboot! I could save to other databases but not to that specific SQL database. Rebooted and all is fine. I have been hibernating my machine so it has been on for a number of days. Maybe that contributed, who knows?
View as RSS news feed in XML