다음과 같이 나는 (내가 먼저 코드를 사용) 일대 다 관계로 연결된 두 기관이 : 나는 하나 또는 여러 스크린에 연결된 컴퓨터를 삭제하면EF로 캐스케이드 삭제를 비활성화 하시겠습니까?
public class Computer
{
[Key]
public int ComputerId { get; set; }
[Required]
public int ComputerIdInventory { get; set; }
[DataType(DataType.Date)]
public DateTime? AcquisitionDate { get; set; }
[DataType(DataType.Date)]
public DateTime? LastUpdate { get; set; }
public string Comment { get; set; }
//Foreign keys
public int? ComputerModelId { get; set; }
public int? EmployeeId { get; set; }
//Navigation properties
public virtual ICollection<Screen> Screens { get; set; }
}
public class Screen
{
[Key]
public int ScreenId { get; set; }
[Required]
public int ScreenIdInventory { get; set; }
public string Comment { get; set; }
//Foreign keys
public int? ComputerId { get; set; }
//Navigation properties
public virtual Computer Computer { get; set; }
}
, 나는 다음과 같은 한 오류 :
[SqlException (0x80131904): The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Screen_dbo.Computer_ComputerId". The conflict occurred in database "CPInventory", table "dbo.Screen", column 'ComputerId'.
나는 많은 게시물을 읽었으며 다른 사람들을 위해 일한 것으로 보인 2 가지를 시도했다. 나는 "OnModelCreating"방법을 변경 및 추가 :
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
을 그리고 나도이 시도 :
modelBuilder.Entity<Computer>()
.HasMany<Screen>(c => c.Screens)
.WithOptional(s => s.Computer)
.WillCascadeOnDelete(false);
그러나 해결책으로는 ... 내가 뭔가 잘못하고없는 건가요 일했다? 또한 데이터베이스를 업데이트하지만 아무 것도 변경되지 않았습니다. 변경 사항을 고려하기 위해 데이터베이스를 완전히 삭제하고 다시 만들어야합니까?
고맙습니다.
편집
: 여기 은 삭제 코드public ActionResult DeleteConfirmed(int id)
{
Computer computer = db.Computers.Find(id);
db.Computers.Remove(computer);
db.SaveChanges();
return RedirectToAction("Index");
}
유창 구성이 괜찮 저장 컴퓨터
을 삭제, 문제는 다르다. 삭제 코드를 공유 할 수 있습니까? –