하위 엔티티가 첨부 된 간단한 개체가 있습니다. 분리 된 개체를 업데이트 할 때 자식 개체가있는 속성이 저장되지 않습니다. 이 양식에 많은 게시물을 읽었지 만 왜 업데이트되지 않는지 알 수 없습니다.분리 된 엔티티는 속성을 업데이트하지 않습니다.
엔티티를 업데이트 여기를 내부 방법을 참조하십시오
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에 내 내기를 넣어 것입니다. –
먼저 반응에 감사드립니다. 거의 완전한 모델로 질문을 업데이트했습니다. 나는 속성과 같은 필요한 정보를 제거하지 않았습니다. 나는'private' 인 생성자를 가지고 있습니다.개인 집합을 사용하는 이유에 대한 속성을 만들었습니다. 그 이유는 개체 외부에서 값을 설정할 수 없기 때문입니다. 객체는 유지 보수 방법에 대한 논리를 가져야하며 다른 일부 표면은 유지 보수하지 말아야합니다. 한 클래스에서 변경할 수 있다면 여러 클래스로 변경할 수 있습니다. 그 후, 코드 복제가 시작됩니다. 나는 또한 나의 실체가 분리되었다고 말했다. 추적을 원하지 않기 때문입니다. –
문제에 대한 해답을 추가합니다. 이제 객체는 객체 자체의 외부에서 '불변'상태가됩니다. 나를 올바른 방향으로 가리켜 주셔서 감사합니다. –