public Extract_string_value_to_model_property() { EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); }
public void SetUp() { entities = new EntitySetImpl(); entity1 = new EntityImpl("Entity1"); entity1.AddProperty(new PropertyImpl("PrimaryKey") { Type = "System.Int32", IsKeyProperty = true }); entity2 = new EntityImpl("Entity2"); entity2.AddProperty(new PropertyImpl("PrimaryKey") { Type = "System.Int32", IsKeyProperty = true }); entities.AddEntity(entity1); entities.AddEntity(entity2); reference = entity1.CreateReferenceTo(entity2); reference.Cardinality1 = Cardinality.Many; reference.Cardinality2 = Cardinality.Many; var proc = new MappingProcessor(new OneToOneEntityProcessor()); mappingSet = proc.CreateOneToOneMapping(entities); }
public void SetUp() { entities = new EntitySetImpl(); entityParent = new EntityImpl("EntityParent"); entityParent.AddProperty(new PropertyImpl("PrimaryKey") { Type = "int", IsKeyProperty = true }); entityChild = new EntityImpl("EntityChild"); entityParent.AddChild(entityChild); entityChild.CopyPropertyFromParent(entityParent.ConcreteProperties[0]); entityChild.AddProperty(new PropertyImpl("ActualProperty") { Type = "string" }); entities.AddEntity(entityParent); entities.AddEntity(entityChild); var proc = new MappingProcessor(new OneToOneEntityProcessor()); mappingSet = proc.CreateOneToOneMapping(entities); }
public void MappingProcessor_should_call_post_deploy_action_when_script_not_specified() { // Arrange var deployAgentProvider = new DeployAgentProvider(); var deploymentFolderSource = MockRepository.GenerateStub <IDeploymentFolderSource>(); var mappingEvaluator = MockRepository.GenerateStub <IMappingEvaluator>(); var mappingProcessor = new MappingProcessor(deployAgentProvider, deploymentFolderSource, mappingEvaluator); var postDeployAction = MockRepository.GenerateStub <IPostDeployAction>(); var buildDetail = new BuildDetail(); var mappings = new[] { new Mapping { RetainBuildSpecified = true, RetainBuild = true } }; mappingEvaluator.Stub(o => o.DoesMappingApply(null, null, null)) .IgnoreArguments() .Return(true); var statusChanged = new BuildStatusChangeEvent { StatusChange = new Change() }; // Act mappingProcessor.ProcessMappings(mappings, statusChanged, buildDetail, postDeployAction); // Assert postDeployAction.AssertWasCalled(o => o.DeploymentFinished( Arg <Mapping> .Is.Equal(mappings[0]), Arg <DeployAgentResult> .Matches(result => !result.HasErrors)) ); }
public void MappingProcessor_should_record_mapped_event_for_applicable_mappings() { // Arrange const int eventId = 7; var mappingEvaluator = MockRepository.GenerateStub <IMappingEvaluator>(); var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>(); var mappingExecutor = MockRepository.GenerateStub <IMappingExecutor>(); Func <IMappingExecutor> executorFactory = () => mappingExecutor; var mappingProcessor = new MappingProcessor(mappingEvaluator, deploymentEventRecorder, executorFactory); var postDeployAction = MockRepository.GenerateStub <IPostDeployAction>(); var buildDetail = new BuildDetail(); var mappings = new[] { new Mapping { Script = "AScript.ps1", Queue = "AQueue" } }; mappingEvaluator.Stub(o => o.DoesMappingApply(null, null, null)) .IgnoreArguments() .Return(true); var statusChanged = new BuildStatusChangeEvent { StatusChange = new Change() }; // Act mappingProcessor.ProcessMappings(mappings, statusChanged, buildDetail, postDeployAction, eventId); // Assert deploymentEventRecorder.AssertWasCalled(o => o.RecordQueued(eventId, mappings[0].Script, mappings[0].Queue)); }
private void AcceptChanges() { var tablesToProcess = SelectedTables; MappingProcessor proc = new MappingProcessor(new OneToOneEntityProcessor(Set.EntitySet.Entities.Select(e => e.Name))); proc.CreateOneToOneMappingsFor(tablesToProcess, Set); Close(); //mainPanel.CloseDatabaseRefreshResultsForm(Changes.WereAccepted); }
public ActionResult Index() { var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var characterViewModel = new CharacterViewModel(RenderingContext.Current.Rendering); ViewBag.SerializedModel = JsonConvert.SerializeObject(characterViewModel, Formatting.Indented); return(View(characterViewModel)); }
public void It_Creates_A_Column_On_The_Many_Side() { var proc = new MappingProcessor(new OneToOneEntityProcessor()); var mappingSet = proc.CreateOneToOneMapping(entities); var table1 = mappingSet.Database.Tables[1]; var table2 = mappingSet.Database.Tables[1]; Assert.That(table2.Columns, Has.Count(2)); // Extra column added var newColumn = table2.Columns[1]; Assert.That(newColumn.Datatype, Is.SameAs(table1.Columns[0].Datatype)); }
public void Correct_Entities_Are_Created() { EntityProcessor proc = MockRepository.GenerateMock <EntityProcessor>(); Entity entity1 = new EntityImpl(); proc.Stub(p => p.CreateEntity(Table1)).Return(entity1); MappingProcessor mappingProc = new MappingProcessor(proc); mappingProc.CreateOneToOneMappingsFor(new[] { Table1 }, Set); proc.AssertWasCalled(p => p.CreateEntity(Table1)); proc.AssertWasNotCalled(p => p.CreateEntity(Table2)); }
public void It_Creates_A_Unique_Key_On_The_Second_Entity() { var proc = new MappingProcessor(new OneToOneEntityProcessor()); var mappingSet = proc.CreateOneToOneMapping(entities); var table2 = mappingSet.Database.Tables[1]; Assert.That(table2.Keys, Has.Count(3), "The second table did not get the new keys"); var uniqueKey = table2.Keys.FirstOrDefault(k => k.Keytype == DatabaseKeyType.Unique); var foreignKey = table2.Keys.FirstOrDefault(k => k.Keytype == DatabaseKeyType.Foreign); Assert.That(uniqueKey, Is.Not.Null); Assert.That(uniqueKey.Columns, Has.Count(1)); Assert.That(uniqueKey.Columns[0], Is.SameAs(foreignKey.Columns[0])); }
public void It_Creates_A_Foreign_Key_On_The_Second_Entity() { var proc = new MappingProcessor(new OneToOneEntityProcessor()); var mappingSet = proc.CreateOneToOneMapping(entities); var table1 = mappingSet.Database.Tables[0]; var table2 = mappingSet.Database.Tables[1]; Assert.That(table2.Keys, Has.Count(3), "The second table did not get the new keys"); var key = table2.Keys.FirstOrDefault(k => k.Keytype == DatabaseKeyType.Foreign); var primaryKey = table1.Keys.FirstOrDefault(k => k.Keytype == DatabaseKeyType.Primary); Assert.That(key, Is.Not.Null); Assert.That(key.ReferencedKey, Is.Not.Null); Assert.That(key.ReferencedKey, Is.SameAs(primaryKey)); }
public void It_Creates_A_Single_Relationship_With_The_Right_Keys() { var proc = new MappingProcessor(new OneToOneEntityProcessor()); var mappingSet = proc.CreateOneToOneMapping(entities); var table1 = mappingSet.Database.Tables[0]; var table2 = mappingSet.Database.Tables[1]; var foreignKey = table2.Keys.FirstOrDefault(k => k.Keytype == DatabaseKeyType.Foreign); var primaryKey = table1.Keys.FirstOrDefault(k => k.Keytype == DatabaseKeyType.Primary); Assert.That(table1.Relationships, Has.Count(1)); var relationship = table1.Relationships[0]; Assert.That(relationship.PrimaryKey, Is.SameAs(primaryKey)); Assert.That(relationship.ForeignKey, Is.SameAs(foreignKey)); }
public void GetIsFromAMultilist() { using (var db = SharedDatabaseDefinition.CpuListDatabase()) { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); // Act var item = db.GetItem("/sitecore/content/IntelCps"); var intelCpus = factory.Create <CpuSelectionModel>(item); // Assert intelCpus.SelectedCps.Split('|').Should().HaveCount(2); } }
public void DateTime_type_success() { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); using (var db = SharedDatabaseDefinition.StarwarsDatabase()) { // Act var item = db.GetItem("/sitecore/content/Astra"); var carViewModel = factory.Create <VehichleModel>(item); // Assert } }
public void MappingProcessor_should_process_multiple_mappings_in_same_queue_in_series() { // Arrange var deployAgent = new ParallelDeployAgent(); var deployAgentProvider = MockRepository.GenerateStub <IDeployAgentProvider>(); deployAgentProvider.Stub(o => o.GetDeployAgent(Arg <Mapping> .Is.Anything)) .Return(deployAgent); var deploymentFolderSource = MockRepository.GenerateStub <IDeploymentFolderSource>(); var mappingEvaluator = MockRepository.GenerateStub <IMappingEvaluator>(); var postDeployAction = MockRepository.GenerateStub <IPostDeployAction>(); var buildDetail = new BuildDetail(); var mappings = new[] { new Mapping { Queue = "A" }, new Mapping { Queue = "A" } }; mappingEvaluator.Stub(o => o.DoesMappingApply(null, null, null)) .IgnoreArguments() .Return(true); var statusChanged = new BuildStatusChangeEvent { StatusChange = new Change() }; var mappingProcessor = new MappingProcessor(deployAgentProvider, deploymentFolderSource, mappingEvaluator); // Act mappingProcessor.ProcessMappings(mappings, statusChanged, buildDetail, postDeployAction); var expire = DateTime.UtcNow.AddSeconds(5); while (!deployAgent.HasExecuted && DateTime.UtcNow < expire) { Thread.Sleep(500); } // Assert Assert.IsFalse(deployAgent.WasParallel); }
private MappingSet CreateMappingSet_OneTable() { Database database = new Database("DB1"); var table = new Table("Table1"); table.AddColumn(new Column("Column1") { Datatype = "int", InPrimaryKey = true }); table.AddColumn(new Column("Column2") { Datatype = "nvarchar", Size = 50 }); database.AddTable(table); MappingProcessor processor = new MappingProcessor(new OneToOneEntityProcessor()); return(processor.CreateOneToOneMapping(database)); }
public void CircularReferenceBetweenTwoViewModels() { using (var db = SharedDatabaseDefinition.CpuDatabase()) { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); // Act var item = db.GetItem("/sitecore/content/Haswell"); var haswellCpu = factory.Create <CpuModel>(item); // Assert haswellCpu.ArchitectureName.ShouldBeEquivalentTo("Haswell"); haswellCpu.Predecessor.ArchitectureName.Should().Be("Ivy Bridge"); haswellCpu.Predecessor.Successor.ArchitectureName.Should().Be("Haswell"); } }
public void boolean_is_true_Success() { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); using (var db = SharedDatabaseDefinition.StarwarsDatabase()) { // Act var item = db.GetItem("/sitecore/content/XWing"); var carViewModel = factory.Create <VehichleModel>(item); // Assert carViewModel.HasHyperdrive.Should().Be(true); } }
public void GeneralLinkAdapter_ExternalLink_Success() { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); using (var db = SharedDatabaseDefinition.StarwarsDatabase()) { // Act var item = db.GetItem("/sitecore/content/XWing"); var carViewModel = factory.Create <VehichleModel>(item); // Assert carViewModel.WikiLink.Url.Should().Be(SharedDatabaseDefinition.StaticVehichleData.WikiLink.Url); carViewModel.WikiLink.Desciption.Should().Be(SharedDatabaseDefinition.StaticVehichleData.WikiLink.Desciption); } }
public void Correct_References_Are_Created() { EntityProcessor proc = MockRepository.GenerateMock <EntityProcessor>(); Entity entity1 = new EntityImpl(); Entity entity2 = new EntityImpl(); Reference reference = new ReferenceImpl(entity1, entity2); proc.Stub(p => p.CreateEntity(Table1)).Return(entity1); proc.Stub(p => p.CreateEntity(Table2)).Return(entity2); proc.Stub(p => p.CreateReference(Relationship12, Set.EntitySet)).Return(reference); MappingProcessor mappingProc = new MappingProcessor(proc); mappingProc.CreateOneToOneMappingsFor(new[] { Table1, Table2 }, Set); proc.AssertWasCalled(p => p.CreateEntity(Table1)); proc.AssertWasCalled(p => p.CreateEntity(Table2)); proc.AssertWasCalled(p => p.CreateReference(Relationship12, Set.EntitySet)); proc.AssertWasNotCalled(p => p.CreateReference(Relationship13, Set.EntitySet)); }
public void Get_a_single_reference_viewmodel_with_its_properties_populated_Success() { using (var db = SharedDatabaseDefinition.StarwarsDatabase()) { using (ShareMediaProvider.MediaProvider()) { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); // Act var item = db.GetItem("/sitecore/content/XWing"); var xwingViewModel = factory.Create <VehichleModel>(item); // Assert xwingViewModel.Name.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Name); xwingViewModel.Length.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Length); xwingViewModel.CargoKg.Should().Be(SharedDatabaseDefinition.StaticVehichleData.CargoKg); xwingViewModel.HasHyperdrive.Should().Be(SharedDatabaseDefinition.StaticVehichleData.HasHyperdrive); xwingViewModel.WikiLink.Desciption.Should().Be(SharedDatabaseDefinition.StaticVehichleData.WikiLink.Desciption); xwingViewModel.WikiLink.Url.Should().Be(SharedDatabaseDefinition.StaticVehichleData.WikiLink.Url); xwingViewModel.WikiLink.LinkType.Should().Be(SharedDatabaseDefinition.StaticVehichleData.WikiLink.LinkType); xwingViewModel.Photo.Url.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Photo.Url); xwingViewModel.Photo.Alt.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Photo.Alt); xwingViewModel.ClassModel.Name.Should().Be(SharedDatabaseDefinition.StaticVehichleData.ClassModel.Name); xwingViewModel.Appearances.Should().HaveCount(SharedDatabaseDefinition.StaticVehichleData.Appearances.Count()); xwingViewModel.Appearances.Last().Title.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Appearances.First().Title); xwingViewModel.Appearances.Last().ReleaseDate.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Appearances.First().ReleaseDate); xwingViewModel.Appearances.First().Title.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Appearances.Last().Title); xwingViewModel.Appearances.First().ReleaseDate.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Appearances.Last().ReleaseDate); } } }
private MappingSet CreateMappingSet_TwoTables() { Database database = new Database("DB1"); var table1 = new Table("Table1"); table1.AddColumn(new Column("Column1") { Datatype = "int", InPrimaryKey = true }); var table2 = new Table("Table2"); table2.AddColumn(new Column("Column1") { Datatype = "int", InPrimaryKey = true }); database.AddTable(table1); database.AddTable(table2); MappingProcessor processor = new MappingProcessor(new OneToOneEntityProcessor()); return(processor.CreateOneToOneMapping(database)); }
public void ImageAdapter_Success() { // Arrange EnvironmentSetttings.ApplicationPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; var mappingProcessor = new MappingProcessor(); mappingProcessor.Process(null); var factory = new ModelFactory(); using (var db = SharedDatabaseDefinition.StarwarsDatabase()) { using (ShareMediaProvider.MediaProvider()) { // Act var item = db.GetItem("/sitecore/content/XWing"); var carViewModel = factory.Create <VehichleModel>(item); // Assert carViewModel.Photo.Alt.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Photo.Alt); carViewModel.Photo.Url.Should().Be(SharedDatabaseDefinition.StaticVehichleData.Photo.Url); } } }
public void MappingProcessor_should_process_multiple_mappings_in_parallel() { // Arrange var mappingEvaluator = MockRepository.GenerateStub <IMappingEvaluator>(); var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>(); var postDeployAction = MockRepository.GenerateStub <IPostDeployAction>(); var buildDetail = new BuildDetail(); var mappings = new[] { new Mapping(), new Mapping() }; mappingEvaluator.Stub(o => o.DoesMappingApply(null, null, null)) .IgnoreArguments() .Return(true); var statusChanged = new BuildStatusChangeEvent { StatusChange = new Change() }; int threadCount; using (var mappingExecutor = new ThreadCountingMappingExecutor()) { Func <IMappingExecutor> executorFactory = () => mappingExecutor; var mappingProcessor = new MappingProcessor(mappingEvaluator, deploymentEventRecorder, executorFactory); // Act mappingProcessor.ProcessMappings(mappings, statusChanged, buildDetail, postDeployAction, 0); Thread.Sleep(250); threadCount = mappingExecutor.ConcurrentThreadCount; mappingExecutor.WaitHandle.Set(); } // Assert Assert.IsTrue(threadCount > 1); }
public override void RunCustomNewProjectLogic(ArchAngel.Interfaces.ProviderInfo theProviderInfo, IUserInteractor userInteractor) { //if (theProviderInfo is ArchAngel.Providers.EntityModel.ProviderInfo) //{ // ArchAngel.Providers.EntityModel.ProviderInfo providerInfo = (ArchAngel.Providers.EntityModel.ProviderInfo)theProviderInfo; // providerInfo.Clear(); // return; //} try { log.Debug("Loading project..."); userInteractor.UpdateWaitScreen("Loading project..."); NHibernateHelper.ProviderInfo providerInfo = (NHibernateHelper.ProviderInfo)theProviderInfo; providerInfo.Clear(); log.Debug("Loading database..."); Database database = DatabaseLoader.LoadDatabase(DatabaseLoader.DatabaseObjectsToFetch, null); DatabaseProcessor dbProcessor = new DatabaseProcessor(); dbProcessor.LogErrors = true; log.Debug("Creating relationships..."); dbProcessor.CreateRelationships(database); if (dbProcessor.Errors.Count > 0) { log.Debug("Database errors exist..." + dbProcessor.Errors.Count.ToString()); UI.FormErrors form = new UI.FormErrors("<b><font color='Red'>Note:</font></b> Database problems exist. Please <b>fix</b> these problems (or <b>omit the tables</b> in question) before trying again.", dbProcessor.Errors); form.ShowDialog(); return; } log.Debug("Creating 1 to 1 mappings..."); var mappingSet = new MappingProcessor(new OneToOneEntityProcessor()) .CreateOneToOneMapping(database, this.TablePrefixes, this.ColumnPrefixes, this.TableSuffixes, this.ColumnSuffixes); foreach (var entity in mappingSet.EntitySet.Entities) { ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.ExistingPropertyNames = new List <string>(); foreach (Property prop in entity.Properties) { IColumn mappedCol = prop.MappedColumn(); ArchAngel.Interfaces.Scripting.NHibernate.Model.IColumn scriptCol = new Interfaces.Scripting.NHibernate.Model.IColumn() { IsNullable = mappedCol.IsNullable, //IsText = Length = mappedCol.Size, Name = mappedCol.Name, ScriptObject = mappedCol, Type = mappedCol.OriginalDataType }; prop.Name = ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.GetPropertyName(scriptCol); } } providerInfo.EntityProviderInfo.MappingSet = mappingSet; ///////////////////////////////// providerInfo.EntityProviderInfo.Engine.AddModule(new NHibernateProjectLoaderModule(database)); // Then run the validation rules log.Debug("Validating model..."); userInteractor.UpdateWaitScreen("Validating model..."); //var rulesEngine = new ValidationRulesEngine(mappingSet); //rulesEngine.AddModule(new NHibernateProjectLoaderModule(database)); log.Debug("Running validation rules..."); providerInfo.EntityProviderInfo.RunValidationRules(); //rulesEngine); } //catch (Exception ex) //{ // MessageBox. //} finally { log.Debug("Removing wait screen..."); userInteractor.RemoveWaitScreen(); } }
private void Save() { if (RelatedTable == null) { MessageBox.Show(this, "You need to select a foreign table.", "Foreign table missing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (RelatedColumnCombos.Count == 0) { MessageBox.Show(this, "No columns selected.", "Columns missing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } bool mainColsAreUnique; // = checkBoxPrimaryUnique.Checked ? true : ColumnsAreUnique(GetMainColumns()); bool relatedColsAreUnique; // = checkBoxForeignUnique.Checked ? true : ColumnsAreUnique(GetRelatedColumns()); // Check if the Main columns are unique if (checkBoxPrimaryUnique.Checked) { mainColsAreUnique = true; } else { List <Column> mainColumns = GetMainColumns(); bool selectedColsContainAllPrimaryCols = MainTable.ColumnsInPrimaryKey.Count() > 0; foreach (var col in MainTable.ColumnsInPrimaryKey) { if (!mainColumns.Contains(col)) { selectedColsContainAllPrimaryCols = false; break; } } if (selectedColsContainAllPrimaryCols) { mainColsAreUnique = true; } else { mainColsAreUnique = ColumnsAreUnique(mainColumns); } } // Check if the Realted columns are unique if (checkBoxForeignUnique.Checked) { relatedColsAreUnique = true; } else { List <Column> relatedColumns = GetRelatedColumns(); bool selectedColsContainAllPrimaryCols = RelatedTable.ColumnsInPrimaryKey.Count() > 0; foreach (var col in RelatedTable.ColumnsInPrimaryKey) { if (!relatedColumns.Contains(col)) { selectedColsContainAllPrimaryCols = false; break; } } if (selectedColsContainAllPrimaryCols) { relatedColsAreUnique = true; } else { relatedColsAreUnique = ColumnsAreUnique(relatedColumns); } } IKey mainTableKey = null; foreach (var key in MainTable.Keys) { if (MainColumnCombos.Count == key.Columns.Count) { bool keyMatches = true; foreach (var combo in MainColumnCombos) { if (!key.Columns.Contains((Column)((DisplayItem)combo.SelectedItem).Value)) { keyMatches = false; break; } } if (keyMatches) { // Key is found mainTableKey = key; break; } } } IKey relatedTableKey = null; foreach (var key in RelatedTable.Keys) { if (RelatedColumnCombos.Count == key.Columns.Count) { bool keyMatches = true; foreach (var combo in RelatedColumnCombos) { // Can't have two primary keys, but there might be a foreign key on the same columns, so skip for now. Otherwise we'll need to create a new foreign key anyway. if (!key.Columns.Contains((Column)((DisplayItem)combo.SelectedItem).Value) && !((mainTableKey != null && mainTableKey.Keytype == DatabaseKeyType.Primary) && key.Keytype == DatabaseKeyType.Primary)) { keyMatches = false; break; } } if (keyMatches) { // Key is found relatedTableKey = key; break; } } } if (mainTableKey != null && relatedTableKey != null) { // Both keys exist, so let's check whether this relationship already exists foreach (var relationship in MainTable.Relationships) { if ((relationship.PrimaryKey == mainTableKey && relationship.ForeignKey == relatedTableKey) || (relationship.PrimaryKey == relatedTableKey && relationship.ForeignKey == mainTableKey)) { MessageBox.Show(this, "This relationship already exists.", "Relationship exists", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } } if (mainTableKey != null && relatedTableKey != null && (mainTableKey.Keytype == DatabaseKeyType.Primary && relatedTableKey.Keytype == DatabaseKeyType.Primary)) { // A relationship can't have two primary keys, so we'll need to create a new 'foreign-key' relatedTableKey = null; } RelatedTable = (Table)comboBoxForeignTable.SelectedItem; if (mainTableKey == null) { mainTableKey = new Key { Name = string.Format("FK_{0}_{1}", MainTable.Name, RelatedTable.Name).GetNextName(MainTable.Relationships.Select(r => r.Name).ToList()), IsUserDefined = true, Keytype = DatabaseKeyType.Foreign, Parent = MainTable, IsUnique = mainColsAreUnique }; foreach (Column column in GetMainColumns()) { mainTableKey.AddColumn(column.Name); } } if (relatedTableKey == null) { relatedTableKey = new Key { Name = string.Format("FK_{0}_{1}", RelatedTable.Name, MainTable.Name).GetNextName(RelatedTable.Relationships.Select(r => r.Name).ToList()), IsUserDefined = true, Keytype = DatabaseKeyType.Foreign, Parent = RelatedTable, IsUnique = relatedColsAreUnique }; foreach (Column column in GetRelatedColumns()) { relatedTableKey.AddColumn(column.Name); } } mainTableKey.ReferencedKey = relatedTableKey; relatedTableKey.ReferencedKey = mainTableKey; if (Relationship == null) { Relationship newRelationship = new RelationshipImpl(); newRelationship.Database = MainTable.Database; newRelationship.IsUserDefined = true; newRelationship.Schema = MainTable.Schema; if (relatedTableKey.Keytype == DatabaseKeyType.Primary) { newRelationship.AddThisTo(RelatedTable, MainTable); newRelationship.Name = string.Format("FK_NH_{0}_{1}", RelatedTable, MainTable).GetNextName(RelatedTable.Relationships.Select(r => r.Name).ToList()); //newRelationship.PrimaryCardinality = mainColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; //newRelationship.ForeignCardinality = relatedColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; newRelationship.ForeignCardinality = mainColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; newRelationship.PrimaryCardinality = relatedColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; newRelationship.PrimaryKey = relatedTableKey; newRelationship.ForeignKey = mainTableKey; if (!newRelationship.PrimaryTable.Keys.Contains(relatedTableKey)) { newRelationship.PrimaryTable.AddKey(relatedTableKey); } if (!newRelationship.ForeignTable.Keys.Contains(mainTableKey)) { newRelationship.ForeignTable.AddKey(mainTableKey); } } else { newRelationship.AddThisTo(MainTable, RelatedTable); newRelationship.Name = string.Format("FK_NH_{0}_{1}", MainTable, RelatedTable); //newRelationship.PrimaryCardinality = mainColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; //newRelationship.ForeignCardinality = relatedColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; newRelationship.ForeignCardinality = mainColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; newRelationship.PrimaryCardinality = relatedColsAreUnique ? ArchAngel.Interfaces.Cardinality.One : ArchAngel.Interfaces.Cardinality.Many; newRelationship.PrimaryKey = mainTableKey; newRelationship.ForeignKey = relatedTableKey; if (!newRelationship.PrimaryTable.Keys.Contains(mainTableKey)) { newRelationship.PrimaryTable.AddKey(mainTableKey); } if (!newRelationship.ForeignTable.Keys.Contains(relatedTableKey)) { newRelationship.ForeignTable.AddKey(relatedTableKey); } } //MainTable.Database.AddRelationship(newRelationship); OneToOneEntityProcessor proc = new OneToOneEntityProcessor(MainTable.Database.MappingSet.EntitySet.Entities.Select(e => e.Name)); //List<ITable> tables = new List<ITable>(); //tables.Add((ITable)MainTable); //proc.CreateOneToOneMappingsFor(tables, MainTable.Database.MappingSet); //proc.CreateReference(newRelationship, MainTable.Database.MappingSet.EntitySet); MappingProcessor.ProcessRelationshipInternal(MainTable.Database.MappingSet, newRelationship, proc); if (RelationshipAdded != null) { RelationshipAdded(null, null); } } this.Visible = false; }