我有三个课程UserProfile
,Groups
和Contacts
。我想要
删除Group
而不删除其Contacts
。 GroupID
表格中的Contacts
删除null
时应设置为Group
。怎么办?
三个班级:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<Groups> Groups { get; set; }
public virtual ICollection<Contacts> Contacts { get; set; }
}
public class Groups
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int GroupID { get; set; }
[Required]
public string GroupName { get; set; }
public string Description { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreateDate { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? ModifiedDate { get; set; }
public virtual UserProfile User { get; set; }
public virtual ICollection<Contacts> Contacts { get; set; }
}
public class Contacts
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ContactID { get; set; }
[ForeignKey("Group")]
public int? GroupID { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
public Prefix? Prefix { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime? BirthDate { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreateDate { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? ModifiedDate { get; set; }
public virtual UserProfile User { get; set; }
public virtual Groups Group { get; set; }
public virtual ICollection<Groups> Groups { get; set; }
}