private void ProcessMappingFragments(EntityInfo info, EntityTypeMapping etm)
        {
            // process each relationship between the type and a table
            foreach (var table in info.Tables.Keys)
            {
                var tableInfo = info.Tables[table];

                // find or create the mapping fragment
                var frag = FindMappingFragment(etm, table);
                if (tableInfo.UsesEntityTypeMappingKind(etm.Kind))
                {
                    if (frag == null)
                    {
                        var cmd = new CreateMappingFragmentCommand(etm, table.EntitySet as StorageEntitySet);
                        CommandProcessor.InvokeSingleCommand(_cpc, cmd);
                        frag = cmd.MappingFragment;
                    }
                    Debug.Assert(frag != null, "Could not locate or create the required MappingFragment");

                    ProcessMappingFragment(info, table, frag);
                }
                else
                {
                    // don't need it, remove it if we have one
                    if (frag != null)
                    {
                        AddToDeleteList(frag);
                    }
                }
            }
        }
Exemple #2
0
        internal override void CreateModelItem(CommandProcessorContext cpc, EditingContext context, EFElement underlyingModelItem)
        {
            Debug.Assert(context != null, "The context argument cannot be null");
            Debug.Assert(StorageEntityType == null, "Don't call this method if we already have a ModelItem");
            Debug.Assert(MappingConceptualEntityType.ConceptualEntityType != null, "The parent item isn't set up correctly");
            Debug.Assert(underlyingModelItem != null, "The underlyingModelItem cannot be null");
            var storeEntityType = underlyingModelItem as EntityType;

            Debug.Assert(
                storeEntityType != null,
                "underlyingModelItem must be of type EntityType, actual type = " + underlyingModelItem.GetType().FullName);
            Debug.Assert(storeEntityType.EntityModel.IsCSDL == false, "The storageEntityType must not be a CSDL EntityType");

            Context = context;
            ColumnMappings.Context = context;

            // create a context if we weren't passed one
            if (cpc == null)
            {
                cpc = new CommandProcessorContext(
                    Context, EfiTransactionOriginator.MappingDetailsOriginatorId, Resources.Tx_CreateMappingFragment);
            }

            // create the MappingFragment - if we already have a default EntityTypeMapping then just add
            // the MappingFragment to that mapping, otherwise if we already have an IsTypeOf
            // EntityTypeMapping then add the MappingFragment to that, otherwise create an IsTypeOf
            // EntityTypeMapping and add the MappingFragment to that
            var cet        = MappingConceptualEntityType.ConceptualEntityType;
            var defaultEtm = ModelHelper.FindEntityTypeMapping(cpc, cet, EntityTypeMappingKind.Default, false);
            var etmKind    = (defaultEtm == null ? EntityTypeMappingKind.IsTypeOf : EntityTypeMappingKind.Default);
            var cmd        = new CreateMappingFragmentCommand(cet, storeEntityType, etmKind);

            // add post-invoke event to fix up our view model
            cmd.PostInvokeEvent += (o, eventsArgs) =>
            {
                // fix up our view model
                ModelItem = storeEntityType;
                Parent.AddChild(this);

                // assign the table to our container node as well
                ColumnMappings.ModelItem = storeEntityType;

                // now try and do some match ups between the entity and the table
                var mappingStrategy = ModelHelper.DetermineCurrentInheritanceStrategy(cet);
                var topMostBaseType = cet.ResolvableTopMostBaseType;
                foreach (var child in ColumnMappings.Children)
                {
                    var msp = child as MappingScalarProperty;
                    if (msp != null)
                    {
                        List <Property> properties;
                        if (ModelHelper.FindScalarPropertyPathByLocalName(cet, msp.ColumnName, out properties))
                        {
                            msp.CreateModelItem(cpc, _context, properties);
                        }
                        else if (InheritanceMappingStrategy.TablePerType == mappingStrategy
                                 &&
                                 ModelHelper.FindScalarPropertyPathByLocalName(topMostBaseType, msp.ColumnName, out properties))
                        {
                            msp.CreateModelItem(cpc, _context, properties);
                        }
                    }
                }
            };

            try
            {
                // now update the model
                var cp = new CommandProcessor(cpc);
                cp.EnqueueCommand(cmd);
                cp.Invoke();
            }
            catch
            {
                ModelItem = null;
                ColumnMappings.ModelItem = null;
                Parent.RemoveChild(this);
                throw;
            }
        }