代码第一个外键关系,删除时设置为null

时间:2013-09-28 08:56:21

标签: entity-framework-5

我有三个课程UserProfileGroupsContacts。我想要 删除Group而不删除其ContactsGroupID表格中的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; }
}

0 个答案:

没有答案