OK, i've looked at this a bit further.
On the table in the database there is an "INSTEAD OF" Trigger
ALTER TRIGGER [dbo].[trUserInsteadOfIUD] ON [dbo].[tUser]
INSTEAD OF INSERT, UPDATE, DELETE
If I disable this trigger, the entity works correctly and returns the right UserID Every time.
I think this lies at the heart of all the strange problems we've encountered with recursively adding entities because all the tables currently have an instead of trigger.
I've distilled the code to reproduce the error to this.
So the insert code is reduced to this:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var user = new UserEntity();
user.Firstname = "John";
user.Surname = "Smith";
user.Save();
int userId = user.UserId; // Throws invalid cast exception
}
}
}
The trigger has also been cut back to basically nothing:
CREATE TRIGGER [dbo].[trUserInsteadOfIUD] ON [dbo].[tUser]
INSTEAD OF INSERT
AS
-- This correctly inserts the user - since the remainder of the columns are nullable
-- Everything bar the identity column
INSERT INTO tUser
SELECT FirstName, Surname, EmployeeNo, Comment, DefaultAccountId, LCB, LCD
FROM inserted
-- This inserts the identity value and the name into a test table for testing purposes
INSERT INTO tTestUserInsert
SELECT inserted.Firstname + ' ' +inserted.Surname ,SCOPE_IDENTITY()
FROM inserted
GO
I can remove the tTestUserInsert SQL and reproduce the error as well. The tTestUserInsert table grows as one would expect with the correct values.
There are no datatype casting issues in the SQL because the tTestUserInsert table wouldn't fire if there were.
The exception thrown by LLBLGen is this:
System.InvalidCastException was unhandled
Message="Specified cast is not valid."
Source="Lloyds.WMIT.Cristal.Entity"
StackTrace:
at Lloyds.WMIT.Cristal.Entity.EntityClasses.UserEntity.get_UserId() in C:\Development\Cristal\Lloyds.WMIT.Cristal.Entity\EntityClasses\UserEntity.cs:line 1178
at Lloyds.WMIT.Cristal.Entity.EntityClasses.UserEntity.Refetch() in C:\Development\Cristal\Lloyds.WMIT.Cristal.Entity\EntityClasses\UserEntity.cs:line 459
at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.CheckForRefetch() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\EntityBase.cs:line 2066
at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.GetValue(Int32 fieldIndex, Boolean returnDefaultIfNull) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.0\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\SelfServicingSpecific\EntityBase.cs:line 2371
at Lloyds.WMIT.Cristal.Entity.EntityClasses.UserEntity.get_UserId() in C:\Development\Cristal\Lloyds.WMIT.Cristal.Entity\EntityClasses\UserEntity.cs:line 1178
at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\c000580\My Documents\Visual Studio 2008\Projects\UserTests\ConsoleApplication1\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
The build number is no longer available in the about box - though I'm using version 3.0 Final according to the application information. The version of ProLinq Support Classes for .NET 3.5 is 3.0.10.706
If you let me know where to find the DLL that you'd like a version for I'll find it.
We are using SQL 2008, .NET 3.5 SP1.