1

当我在做一个 ASP.NET MVC 项目时,我看到了一个奇怪的 EF 行为,这让我延迟了(确切地说,我至少在一个月内仍然被这个问题困住......直到现在我才意识到我的 DDD 架构代码没有损坏,它是我所拥有的与 EF 相关的代码错误)。

我的网站有帖子。每个帖子都有一组属性 ( PostAttributeValue),每个属性值都有一个相关的PostAttributeDefinition,其中包含有关它的数据 - 例如标题、验证规则、原始值(二进制序列化)、数据类型等。

这是我的帖子模型:

public class Post
{
    #region Settings

    /// <summary>
    /// Maximum linked images
    /// </summary>
    public static int MaximumLinkedImages
    {
        get { return Properties.Settings.Default.MaximumPostsLinkedImages; }
    }

    /// <summary>
    /// Maximum linked image size in MB
    /// </summary>
    public static int MaximumLinkedImageSize
    {
        get { return Properties.Settings.Default.MaximumPostLinkedImageSize; }
    }

    /// <summary>
    /// Delay hours between posts bumping
    /// </summary>
    public static int BumpPostDelayHours
    {
        get { return Properties.Settings.Default.BumpPostDelayHours; }
    }

    #endregion

    #region ctor

    public Post()
    {
        this.Attributes = new List<PostAttributeValue>();
        this.LinkedImages = new List<string>();
    }

    #endregion

    /// <summary>
    /// The parent category that this post was posted into
    /// </summary>
    [Required]
    public virtual Category ParentCategory { get; set; }

    /// <summary>
    /// The post unique identifier
    /// </summary>
    [Key]
    public Guid PostIdentifier { get; set; }

    /// <summary>
    /// The post title (e.g. "Great vila!")
    /// </summary>
    [Required]
    public string Title { get; set; }

    /// <summary>
    /// The post title url alias (e.g. "great-vila")
    /// </summary>
    public string TitleUrlAlias { get; set; }

    /// <summary>
    /// Post extra notes and information written by the author
    /// </summary>
    [Required]
    public string Description { get; set; }

    /// <summary>
    /// The post item city
    /// </summary>
    [Required]
    public virtual City City { get; set; }

    /// <summary>
    /// The post item location
    /// </summary>
    public string Location { get; set; }

    /// <summary>
    /// Is the post was published and marketed by brokerage (Tivuuch)
    /// </summary>
    [Required]
    public bool Brokerage { get; set; }

    /// <summary>
    /// Post custom attributes
    /// </summary>
    public virtual ICollection<PostAttributeValue> Attributes { get; set; }

    /// <summary>
    /// The post assigned price
    /// </summary>
    [Required]
    public int RequestedPrice { get; set; }

    /// <summary>
    /// List of images linked with the post (includes only the name of the picture, a.k.a "foo.png", "bar.jpg" etc.)
    /// </summary>
    public virtual ICollection<string> LinkedImages { get; set; }

    public string LinkedImagesSerialized
    {
        get
        {
            if (this.LinkedImages == null)
            {
                this.LinkedImages = new List<string>();
            }

            return string.Join(",", this.LinkedImages);
        }
        set
        {
            if (this.LinkedImages == null)
            {
                this.LinkedImages = new List<string>();
            }

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            this.LinkedImages = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }
    }

    /// <summary>
    /// Cached generated cached url using IShorterUrlService
    /// </summary>
    public string GeneratedShorterUrl { get; set; }

    /// <summary>
    /// Is this post marked as hot
    /// </summary>
    public bool IsHotPost { get; set; }

    /// <summary>
    /// The post publish status
    /// </summary>
    public PostPublishStatus PublishStatus { get; set; }

    /// <summary>
    /// The post author
    /// </summary>
    public virtual Account Author { get; set; }

    /// <summary>
    /// The author IP address (collected to determine different IPs)
    /// </summary>
    public string AuthorIPAddress { get; set; }

