Codehelp
Codehelp

Reputation: 4757

Using GraphDiff on three tables

I have three tables

ShippingZone
    ShippingZoneID -> PK

ZoneShippingMethod:
    ZoneShippingMethodID -> PK
    ShippingZoneID -> FK

ZoneShippingMethodRange
    ZoneShippingMethodID -> FK

The context:

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);
        }

Current code:

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

Gives this error:

The method used in the update mapping is not supported

Any clues?

Thanks & Regards.

Upvotes: 1

Views: 78

Answers (1)

andyp
andyp

Reputation: 6269

Your mapping is using FirstOrDefault which isn't supported in GraphDiff mappings, so this is producing the error.

The correct mapping in your case looks like this:

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

Upvotes: 1

Related Questions