私は子エンティティが付加された単純なオブジェクトを持っています。 デタッチされたオブジェクトを更新すると、その子オブジェクトのプロパティは保存されません。私はこのフォームで多くの投稿を読んでいますが、なぜ更新していないのか分かりません。分離されたエンティティはプロパティを更新しません
ここでエンティティを更新するための内部メソッドを参照してください:
public class HtmlContent : ITextContentItem, ICreateStamp, IEditStamp, IImmutable
{
// ReSharper disable once UnusedMember.Local
private HtmlContent()
{
}
public HtmlContent(string name, string description, string text, DateTime creationDate,
DateTime? lastEditDate, ApplicationUser createdBy, ApplicationUser lastEditedBy)
{
this.Name = name;
this.Description = description;
this.Text = text;
this.CreationDate = creationDate;
this.LastEditDate = lastEditDate;
this.CreatedBy = createdBy;
this.LastEditedBy = lastEditedBy;
}
public HtmlContent(int id, string name, string description, string text, DateTime creationDate,
DateTime? lastEditDate, ApplicationUser createdBy, ApplicationUser lastEditedBy)
: this(name, description, text, creationDate, lastEditDate, createdBy, lastEditedBy)
{
this.Id = id;
}
public int Id { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
public string Text { get; private set; }
public DateTime CreationDate { get; private set; }
public DateTime? LastEditDate { get; private set; }
public ApplicationUser CreatedBy { get; private set; }
public ApplicationUser LastEditedBy { get; private set; }
internal HtmlContent SetLastEditInfo(DateTime? lastEditDate, ApplicationUser lastEditedBy)
{
if ((lastEditDate.HasValue && lastEditedBy == null) ||
(!lastEditDate.HasValue && lastEditedBy != null))
{
throw new InvalidOperationException($"{nameof(lastEditDate)} and {nameof(lastEditedBy)} must be used together");
}
return new HtmlContent(this.Id, this.Name, this.Description, this.Text, this.CreationDate, lastEditDate, this.CreatedBy, lastEditedBy);
}
internal HtmlContent UpdateHtmlContent(string name, string description, string text)
{
return new HtmlContent(this.Id, name, description, text, this.CreationDate, this.LastEditDate, this.CreatedBy, this.LastEditedBy);
}
}
は、ここで更新方法を参照してください。
public async Task Edit(int id, string name, string description, string text)
{
try
{
var content = await this.WithId(id);
this.db.Entry(content).State = EntityState.Detached;
var currentDate = DateTime.UtcNow;
var lastEditedBy = this.userProvider.GetCurrentUser();
content = content.SetLastEditInfo(currentDate, lastEditedBy);
content = content.UpdateHtmlContent(name, description, text);
this.db.Entry(content).State = EntityState.Modified;
await this.db.SaveChangesAsync();
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
var errors = ex.EntityValidationErrors;
throw;
}
}
他のすべてのプロパティがうまく更新されます。 LastEditedBy
のみが更新されません。 CreateメソッドでCreatedBy
が正しく動作しているのは、データベースに保存されている新しいエンティティなのでです。 ApplicationUser
プロパティには、コードで最初に生成されたデータベースに外部キーがあります。
理由#4に私の賭けを入れました。 –
まず、反応に感謝します。ほぼ完全なモデルで質問を更新しました。属性のような必要な情報を削除しました。私は 'プライベート'のコンストラクタを持っています。プライベートセットの理由でプロパティを作成した理由は、オブジェクトの外側から値を設定できないためです。オブジェクトは、それがどのように維持され、他の何らかの表面ではなく、どのように維持されるかについての論理を有していなければならない1つのクラスで変更できる場合は、複数のクラスに変更できます。その後、コードの複製がコーナーにあります。私の主体は切り離されているとも言いました。私は追跡をしたくないからです。 –
私は問題の解答を追加します。オブジェクトはオブジェクト自体の外側では「不変」です。正しい方向に私を指摘してくれてありがとう。 –