gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:04:52 25 4
gpt4 key购买 nike

我试过这个教程,但它对我不起作用 ( 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 类

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

An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

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

希望对您有所帮助。

关于c# - 仅更新 Entity Framework 6 中的一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24809993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com