fk constraint error (guid coming through as empty)

Thread is closed for posting
3 posts, 0 answers
  1. Adam
    Adam  avatar
    30 posts
    Member since:
    Jun 2009

    Posted 08 Oct 2009 Link to this post

    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. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 09 Oct 2009 Link to this post

    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



  3. Adam
    Adam  avatar
    30 posts
    Member since:
    Jun 2009

    Posted 09 Oct 2009 Link to this post

    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!
Back to Top