Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Erwin 22 posts 43 karma points
    May 21, 2012 @ 15:03
    Erwin
    0

    update content throws exception The INSERT statement conflicted with the FOREIGN KEY constraint

    hello!

    i'm trying to update content programmaticly in Umbraco 5.01 but i get the folowing exceptions when hive trys to flush the data to the database : 

    The INSERT statement conflicted with the FOREIGN KEY constraint "U5_FK_Attribute_AttributeLongStringValues". The conflict occurred in database "umbSchouwburgAmstelveen_umb5", table "dbo.Attribute", column 'Id'.
    The statement has been terminated.

    The werd part is that i use simmeler code in a nother project where i don't have the problem. ther the document type is less complex tho.

    here is my code:

    protected virtual void AddOrUpdate(IEnumerable<TType> items, string parentId, RevisionStatusType revisionStatus)
            {
                var documentType = DocumentType;
                using(var writer = Context.Hive.OpenWriter<IContentStore>())
                {
                    var contentRepository = new List<ContentEditorModel>();
                    foreach (var item in items)
                    {
                        ContentEditorModel model = null;
                        if (item.ID != null)// && writer.Repositories.Exists<TypedEntity>(new HiveId(item.ID)))
                        {
                            var entity = writer.Repositories.Revisions.GetLatestSnapshot<TypedEntity>(new HiveId(item.ID));
                            entity.Revision.Item.EntitySchema =
                                writer.Repositories.Schemas.GetComposite<EntitySchema>(entity.Revision.Item.EntitySchema.Id);
                            model = Context.FrameworkContext.TypeMappers
                                .Map<EntitySnapshot<TypedEntity>, ContentEditorModel>(entity);
                        }
                        else
                        {
                            model = new ContentEditorModel();
                            model.DocumentTypeAlias = DocumentTypeAlias;
                            model.DocumentTypeId = documentType.Id;
                            model.DocumentTypeName = documentType.Name;
                            model.Id = new HiveId(Guid.NewGuid());
                            if (documentType.DefaultTemplateId != null)
                            {
                                SetProperty(model, SelectedTemplateAttributeDefinition.AliasValue,
                                            new Dictionary<string, object>()
                                            {
                                                {"TemplateId", DocumentType.DefaultTemplateId.ToString()}
                                            });
                            }
                        }
                        if(!string.IsNullOrEmpty(parentId))
                            model.ParentId = new HiveId(parentId);
                        UpdateContent(model, item);
                        contentRepository.Add(model);
                        item.ID = model.Id.ToString();
                    }
                    //map model with Revision<TypedEntity>
                    var mappedCollection =
                        Context.FrameworkContext.TypeMappers.Map
                            <IEnumerable<ContentEditorModel>, IEnumerable<Revision<TypedEntity>>>(contentRepository);
                    if(revisionStatus != null)
                        mappedCollection.ForEach(x => x.MetaData.StatusType = revisionStatus);
                    //add or update model
                    writer.Repositories.Revisions.AddOrUpdate(mappedCollection);
                    writer.Complete();
    protected void SetProperty(ContentEditorModel model, string propertyAlias, object propertyValue)
            {
                propertyValue = propertyValue ?? new Dictionary<string, object>();
    
                //TODO: fix hack with datetime when bug fixed with datetime in umbraco
                if(propertyValue is DateTime && ((DateTime)propertyValue) == DateTime.MinValue)
                {
                    propertyValue = new DateTime(1900, 3, 4, 5, 6, 7);
                }
    
                var docTypeProperty = DocumentType.Properties.Single(x => x.Alias == propertyAlias);
    
                //NOTE: If the Umbraco API changes (an extra overload is added to new ContentProperty), this might break
                var dictionary = propertyValue as IDictionary<string, object> ?? new Dictionary<string, object> { { "Value", propertyValue } };
    
                var property = model.Properties.FirstOrDefault(o => o.Alias == propertyAlias);
                if (property != null)
                {
                    property.PropertyEditorModel.SetModelValues(dictionary);
                }
                else
                {
                    property = new ContentProperty(docTypeProperty.Id, docTypeProperty, dictionary)
                    {
                        Alias = propertyAlias,
                        TabId = DocumentType.DefinedTabs.Single(x => x.Alias == FixedGroupDefinitions.GeneralGroup.Alias).Id
                    };
                    model.Properties.Add(property);
                }
            }

    I hope any1 can help me with this i have had the problem for some time

Please Sign in or register to post replies

Write your reply to:

Draft