我已经尝试了这个教程,但它对我没用(链接),我的目标是只更新实体中的一个字段... 不涉及其他字段。我正在测试 EntityFramework,并且想检查一下是否可以自动映射 DTO 并在之后只更新一个字段。
我试过了这个:
我试过了这个:
MyContext db = new MyContext();
MyClass sampleModel = new MyClass();
sampleModel.IdMyClass = "1d1ba1f2-8c08-c334-5486-08d16fecc6e3"; //GUID
sampleModel.ModificationDate = DateTime.Now;
db.MyClass.Attach(sampleModel);
db.Entry(sampleModel).Property(x => x.ModificationDate).IsModified = true;
db.SaveChanges();
MyClass 类
public partial class MyClass
{
public string IdMyClass { get; set; }
public string Name { get; set; }
public System.DateTime ModificationDate { get; set; }
public virtual ICollection<OtherClass> OtherClass{ get; set; }
}
MyClassMap
public MyClassMap() : EntityTypeConfiguration<MyClass>
{
//Primary Key
this.HasKey(t => t.IdMyClass);
//Properties
this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(256);
// Table & Column Mappings
this.ToTable("MyClass");
this.Property(t => t.IdMyClass).HasColumnName("IdMyClass");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.ModificationDate).HasColumnName("ModificationDate");
}
我的问题出在哪里??
错误细节,出现在db.SaveChanges()中
类型为'System.Data.Entity.Validation.DbEntityValidationException'的异常在EntityFramework.dll中发生,但未在用户代码中处理
其他信息:一个或多个实体验证失败。请参阅“EntityValidationErrors”属性获取更多详细信息。
EntityValidationErrors为空...