- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Migration to 2.6 - Save not working
Joined: 02-Jun-2004
I'm using : Migrating LLBLGen Pro 1.0.2004.2 to LLBLGen PRO 2.6 SQL Server 2005 sp3 Adapter C# visual studio 2008
I have a Structure :
Company CompanyID - pk other columns
CompanyProperty CompanyPropertyId pk CompanyID fk other columns
CompanyState CompanyStateId pk CompanyId fk other columns
CompanyAddress CompanyAddressId pk CompanyId fk CompanyPropertyId fk other columns
Using LLBLGen 1.0.2004.2, a companyEntity.Save will save the rows correctly in this order: Company CompanyProperty CompanyState CompanyAddress
I've converted to 2.6 and now I get a fk violation when it saves my trace says it saves: Company then attempts to save CompanyAddress without first saving CompanyProperty
The data looks good in the companyEntity (ie the collections of related entities are populated correctly).
I've read thru the doc but I may have missed something.
Any help would be appreciated. Dave
Joined: 02-Jun-2004
Runtime file version 2.6.8.709 assembly version 2.6.0.0
.NET 3.5 generate for 2.0
Here's a minimum app to recreate the issue.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Fiserv.ASNET.Data.Entities.DatabaseSpecific;
using Fiserv.ASNET.Data.Entities.EntityClasses;
namespace llbgenminapp
{
class Program
{
static void Main(string[] args)
{
CompanyEntity newCompanyEntity = new CompanyEntity();
newCompanyEntity.CompanyId = new Guid();
newCompanyEntity.CompanyAddress.Clear();
newCompanyEntity.CompanyProperty.Clear();
newCompanyEntity.CompanyShortName = "MyCompany";
// add a new companyproperty
CompanyPropertyEntity companyPropertyEntity = new CompanyPropertyEntity();
companyPropertyEntity.CompanyPropertyId = new Guid();
companyPropertyEntity.CompanyId = newCompanyEntity.CompanyId;
companyPropertyEntity.DateLastModification = DateTime.Now;
// add some contact addresses for this company
CompanyAddressEntity companyAddressEntity = new CompanyAddressEntity();
companyAddressEntity.CompanyAddressId = new Guid();
companyAddressEntity.CompanyId = newCompanyEntity.CompanyId;
companyAddressEntity.DateLastModification = DateTime.Now;
companyAddressEntity.CompanyPropertyId = companyPropertyEntity.CompanyPropertyId;
newCompanyEntity.CompanyAddress.Add(companyAddressEntity);
companyAddressEntity.CompanyAddressId = new Guid();
companyAddressEntity.CompanyId = newCompanyEntity.CompanyId;
companyAddressEntity.DateLastModification = DateTime.Now;
companyAddressEntity.CompanyPropertyId = companyPropertyEntity.CompanyPropertyId;
newCompanyEntity.CompanyAddress.Add(companyAddressEntity);
companyAddressEntity.CompanyAddressId = new Guid();
companyAddressEntity.CompanyId = newCompanyEntity.CompanyId;
companyAddressEntity.DateLastModification = DateTime.Now;
companyAddressEntity.CompanyPropertyId = companyPropertyEntity.CompanyPropertyId;
newCompanyEntity.CompanyAddress.Add(companyAddressEntity);
// now save it
using (DataAccessAdapter companyManagementModelDataAdapter = new DataAccessAdapter("Data Source=localhost;Initial Catalog=llblgenMinAppTest;Integrated Security=true"))
{
try
{
companyManagementModelDataAdapter.StartTransaction(IsolationLevel.ReadCommitted, "CompanySave");
try
{
//Save the company changes
companyManagementModelDataAdapter.SaveEntity(newCompanyEntity);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
Database script for tables:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING OFF
GO
CREATE TABLE [dbo].[CompanyAddress](
[CompanyAddressId] [uniqueidentifier] NOT NULL,
[CompanyId] [uniqueidentifier] NOT NULL,
[DateLastModification] [smalldatetime] NOT NULL CONSTRAINT [df_CoAddr_DateLastMod] DEFAULT (getdate()),
[CompanyPropertyId] [uniqueidentifier] NULL,
CONSTRAINT [pkn_CompanyAddress] PRIMARY KEY NONCLUSTERED
(
[CompanyAddressId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[CompanyAddress] WITH CHECK ADD CONSTRAINT [fk_CoAddr_Co] FOREIGN KEY([CompanyId])
REFERENCES [dbo].[Company] ([CompanyId])
GO
ALTER TABLE [dbo].[CompanyAddress] CHECK CONSTRAINT [fk_CoAddr_Co]
GO
ALTER TABLE [dbo].[CompanyAddress] WITH CHECK ADD CONSTRAINT [fk_CoAddr_CompanyProperty] FOREIGN KEY([CompanyPropertyId])
REFERENCES [dbo].[CompanyProperty] ([CompanyPropertyId])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CompanyAddress] CHECK CONSTRAINT [fk_CoAddr_CompanyProperty]
GO
CompanyAddress CompanyAddressId pk CompanyId fk CompanyPropertyId fk other columns
First of all why does CompanyAddress refer to Company? Since it's already refering to CompanyProperty which in turn is refering to Company.
What would make the framework save the CompanyProperty first before Saving the Address, you haven't added this COmpanyAddress entities to the CompanyProperty.CompanyAddress collection. Please do so instead of setting the FK.
Anyway please try using the following code:
CompanyEntity newCompanyEntity = new CompanyEntity();
newCompanyEntity.CompanyId = new Guid();
newCompanyEntity.CompanyAddress.Clear();
newCompanyEntity.CompanyProperty.Clear();
newCompanyEntity.CompanyShortName = "MyCompany";
// add a new companyproperty
CompanyPropertyEntity companyPropertyEntity = new CompanyPropertyEntity();
companyPropertyEntity.CompanyPropertyId = new Guid();
companyPropertyEntity.CompanyId = newCompanyEntity.CompanyId;
companyPropertyEntity.DateLastModification = DateTime.Now;
// add some contact addresses for this company
CompanyAddressEntity companyAddressEntity = new CompanyAddressEntity();
companyAddressEntity.CompanyAddressId = new Guid();
//companyAddressEntity.CompanyId = newCompanyEntity.CompanyId;
companyAddressEntity.DateLastModification = DateTime.Now;
newCompanyEntity.CompanyAddress.Add(companyAddressEntity);
companyPropertyEntity.CompanyAddress.Add(companyAddressEntity);
CompanyAddressEntity companyAddressEntity2 = new CompanyAddressEntity();
companyAddressEntity2.CompanyAddressId = new Guid();
companyAddressEntity2.DateLastModification = DateTime.Now;
newCompanyEntity.CompanyAddress.Add(companyAddressEntity2);
companyPropertyEntity.CompanyAddress.Add(companyAddressEntity2);
CompanyAddressEntity companyAddressEntity3 = new CompanyAddressEntity();
companyAddressEntity3.CompanyAddressId = new Guid();
companyAddressEntity3.DateLastModification = DateTime.Now;
newCompanyEntity.CompanyAddress.Add(companyAddressEntity3);
companyPropertyEntity.CompanyAddress.Add(companyAddressEntity3);
// now save it
using (DataAccessAdapter companyManagementModelDataAdapter = new DataAccessAdapter("Data Source=localhost;Initial Catalog=llblgenMinAppTest;Integrated Security=true"))
{
try
{
companyManagementModelDataAdapter.StartTransaction(IsolationLevel.ReadCommitted, "CompanySave");
try
{
//Save the company changes
companyManagementModelDataAdapter.SaveEntity(newCompanyEntity, true, true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Joined: 02-Jun-2004
First of all why does CompanyAddress refer to Company? Since it's already refering to CompanyProperty which in turn is refering to Company.
We are investigating this -- I raised the same question.
Ok - I got my min app to work with the following changes - first, I needed to use Guid.NewGuid() actually set a value for the keys.
I wasn't adding the CompanyPropertyEntity to the companyEntity.CompanyProperty collection. Also, I used your change to add the addresses to the property.address collection.
So, I'll go back to my real code with this info to see if I can make it work. Thanks for your help.
Dave