【问题标题】:Entity framework assign automaticly new instance entity as deleted实体框架自动将新的实例实体分配为已删除
【发布时间】:2013-02-08 00:33:49
【问题描述】:

当我在处理 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 公共类 PostAttributeValue { /// /// 属性值id /// [钥匙] 公共 int AttributeValueId { 获取;放; }

    /// <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 公共类 PostAttributeDefinition { /// /// 过滤器名称 /// [钥匙] 公共 int 定义 ID { 获取;放; }

    /// <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; }
}

我的问题是,当我尝试添加新帖子时,我遇到了关系错误 (A relationship from the AssociationSet is in the 'Deleted' state) 这是

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 项 - 定义。当我评论这条线时,商店里没有商品。

您对如何解决有任何想法吗? 感谢您的宝贵时间!

【问题讨论】:

    标签: asp.net-mvc entity-framework


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多