仅更新Entity Framework 6中的一个字段

时间:2014-07-17 17:33:44

标签: c# entity-framework asp.net-mvc-5

我已经尝试过这个教程,但它并不适合我(Link),我的目标是更新我的实体的一个字段......没有其他字段。我正在测试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 Class

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

  

类型的例外   &#39; System.Data.Entity.Validation.DbEntityValidationException&#39;发生了   在EntityFramework.dll中但未在用户代码中处理

     

其他信息:一个或多个实体的验证失败。   请参阅&#39; EntityValidationErrors&#39;物业详情。

EntityValidationErrors为空...

1 个答案:

答案 0 :(得分:3)

您可以使用以下内容定义要更新的属性:

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

希望有所帮助。