Many-to-many confusion

Posts   
 
    
csmac3144
User
Posts: 74
Joined: 12-Sep-2007
# Posted on: 22-Sep-2008 23:35:44   

I am having a problem with many to many.

I had the same problem before, but I fixed it by toggling relations back on which I had previously hidden.

I created a very simple project from scratch and I STILL can't get many-to-many to work, even when I basically copy the sample code out of the help file.

What can possibly be wrong with this?

Error on this line: adapter.SaveEntity(surveyPackage, true);

An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'PackageID', table 'Scratch.dbo.SurveyPackage'; column does not allow nulls. INSERT fails. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

class Program
{
    static void Main(string[] args)
    {
        var adapter = new DataAccessAdapter();
        var survey = new SurveyEntity(new Guid("bf04c246-fe88-dd11-941d-005056c00008"));
        adapter.FetchEntity(survey);
        var package = new PackageEntity { Name = "Test" };
        var surveyPackage = new SurveyPackageEntity {Survey = survey, Package = package};
        adapter.SaveEntity(surveyPackage, true);
    }
}

Database:

CREATE TABLE [dbo].[Package]( [PackageID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Package_PackageID] DEFAULT (newsequentialid()), [Name] nchar NOT NULL, CONSTRAINT [PK_Package] PRIMARY KEY CLUSTERED ( [PackageID] 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 /****** Object: Table [dbo].[Survey] Script Date: 09/22/2008 19:43:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Survey]( [SurveyID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Survey_SurveyID] DEFAULT (newsequentialid()), [Name] nchar NOT NULL, CONSTRAINT [PK_Survey] PRIMARY KEY CLUSTERED ( [SurveyID] 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 /****** Object: Table [dbo].[SurveyPackage] Script Date: 09/22/2008 19:43:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[SurveyPackage]( [SurveyPackageID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_SurveyPackage_SurveyPackageID] DEFAULT (newsequentialid()), [SurveyID] [uniqueidentifier] NOT NULL, [PackageID] [uniqueidentifier] NOT NULL, CONSTRAINT [PK_SurveyPackage] PRIMARY KEY CLUSTERED ( [SurveyPackageID] 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].[SurveyPackage] WITH CHECK ADD CONSTRAINT [FK_SurveyPackage_Package] FOREIGN KEY([PackageID]) REFERENCES [dbo].[Package] ([PackageID]) GO ALTER TABLE [dbo].[SurveyPackage] CHECK CONSTRAINT [FK_SurveyPackage_Package] GO ALTER TABLE [dbo].[SurveyPackage] WITH CHECK ADD CONSTRAINT [FK_SurveyPackage_Survey] FOREIGN KEY([SurveyID]) REFERENCES [dbo].[Survey] ([SurveyID]) GO ALTER TABLE [dbo].[SurveyPackage] CHECK CONSTRAINT [FK_SurveyPackage_Survey]

csmac3144
User
Posts: 74
Joined: 12-Sep-2007
# Posted on: 23-Sep-2008 02:07:34   

Here is the project.

(Attachment removed as it isn't relevant -- Otis)

csmac3144
User
Posts: 74
Joined: 12-Sep-2007
# Posted on: 23-Sep-2008 05:21:45   

OH MAN!

It was this thing again:

<add key="SqlServerDQECompatibilityLevel" value="2" />

Frans, it might help to point this out more clearly. I had this same issue a few weeks ago, but in the meantime was doing a completely seperate non-LLBL project and it slipped my mind.

If you leave that out there is lots of very strange behavior with no real indicator of where to look for a solution.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 23-Sep-2008 10:10:01   

csmac3144 wrote:

OH MAN!

It was this thing again:

<add key="SqlServerDQECompatibilityLevel" value="2" />

Frans, it might help to point this out more clearly. I had this same issue a few weeks ago, but in the meantime was doing a completely seperate non-LLBL project and it slipped my mind.

If you leave that out there is lots of very strange behavior with no real indicator of where to look for a solution.

That setting has no real influence on saving data EXCEPT if you use NEWSEQUENTIALID() pk value sequences, and that's what you use. So having the default of '1', it will ignore GUIDs without a value set and no PK value is retrieved back, leading to crashing save routines.

The setting is documented in several places, but indeed it could be overlooked.

In v3 I'll make the default '2', so this will go away. simple_smile If you have problems with this setting, please alter the app.config template by creating a copy, binding it to the same templateid in a new templatebindings file and use that instead.

Frans Bouma | Lead developer LLBLGen Pro