잘못된 경우 AutoDetectChanges = 업데이트되지 않습니다 POCO : 나는 데이터 업데이트 속도를 AutoDetectChanges (Configuration.AutoDetectChangesEnabled = 거짓를) 해제. 그런 다음 을 추가하십시오. 또는 을 첨부하십시오.을 첨부하고 엔티티 상태를 EntityState.Modified으로 변경하십시오. 이 때문에 데이터베이스에서 업데이트되지 않는 다른 객체에 대한 참조가 발생합니다. 그러나 모든 스. 라 특성은 성공적으로 갱신됩니다.는 EF 4.1, POCO는 : 참조 종류의 호텔이 EF 4.1
Profiler는 모든 스칼라 속성에 대해 SQL 업데이트 작업을 생성하지만 참조 유형은 아닙니다. 실제로 코드에서 값이 변경되었지만 참조 유형은 아닙니다. 이 문제는 제 모델의 모든 유형의 엔티티를 재현했습니다.
는 작업을 추가 는와는 모두 잘 작동 EntityState.Added 연결합니다. AutoDetectChanges을 다시 켜면 모든 것이 업데이트 된 레코드에도 예상대로 작동합니다.
무엇이 잘못되었는지 알아주세요. EF의 변경 사항 감지에 대한 포괄적 인 문서를 찾을 수 없습니다.
UPDATE는
나는 문제를 재현하는 코드의 몇 가지 예제를 넣어하도록 요청했다. 도메인 :
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public virtual string City { get; set; }
}
의 DataContext :
public class DataContext : DbContext
{
public DbSet<Client> Clients { get; set; }
public DbSet<Address> Address { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Client>().HasOptional(c => c.Address);
}
}
는 클린트 테이블에 하나 개의 레코드를 추가 한 주소 수 있습니다. 포인트 클라이언트를 주소로 보냅니다. , 좋아,이 시간 모든
update [dbo].[Clients] set [Name] = 'Anna'
where ([Id] = 1)
설정이 true로 AutoDetectChangesEnabled 다시 코드를 실행합니다 :
update [dbo].[Clients]
set [Name] = 'Anna', [Address_Id] = null
where (([Id] = 1) and [Address_Id]=1)
을이 코드가하는 using (var cntx = new DataContext())
{
cntx.Configuration.AutoDetectChangesEnabled = false; // Reason of problem
var client = cntx.Clients.First();
client.Name = "Anna"; // This property will be updated
client.Address = null; // This property will not be updated
cntx.Clients.Attach(client);
cntx.Entry(client).State = EntityState.Modified;
cntx.SaveChanges();
}
는 다음과 같이 SQL 스크립트를 생성 : 그런 다음, 다음 코드를 실행
주소 값을 특정 값에서 null로 변경하거나 특정 값으로 다시 변경하거나 구체적인 값 하나를 다른 구체적 값으로 변경하면 AutoDe가 변경되는 동안 변경 내용이 추적되지 않습니다. tectChanges = false. EF 버그 같은데.
cntx.Entry(client).Reference(c => c.Address).CurrentValue = null;
그러나 나는 확실히 그것을 좋아하지 않아 :
문제를 재현 할 수있는 몇 가지 코드를 표시하십시오. –