EF从连接表中删除连接3个表

时间:2017-03-14 17:53:17

标签: c# entity-framework

我目前正在尝试实现连接3个表的联结表。我坚持要从这张桌子上删除物品。

连接表由相关实体的表和导航属性的键组成。 (我简化了这一点,在完整的代码中它有支持字段的实现)

[Table("SeasonHasBroadcast")]
public partial class SeasonHasBroadcastModel : BaseModel
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SeasonShow {get; set;}

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SeasonNumber {get; set;}

    [Key]
    [Column(Order = 2)]
    [StringLength(45)]
    public string AirtimeDay {get; set;}

    [Key]
    [Column(Order = 3)]
    public TimeSpan AirtimeTime {get; set;}     

    [Key]
    [Column(Order = 4)]
    [StringLength(45)]
    public string NetworkName {get; set;}

    public virtual AirtimeModel AirtimeLink {get; set;}

    public virtual NetworkModel NetworkLink {get; set;}

    public virtual SeasonModel SeasonLink {get; set;}
}

连接的表格包括public virtual ICollection<SeasonHasBroadcastModel> SeasonHasBroadcasts { get; set; }

当我向这个关系添加新实体时,我只是将新实体添加到此表中。问题是,当我尝试删除实体时。

我尝试使用SeasonViewModel.Season.SeasonHasBroadcasts.Remove(selected);删除。这样我就可以从其他只连接2个表的连接表中删除。那些表只是没有单独的表,只存在于集合中。其他尝试是通过导航属性,我从每个集合中删除了selected实体。

这两个人都抛出了这个错误:

  

属性'SeasonShow'是对象关键信息的一部分,无法修改。

当我调试并查看DBContext时,它会在显示,通话时间和网络实体上显示此错误,但不会在结点上显示此错误。

如何从这样的表中删除?

修改

public class BaseModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

的DbContext:

modelBuilder.Entity<AirtimeModel>()
    .HasMany(e => e.SeasonHasBroadcasts)
    .WithRequired(e => e.AirtimeLink)
    .HasForeignKey(e => new { e.AirtimeDay, e.AirtimeTime })
    .WillCascadeOnDelete(false);

modelBuilder.Entity<NetworkModel>()
    .HasMany(e => e.SeasonHasBroadcasts)
    .WithRequired(e => e.NetworkLink)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<SeasonModel>()
    .HasMany(e => e.SeasonHasBroadcasts)
    .WithRequired(e => e.SeasonLink)
    .HasForeignKey(e => new { e.SeasonShow, e.SeasonNumber })
    .WillCascadeOnDelete(false);

modelBuilder.Entity<SeasonHasBroadcastModel>()
    .Property(e => e.AirtimeDay)
    .IsUnicode(false);

modelBuilder.Entity<SeasonHasBroadcastModel>()
    .Property(e => e.NetworkName)
    .IsUnicode(false);

编辑2用于删除广播的命令     public override void Execute(object parameter)     {         var parrentViewModel =(SeasonViewModel)ParrentViewModel;         var selected = parrentViewModel.SelectedBroadcast;

    if (selected == null) return;

    if (parrentViewModel.Season.SeasonHasBroadcasts.Contains(selected))
    {
        //selected.SeasonLink.SeasonHasBroadcasts.Remove(selected);
        //selected.AirtimeLink.SeasonHasBroadcasts.Remove(selected);
        //selected.NetworkLink.SeasonHasBroadcasts.Remove(selected);

        parrentViewModel.BroadcastsViewModel.Service.DeleteData(selected);

        //parrentViewModel.Service.Context.SeasonHasBroadcasts.Remove(selected);
        //parrentViewModel.Season.SeasonHasBroadcasts.Remove(selected);

    }

    parrentViewModel.SelectedBroadcast = null;
}

添加广播的命令:

public override void Execute(object parameter)
{
    var parrentViewModel = (SeasonViewModel)ParrentViewModel;

    parrentViewModel.NewBroadcast.SeasonLink = parrentViewModel.Season;

    if (!parrentViewModel.Season.SeasonHasBroadcasts.Contains(parrentViewModel.NewBroadcast))
    {
        parrentViewModel.Season.SeasonHasBroadcasts.Add(parrentViewModel.NewBroadcast);
    }

    parrentViewModel.NewBroadcast = new SeasonHasBroadcastModel();
}

服务:(这是今天的尝试,仍然没有解决,同样的错误)

public override void DeleteData(SeasonHasBroadcastModel toDelete)
{
    if (toDelete.AirtimeLink != null && Context.Entry(toDelete.AirtimeLink).State == EntityState.Detached)
        Context.Airtimes.Attach(toDelete.AirtimeLink);

    if (toDelete.NetworkLink != null && Context.Entry(toDelete.NetworkLink).State == EntityState.Detached)
        Context.Networks.Attach(toDelete.NetworkLink);

    if (toDelete.SeasonLink != null && Context.Entry(toDelete.SeasonLink).State == EntityState.Detached)
        Context.Seasons.Attach(toDelete.SeasonLink);

    base.DeleteData(toDelete);
}

Base.DeleteData:

public virtual void DeleteData(TModel toDelete)
{
    _context.Set<TModel>().Remove(toDelete);
}

1 个答案:

答案 0 :(得分:0)

因此,在与我的大学教授协商后,我们达到了最简单的&#34;解决方案可能是SQL服务器上的存储过程。它不是最好的溶剂,但它的工作原理。还是想知道,没有存储过程怎么做。