예제 #1
0
 private void Relate(Feature origin, Feature derived)
 {
     if (_relationshipClass != null)
     {
         _relationshipClass.CreateRelationship(origin, derived);
     }
 }
        public async Task MainMethodCode()
        {
            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (RelationshipClass relationshipClass = geodatabase.OpenDataset <RelationshipClass>("LocalGovernment.GDB.OverviewToProject"))
                    using (FeatureClass projectsFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.CIPProjects"))
                        using (FeatureClass overviewFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.CIPProjectsOverview"))
                        {
                            FeatureClassDefinition overviewDefinition = overviewFeatureClass.GetDefinition();
                            // Because the following fields are used in 2 places, it is more inefficient to get their respective indexes so that they can be resued.
                            int projectManagerIndex = overviewDefinition.FindField("PROJECTMAN");
                            int fundSourceIndex     = overviewDefinition.FindField("FUNDSOUR");

                            RelationshipClassDefinition relationshipClassDefinition = relationshipClass.GetDefinition();
                            // This will be PROJNAME. This can be used to get the field index or used directly as the field name.
                            string originKeyField = relationshipClassDefinition.GetOriginKeyField();

                            EditOperation editOperation = new EditOperation();
                            editOperation.Callback(context =>
                            {
                                // The rows are being added to illustrate adding relationships. If one has existing rows, those can be used to add a relationship.
                                RowBuffer projectsRowBuffer  = projectsFeatureClass.CreateRowBuffer();
                                projectsRowBuffer["TOTCOST"] = 500000;

                                RowBuffer overviewRowBufferWithoutPKValue            = overviewFeatureClass.CreateRowBuffer();
                                overviewRowBufferWithoutPKValue[projectManagerIndex] = "John Doe";
                                overviewRowBufferWithoutPKValue[fundSourceIndex]     = "Public";

                                RowBuffer overviewRowBuffer            = overviewFeatureClass.CreateRowBuffer();
                                overviewRowBuffer["PROJNAME"]          = "LibraryConstruction";
                                overviewRowBuffer[projectManagerIndex] = "John Doe";
                                overviewRowBuffer[fundSourceIndex]     = "Public";

                                using (Row projectsRow = projectsFeatureClass.CreateRow(projectsRowBuffer))
                                    using (Row overviewRowWithoutPKValue = overviewFeatureClass.CreateRow(overviewRowBufferWithoutPKValue))
                                        using (Row overviewRow = overviewFeatureClass.CreateRow(overviewRowBuffer))
                                        {
                                            try
                                            {
                                                Relationship failingRelationship = relationshipClass.CreateRelationship(overviewRowWithoutPKValue, projectsRow);
                                            }
                                            catch (GeodatabaseRelationshipClassException exception)
                                            {
                                                // This will have a message "Unable to obtain origin primary key value.". So, the origin row needs to have the origin *primary*
                                                // key value referenced by the origin *foreign* key value in the RelationshipClass.
                                                Console.WriteLine(exception);
                                            }

                                            Relationship relationship = relationshipClass.CreateRelationship(overviewRow, projectsRow);

                                            //To Indicate that the Map has to draw this feature/row and/or the attribute table has to be updated
                                            context.Invalidate(projectsRow);
                                            context.Invalidate(overviewRow);
                                            context.Invalidate(relationshipClass);
                                        }
                            }, projectsFeatureClass, overviewFeatureClass);

                            bool editResult = editOperation.Execute();

                            //If the table is non-versioned this is a no-op. If it is versioned, we need the Save to be done for the edits to be persisted.
                            bool saveResult = await Project.Current.SaveEditsAsync();
                        }
        }