private static EntityNavigationProperty CreateNavigationProperty(INavigation navigation) { var isCollection = navigation.IsCollection(); var isRequired = navigation.ForeignKey.IsRequired; var navigationProperty = new EntityNavigationProperty(navigation.Name, isCollection ? navigation.ClrType.GetItemType() : navigation.ClrType, isCollection, !isRequired); if (!isCollection) { var properties = navigation.ForeignKey.Properties; navigationProperty.DependentPropertyNames = properties.Select(p => p.Name).ToArray(); } return(navigationProperty); }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = base.CreateProperty(member, memberSerialization); property.ShouldSerialize = instance => { var entity = instance as ITrackable; if (entity == null) { return(true); } EntityNavigationProperty np = entity .GetNavigationProperties(false) .FirstOrDefault(x => x.Property == member); if (np == null) { return(true); // not a nav prop } if (np.ValueIsNull) { return(false); // nav prop is not initialized } foreach (var rp in np.AsReferenceProperty()) { // don't serialize unchanged reference navigation props return(IncludeReferenceProp(entity, rp.Property)); } // serialize collection navigation props return(true); }; // Inject the custom IValueProvider for entity collections which // returns only changed items property.ValueProvider = new CollectionValueProvider(this, member, property.ValueProvider); return(property); }
private static void ApplyChangesOnCollectionProperties(TrackingState stateFilter, bool includeState, EntityNavigationProperty navProp, EntityCollectionProperty <IList> colProp, DbContext context, ITrackable item, ObjectVisitationHelper visitationHelper, TrackingState?state = null) { // Apply changes to 1-M and M-M properties filtering by tracking state var count = colProp.EntityCollection.Count; for (int i = count - 1; i > -1; i--) { var trackableChild = colProp.EntityCollection[i] as ITrackable; if (trackableChild != null) { bool condition = includeState ? trackableChild.TrackingState == stateFilter : trackableChild.TrackingState != stateFilter; if (condition) { context.ApplyChanges(trackableChild, item, visitationHelper, navProp.Property.Name, state); } } } }