ASP.NET MVCプロジェクトに取り組んでいるときに、EFの奇妙な動作が見られ、遅延が発生します(正確には、少なくとも1か月はこの問題に悩まされていますが、今になって初めて気づきました。私の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///[キー]publicint 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
パブリッククラス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に定義を割り当てると、DBからフェッチしている定義を割り当てている場合でも、自動的に削除されることです。
上記のコードをテストしました:
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つのアイテム(定義)が含まれていることを確認しました。この行にコメントしているとき、ストアにアイテムはありません。
それをどのように解決できるかについて何かアイデアはありますか?御時間ありがとうございます!