0
mysql 데이터베이스로 간단한 asp.net (MVC4) 웹 페이지를 만들었습니다. 거기에는 테이블 주문에 FORIGN Key Persons_ID가있는 두 개의 테이블 (사람과 주문)이 있습니다. 나는 삭제 기능을 만들려고하는데, 그래서 사람 테이블에서 사람을 삭제하면이 사람의 주문 테이블에서 모든 주문도 삭제됩니다.MySQL 및 ASP.NET MVC4에 대한 계단식 삭제
모델을 만들기 위해 나는 ADO.NET을 사용하고 각 테이블이 두 모델을 만들 :
persons.cs 을 System.ComponentModel.DataAnnotations를 사용하여;
namespace MvcMySQLTest1.Models
{
using System;
using System.Collections.Generic;
public partial class person
{
public person()
{
this.orders = new HashSet<order>();
}
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Adress { get; set; }
public string City { get; set; }
public virtual ICollection<order> orders { get; set; }
}
}
orders.cs
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace MvcMySQLTest1.Models
{
using System;
using System.Collections.Generic;
public partial class order
{
public int O_Id { get; set; }
public int OrderNo { get; set; }
public Nullable<int> Persons_Id { get; set; }
public virtual person person { get; set; }
}
}
나는 또한 MainModel을 만들 수있다 - 위의 두 모델에 대한 I 컨테이너처럼은 - 그래서 때 계단식 지금
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
namespace MvcMySQLTest1.Models
{
public class MainModel
{
public person Persons { get; set; }
public order Orders { get; set; }
}
}
내가이 시도가 삭제
사람을 삭제합니다.이 사람의 모든 주문도 주문 표에 삭제되지만 이는 이 작동하지 않는 것 같습니다 :using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
namespace MvcMySQLTest1.Models
{
public class MainModel : DbContext //added
{
public person Persons { get; set; }
public order Orders { get; set; }
//added
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<orders>()
.HasOptional(a => a.persons)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
}
}
tnx에 대한 답변을 위해 가능한 한 빨리 시도해 보겠습니다. 하지만 투표 해 줘. – DaniKR