    /// <summary>
    /// The creation date of the post
    /// </summary>
    public DateTime CreationDate { get; set; }

    /// <summary>
    /// The last post modification date
    /// </summary>
    public DateTime LastUpdatedDate { get; set; }

    /// <summary>
    /// The date that the post was bumped at, used to sort the posts in category.
    /// </summary>
    public DateTime LastBumpDate { get; set; }
}

这是PostAttributeValue public class PostAttributeValue { /// /// 属性值 id /// [Key] public int AttributeValueId { get; 放; }

    /// <summary>
    /// The value owner post
    /// </summary>
    public virtual Post OwnerPost { get; set; }

    /// <summary>
    /// The value attribute definition id
    /// </summary>
    //public int RelatedAttributeDefinitionId { get; set; }

    /// <summary>
    /// The value attribute definition
    /// </summary>
    public virtual PostAttributeDefinition Definition { get; set; }

    /// <summary>
    /// The stored raw value
    /// </summary>
    public byte[] RawValue { get; set; }
}

这是PostAttributeDefinition public class PostAttributeDefinition { /// /// 过滤器名称 /// [Key] public int DefinitionId { get; 放; }

    /// <summary>
    /// The owner category
    /// </summary>
    [Required]
    public virtual Category OwnerCategory { get; set; }

    /// <summary>
    /// The filter title
    /// </summary>
    [Required]
    public string Title { get; set; }

    /// <summary>
    /// Metadata enum that provides extra data about the data type
    /// </summary>
    public PostAttributeTypeMetadata TypeMetadata { get; set; }

    /// <summary>
    /// Bitwise metadata that provides data about the object in display mode
    /// </summary>
    public PostAttributeDisplayModeMetadata DisplayModeMetadata { get; set; }

    public PostAttributeEditorType EditorType { get; set; }

    /// <summary>
    /// The attribute raw default value
    /// </summary>
    [Required]
    public byte[] RawDataValue { get; set; }

    /// <summary>
    /// The attribute raw associated validation attributes
    /// </summary>
    public byte[] RawValidationRules { get; set; }

    /// <summary>
    /// Is this field required
    /// </summary>
   public bool IsRequired { get; set; }
}

我的问题是,当我尝试添加新帖子时,我遇到了关系错误(来自 AssociationSet 的关系处于“已删除”状态),即

A relationship from the 'PostAttributeValue_Definition' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'PostAttributeValue_Definition_Source' must also in the 'Deleted' state.

现在,我发现问题在于,当我为 PostAttributeValue 分配一个定义时,它会自动变为已删除 - 即使我正在分配一个我现在从数据库中获取的定义。

我已经测试了上面的代码:

        var db = ServiceLocator.SharedInstance.GetInstance<MyDbContext>();
        List<PostAttributeValue> v = new List<PostAttributeValue>();

        var entity =  db.PostAttributesDefinitions.Where(d => d.DefinitionId==1).Include(d => d.OwnerCategory).First();
        v.Add(new PostAttributeValue() { Definition = entity });

        Post post = new Post()
        {
            Title = "foo",
            Description = "bar",
            City = new City { },
            Brokerage = false,
            Location = "",
            RequestedPrice = 500,
            ParentCategory = new Category() { },
            AuthorIPAddress = "",
            Attributes = v
        };

        db.Posts.Add(post);

        var deletedValuesStore = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db)

.ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted);

并看到 deletedValuesStore 包含 1 项 - 定义。当我评论这条线时,商店里没有商品。

你有什么想法可以解决吗?谢谢你的时间!

4

1 回答 1

1

好的,所以我想通了。以防万一有人感兴趣,我错误地将我的 DbContext 配置为Required().WithOptional() 关系而不是一对多-Required().WithMany()。因此,当我将已经分配给一个值的现有属性定义分配给一个新值时,它会自动将其标记为已删除。

于 2013-02-08T15:52:25.243 回答