在Entity Framework 6中如何只更新一个字段

3
我已经尝试了这个教程,但它对我没用(链接),我的目标是只更新实体中的一个字段... 不涉及其他字段。我正在测试 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为空...


2
你能详细说明一下你的代码“不工作”的情况吗?你期望发生什么,实际上又发生了什么?如果你遇到了异常,请发布它发生的行和异常详情。 - gunr2171
2
我认为,在 db.SaveChanges(); 上,Name = null。 - Alexandr Sulimov
1
您可以附加实体而无需从数据库请求它。 - Rodrigo
1
@Rodrigo - 哦,没错。 - Neil Smith
1
不好意思,别听我说。@Rodrigo是对的。你可以像你现在这样附加它。只需在附加之前分配名称即可。 - Neil Smith
显示剩余3条评论
1个回答

4

您可以通过如下方式定义要更新的属性:

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();
    }
}

希望这能帮到您。

发生了类型为“System.Data.Entity.Validation.DbEntityValidationException”的异常,但在用户代码中未被处理。附加信息:一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。 - chemitaxis
好的,我稍后会测试一下,我现在在家里...但是我认为这很复杂...我想在一个有很多DTO的API REST中使用它...非常感谢,如果它能够工作,我会给你的答案投票,评价为好的 ;) - chemitaxis
1
好的,它可以工作,但是sampleModel.Name=string.Empty; //不应该需要,但实际上是需要的! :( 我不知道为什么... - chemitaxis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接