在三个表上使用GraphDiff

时间:2014-10-18 12:30:01

标签: entity-framework graphdiff

我有三张桌子

ShippingZone
    ShippingZoneID -> PK

ZoneShippingMethod:
    ZoneShippingMethodID -> PK
    ShippingZoneID -> FK

ZoneShippingMethodRange
    ZoneShippingMethodID -> FK

背景信息:

public ShippingZonesContext()
            : base("name=ShippingZonesContext")
        {
        }

        public virtual DbSet<ShippingZone> ShippingZones { get; set; }
        public virtual DbSet<ZoneShippingMethod> ZoneShippingMethods { get; set; }
        public virtual DbSet<ZoneShippingMethodRange> ZoneShippingMethodRanges { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ShippingZone>()
                .Property(e => e.ZoneCountryIDs)
                .IsUnicode(false);

            modelBuilder.Entity<ShippingZone>()
                .Property(e => e.ZoneStateIDs)
                .IsUnicode(false);

            modelBuilder.Entity<ShippingZone>()
                .HasMany(e => e.ZoneShippingMethods)
                .WithRequired(e => e.ShippingZone)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<ZoneShippingMethod>()
                .Property(e => e.UserID)
                .IsUnicode(false);

            modelBuilder.Entity<ZoneShippingMethod>()
                .Property(e => e.Password)
                .IsUnicode(false);

            modelBuilder.Entity<ZoneShippingMethod>()
                .Property(e => e.ShippingServiceTypeIDs)
                .IsUnicode(false);

            modelBuilder.Entity<ZoneShippingMethod>()
                .Property(e => e.AccessKey)
                .IsUnicode(false);

            modelBuilder.Entity<ZoneShippingMethod>()
                .Property(e => e.ShipperNumber)
                .IsUnicode(false);

            modelBuilder.Entity<ZoneShippingMethod>()
                .HasMany(e => e.ZoneShippingMethodRanges)
                .WithRequired(e => e.ZoneShippingMethod)
                .WillCascadeOnDelete(false);
        }

当前代码:

context.UpdateGraph(shippingZone, map => map
                        .OwnedCollection(p => p.ZoneShippingMethods).OwnedCollection(p => p.ZoneShippingMethods.FirstOrDefault().ZoneShippingMethodRanges)
                    );

给出了这个错误:

  

不支持更新映射中使用的方法

任何线索?

谢谢&amp;问候。

1 个答案:

答案 0 :(得分:1)

您的映射使用的是FirstOrDefault,GraphDiff映射不支持,因此产生错误。

您案例中的正确映射如下所示:

context.UpdateGraph(shippingZone, 
    map => map.OwnedCollection(zone => zone.ZoneShippingMethods, 
        with => with.OwnedCollection(method => method.ZoneShippingMethodRanges)));