小编Ste*_*ard的帖子

实体框架:更新影响对象

我遇到了EF Update方法的问题.

我有一个包含mutliple信息和其他复杂对象的对象例如,当我调试我的object.otherObject包含数据时.

然后我用以下行更新它:

_dbContext.Update(object);
Run Code Online (Sandbox Code Playgroud)

问题是在更新调用之后,我的object.otherObject失去了它的值并变为null.

当尝试使用不同的操作(用户界面操作)和相同的对象(不同的值)时,我不会遇到该问题.

根据我对EF更新的理解,它应该将对象标记为已修改但保持完整.

编辑:

我的映射文件是:

internal class ObjectConfiguration: EntityTypeConfiguration<Object>
{
    public ObjectConfiguration()
    {
        ToTable("TABLE_NAME", Properties.Settings.Default.DBSchema);
        HasKey(s => s.Id);

        Property(s => s.Id).HasColumnName("COLUMNNAME");
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑2:

_dbContext是根据EF 5存储库和工作单元格构建的自定义对象:

https://docs.microsoft.com/fr-fr/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-的工作图案,在安-ASP净MVC应用程序

像这样:

public class UserManager : IDisposable
{        
    private readonly IRepository _dbContext;
}

public interface IRepository
{
    TEntity Update<TEntity>(TEntity entity) where TEntity : class;
}
Run Code Online (Sandbox Code Playgroud)

编辑3:

通过Attach函数时,对象实际上会清空其值:

public TEntity Update<TEntity>(TEntity entity) where TEntity : class
    {
        try
        {
            TEntity attachedEntity = Set<TEntity>().Attach(entity);
            var …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

6
推荐指数
1
解决办法
79
查看次数

标签 统计

c# ×1

entity-framework ×1