[英]Update just one field in Entity Framework 6
我已經嘗試過本教程,但是對我來說不起作用( 鏈接 ),我的目標是僅更新實體的一個字段,而沒有其他字段。 我正在測試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()中
EntityFramework.dll中發生類型為'System.Data.Entity.Validation.DbEntityValidationException'的異常,但未在用戶代碼中處理
附加信息:對一個或多個實體的驗證失敗。 有關更多詳細信息,請參見'EntityValidationErrors'屬性。
EntityValidationErrors為空...
您可以定義將使用以下內容更新的屬性:
MyContext db = new MyContext();
MyClass sampleModel = new MyClass();
sampleModel.IdMyClass = "1d1ba1f2-8c08-c334-5486-08d16fecc6e3"; //GUID
sampleModel.ModificationDate = DateTime.Now;
sampleModel.Name=string.Empty; //should not be needed
db.MyClass.Attach(sampleModel);
var entry=db.Entry(sampleModel);
entry.State = EntityState.Modified;
var excluded = new string[] { "Name" };
foreach (var name in excluded)
{
entry.Property(name).IsModified = false;
}
db.SaveChanges();
好的,這是我的代碼版本。 班級
public partial class MyClass
{
public string IdMyClass { get; set; }
public string Name { get; set; }
public System.DateTime ModificationDate { get; set; }
public ICollection<OtherClass> OtherClass { get; set; }
}
public partial class OtherClass
{
public int Id { get; set; }
public string Stuff { get; set; }
}
public class MyContext : DbContext
{
public MyContext()
: base("MyContextDb")
{
}
public DbSet<MyClass> MyClasses { get; set; }
public DbSet<OtherClass> OtherClasses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyClass>().ToTable("MyClass");
modelBuilder.Entity<MyClass>()
.HasKey(t => t.IdMyClass)
.Property(t => t.Name)
.IsRequired()
.HasMaxLength(256);
modelBuilder.Entity<MyClass>().Property(t => t.IdMyClass).HasColumnName("IdMyClass");
modelBuilder.Entity<MyClass>().Property(t => t.Name).HasColumnName("Name");
modelBuilder.Entity<MyClass>().Property(t => t.ModificationDate).HasColumnName("ModificationDate");
}
}
一個簡單的控制台程序來測試結果
class Program
{
static void Main(string[] args)
{
MyContext db = null; ;
try
{
db = new MyContext();
Foo(db);
Console.WriteLine("Id\tDate\tName");
foreach(var c in db.MyClasses)
{
Console.WriteLine("{0}\t{1}\t{2}",c.IdMyClass,c.ModificationDate,c.Name);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (db != null)
{
db.Dispose();
}
}
Console.ReadLine();
}
static void Foo(MyContext db)
{
var sampleModel = new MyClass();
sampleModel.IdMyClass = "1d1ba1f2-8c08-c334-5486-08d16fecc6e3"; //GUID
sampleModel.ModificationDate = DateTime.Now;
sampleModel.Name = string.Empty; //should not be needed
db.MyClasses.Attach(sampleModel);
var entry = db.Entry(sampleModel);
entry.State = EntityState.Modified;
var excluded = new string[] { "Name" };
foreach (var name in excluded)
{
entry.Property(name).IsModified = false;
}
db.SaveChanges();
}
}
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.