と の 3 つのクラスUserProfile
がGroups
ありContacts
ます。Group
を削除せずに を削除したいContacts
。GroupID
in Contacts
table はnull
、 が削除されたときにに設定する必要がありますGroup
。何をすべきか?
3 つのクラス:
[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; }
}