简体   繁体   中英

How to set cascade deleting properly in Entity Framework 6 when I have two foreign keys?

I have below example and I need to have behaviour: when I delete RoundAssetT , I don't want to delete his assetOccurrences ... I can't figure out how to set this relation in modelBuilder.

public class RoundAssetT 
{
    [Column(Order = 1), Key, ForeignKey("RoundT")]
    public int RoundTId { get; set; }

    [Column(Order = 2), Key, ForeignKey("AssetT")]
    public int AssetTId { get; set; }

    public virtual AssetT AssetT { get; set; }
    public virtual RoundT RoundT { get; set; }

    public string Name { get; set; }

    public ICollection<AssetOccurrenceT> AssetOccurrencesT { get; set; }
}

public class AssetOccurrenceT
{
    [Key]
    public int Id { get; set; }

    [Column(Order = 1), ForeignKey("RoundAssetT")]
    public int? RoundTId { get; set; }

    [Column(Order = 2), ForeignKey("RoundAssetT")]
    public int? AssetTId { get; set; }

    public virtual RoundAssetT RoundAssetT { get; set; }

    public string NameAOt { get; set; }
}

Could someone tell me how to set up this relation in Entity Framework 6?

AssetOccurrenceT.RoundAssetT would have to be optional since you want to be able to delete RoundAssetT s without also deleting referencing AssetOccurrenceT s

modelBuilder.Entity<AssetOccurrenceT>()
    .HasOptional(ao => ao.RoundAssetT)
    .WithMany(ra => ra.AssetOccurrencesT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM