This is a migrated thread and some comments may be shown as answers.
fk constraint error (guid coming through as empty)
2 Answers 28 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Adam
Top achievements
Rank 1
Adam asked on 08 Oct 2009, 09:27 PM
I have a method like this:

        public void CreateAndAddTagToElement(string tag, string tagType, Guid elementId, float weight) {
            if (Log.IsDebugEnabled) {
                Log.Debug("CreateAndAddTagToElement Start");
            }

            Guid guid = Guid.NewGuid();
            try
            {


                Tag newTag = new Tag();
                newTag.Created = DateTime.Now;
                newTag.CreatedBy = "Madman";
                newTag.TagId = guid;
                newTag.TagType = tagType;

                TagContent tagContent = new TagContent();
                tagContent.TagContentId = Guid.NewGuid();
                tagContent.Tag = newTag;
                tagContent.TagData = tag;
                tagContent.Created = DateTime.Now;
                tagContent.CreatedBy = "Madman";
                tagContent.CultureName = "en";

                using (IObjectScope scope = PhoenixObjectScopeProvider.GetNewObjectScope())
                {

                    scope.Transaction.Begin();
                    scope.Add(newTag);
                    scope.Add(tagContent);
                    scope.Transaction.Commit();


                }


            }
            catch (Telerik.OpenAccess.Exceptions.DataStoreException dse) {
                Log.Error("CreateAndAddTagToElement DataStoreException.", dse);

            }

        }

When this code runs I always get a fk error. Profiling using sql profiler while the code runs shows these statements being executed:

declare @p1 int
set @p1=1
exec sp_prepexec @p1 output,N'@p0 datetime,@p1 nvarchar(255),@p2 uniqueidentifier,@p3 nvarchar(60)',N'INSERT INTO [KBS].[TAG] ([CREATED], [CREATED_BY], [TAG_ID], [TAG_TYPE]) VALUES (@p0, @p1, @p2, @p3)',@p0='2009-10-08 15:14:11.9870000',@p1=N'Madman',@p2='8CA1C327-3BF9-4A5D-96E8-ADF455DF00AF',@p3=N'Group'
select @p1

declare @p1 int
set @p1=2
exec sp_prepexec @p1 output,N'@p0 datetime,@p1 nvarchar(255),@p2 nvarchar(10),@p3 uniqueidentifier,@p4 nvarchar(60),@p5 uniqueidentifier',N'INSERT INTO [KBS].[TAG_CONTENT] ([CREATED], [CREATED_BY], [CULTURE_NAME], [TAG_CONTENT_ID], [TAG_DATA], [TAG_ID]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5)',@p0='2009-10-08 15:14:11.9900000',@p1=N'Madman',@p2=N'en',@p3='398AF7DD-F075-4B9D-95AB-5889F6A09D81',@p4=N'1newtag',@p5='00000000-0000-0000-0000-000000000000'
select @p1

As you can see TAG_ID in the second call is an empty guid, not the guid that was set on the tag in code. What am I doing wrong here?
Thanks,

Adam

2 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 09 Oct 2009, 06:49 AM
Hi Adam,

It obviously has something to do with the relation between the two classes. And by the way. What is the relationship?

I can only see a TagContent.Tag that references the Tag, but is this actually a 1:m relationsship I mean one Tag can have multiple TagContent.. or is this a 1:1 where one TagContent holds a reference to Tag or vice versa?

And the second issue:
How did you define your key generator (what type is it) and what field is the key in Tag (my guess is TagId) and TagContent( again TagContentId is the guess)? Are those key fields actually mapped as keys?

I know there wasn't that many answers in this post, but I need some more info I guess, to give you the correct answers.

Regards

HG



0
Adam
Top achievements
Rank 1
answered on 09 Oct 2009, 02:39 PM
The mapping was a 1:1 mapping. Because of this issue: http://www.telerik.com/community/forums/orm/general-discussions/reverse-engineering-throwing-object-reference-exception.aspx we rebuilt the data model from scratch and now the issue in this discussion is no longer occurring either. Thanks!
Tags
General Discussions
Asked by
Adam
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Adam
Top achievements
Rank 1
Share this question
or