/// <summary> /// Creates one Entity per database table, and one Reference per database relationship. /// It then looks for association tables and maps them correctly. /// </summary> /// <param name="database"></param> /// <returns></returns> public MappingSet CreateOneToOneMapping(IDatabase database, List<string> tablePrefixes, List<string> columnPrefixes, List<string> tableSuffixes, List<string> columnSuffixes) { EntitySet entitySet = new EntitySetImpl(); MappingSet set = new MappingSetImpl(database, entitySet); set.TablePrefixes = tablePrefixes; set.ColumnPrefixes = columnPrefixes; set.TableSuffixes = tableSuffixes; set.ColumnSuffixes = columnSuffixes; entitySet.MappingSet = set; ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.ExistingEntityNames = new List<string>(); ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.TablePrefixes = (List<string>)tablePrefixes; ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.ColumnPrefixes = (List<string>)columnPrefixes; ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.TableSuffixes = (List<string>)tableSuffixes; ArchAngel.Interfaces.ProjectOptions.ModelScripts.Scripts.ColumnSuffixes = (List<string>)columnSuffixes; foreach (var table in database.Tables) ProcessTable(set, table); foreach (var view in database.Views) ProcessTable(set, view); foreach (var relationship in database.Relationships) ProcessRelationship(set, relationship); CreateManyToManyMappings(entitySet.Entities.ToList(), entitySet); return set; }
public void It_Should_Serialise_To_This() { const string expectedXML = BasicEntitySetXml; EntitySet entitySet = new EntitySetImpl(); string outputXML = new EntitySetSerialisationScheme().SerialiseEntitySet(entitySet); outputXML = XmlSqueezer.RemoveWhitespaceBetweenElements(outputXML); Assert.That(outputXML, Is.EqualTo(expectedXML)); }
public MappingSetImpl() { Database = new Database("", ArchAngel.Providers.EntityModel.Controller.DatabaseLayer.DatabaseTypes.Unknown); EntitySet = new EntitySetImpl(); EventAggregator = new EventAggregator(this); TablePrefixes = new List<string>(); ColumnPrefixes = new List<string>(); TableSuffixes = new List<string>(); ColumnSuffixes = new List<string>(); }
public void It_Should_Serialise_To_This() { const string expectedXML = FullEntitySetXml; EntitySet entitySet = new EntitySetImpl(); entitySet.AddEntity(When_Serialising_An_Empty_Entity.GetEntity()); entitySet.AddReference(When_Serialising_An_Reference_With_All_Fields_Set.GetReference()); string outputXML = new EntitySetSerialisationScheme().SerialiseEntitySet(entitySet); outputXML = XmlSqueezer.RemoveWhitespaceBetweenElements(outputXML); Assert.That(outputXML, Is.EqualTo(expectedXML)); }
public void It_Should_Deserialise_To_This() { var xml = "<MappingSet />"; Database database = new Database("DB1"); EntitySet entitySet = new EntitySetImpl(); MappingSet set = new MappingSetDeserialisationScheme().DeserialiseMappingSet(xml.GetXmlDocRoot(), database, entitySet); Assert.That(set.EntitySet, Is.SameAs(entitySet)); Assert.That(set.Database, Is.SameAs(database)); }
public void It_Should_Create_This() { const string xml = When_Serialising_A_ComponentSpecification.BasicSpecXml; var parentEntity = new EntityImpl("Entity1"); var entitySet = new EntitySetImpl(); entitySet.AddEntity(parentEntity); ComponentSpecification spec = new EntitySetDeserialisationScheme().DeserialiseComponentSpecification(xml.GetXmlDocRoot(), entitySet); Assert.That(spec.Name, Is.EqualTo("Address")); Assert.That(spec.Properties, Has.Count(1)); Assert.That(spec.Properties[0].Name, Is.EqualTo("Street")); Assert.That(spec.ImplementedComponents, Has.Count(1)); Assert.That(spec.ImplementedComponents[0].ParentEntity, Is.SameAs(parentEntity)); }
public void Setup() { database = new Database("Db1"); entitySet = new EntitySetImpl(); table = new Table("Table1"); table.AddColumn(new Column("Column1")); entity1 = new EntityImpl("Entity1"); entity1.AddProperty(new PropertyImpl("Property1")); entity2 = new EntityImpl("Entity2"); entity2.AddProperty(new PropertyImpl("Property2")); var reference = entity1.CreateReferenceTo(entity2); reference.Identifier = new Guid("11111111-1111-1111-1111-111111111111"); reference.End1Name = "end1"; reference.End2Name = "end2"; entitySet.AddReference(reference); database.AddTable(table); entitySet.AddEntity(entity1); }
public void All_Its_References_Are_Deleted() { EntitySet entitySet = new EntitySetImpl(); Entity entity1 = new EntityImpl("Table1"); Entity entity2 = new EntityImpl("Table2"); entitySet.AddEntity(entity1); entitySet.AddEntity(entity2); entity1.CreateReferenceTo(entity2); Assert.That(entity2.References.Count(), Is.EqualTo(1)); Assert.That(entitySet.Entities.Count(), Is.EqualTo(2)); entity1.DeleteSelf(); Assert.That(entitySet.Entities.Count(), Is.EqualTo(1)); Assert.That(entitySet.Entities.ElementAt(0), Is.SameAs(entity2)); Assert.That(entity2.References.Count, Is.EqualTo(0)); }
public void SetUp() { mainPanel = MockRepository.GenerateStub<IMainPanel>(); form = MockRepository.GenerateMock<IEntityForm>(); ms = MockRepository.GenerateStub<MappingSet>(); Property property = new PropertyImpl("Prop1"); EntityKey key = new EntityKeyImpl(); entity = new EntityImpl("Entity1") { Key = key }; entity.AddProperty(property); key.AddProperty(property); mapping = new MappingImpl(); form.Stub(f => f.Mappings).Return(new List<Mapping> { mapping }); EntitySet es = new EntitySetImpl(); es.AddEntity(entity); ms.EntitySet = es; es.MappingSet = ms; ms.Stub(m => m.GetMappingsContaining(entity)).Return(new List<Mapping>()); var presenter = new EntityPresenter(mainPanel, form); presenter.AttachToModel(entity); }
public void It_Should_Deserialise_To_This() { const string xml = Specs_For_Serialisation_Of_Entities.When_Serialising_An_Entity_With_All_Fields_Set.FullEntityXml; var scheme = new EntitySetDeserialisationScheme(); XmlNode root = xml.GetXmlDocRoot(); Entity entity = scheme.DeserialiseEntity(root); Entity entity2 = new EntityImpl("Entity2"); EntitySet entitySet = new EntitySetImpl(); entitySet.AddEntity(entity); entitySet.AddEntity(entity2); scheme.PostProcessEntity(entitySet, null, entity, root); Assert.That(entity.Name, Is.EqualTo("Entity1")); Assert.That(entity.Key, Is.Not.Null); // Don't both checking the contents of the Properties collection, // as long as it had something added to it. The tests on deserialising // properties will catch most bugs. Assert.That(entity.Properties, Is.Not.Empty); Assert.That(entity.Discriminator, Is.Not.Null, "The deserialisation of the Discriminator should happen in the post process step."); Assert.That(entity.HasParent, Is.True); Assert.That(entity.Parent, Is.SameAs(entity2)); }
public EntitySet DeserialiseEntitySet(XmlNode node, IDatabase database) { if (string.IsNullOrEmpty(node.InnerXml)) { return(new EntitySetImpl()); } var set = new EntitySetImpl(); Version = int.Parse(node.Attributes["Version"].Value); Dictionary <XmlNode, Entity> entityNodes = new Dictionary <XmlNode, Entity>(); var entityNodeList = node.SelectNodes("Entities/Entity"); if (entityNodeList != null) { foreach (XmlNode entityNode in entityNodeList) { Entity entity = DeserialiseEntity(entityNode); entityNodes[entityNode] = entity; set.AddEntity(entity); } } ProcessScriptBase(set, node); // Second processing pass if (entityNodeList != null) { foreach (XmlNode entityNode in entityNodeList) { PostProcessEntity(set, database, entityNodes[entityNode], entityNode); } } var refNodes = node.SelectNodes("References/Reference"); if (refNodes != null) { foreach (XmlNode referenceNode in refNodes) { Reference reference = DeserialiseReference(referenceNode, set); if (reference.Entity1 != null && reference.Entity2 != null) { reference.Entity1.AddReference(reference); if (reference.Entity1 != reference.Entity2) { reference.Entity2.AddReference(reference); } set.AddReference(reference); } } } var componentSpecNodes = node.SelectNodes("ComponentSpecifications/ComponentSpecification"); if (componentSpecNodes != null) { foreach (XmlNode specNode in componentSpecNodes) { DeserialiseComponentSpecification(specNode, set); } } if (entityNodeList != null) { foreach (XmlNode entityNode in entityNodeList) { var entity = entityNodes[entityNode]; // Key requires Components to have been deserialised first. entity.Key = DeserialiseKey(entityNode.SelectSingleNode("Key"), entity); } } return(set); }
public void Triggering_The_CreateTable_Event_Creates_A_Table_With_One_To_One_Mapping_To_The_Entity() { IMainPanel mainPanel = MockRepository.GenerateStub<IMainPanel>(); IEntityForm form = MockRepository.GenerateMock<IEntityForm>(); Entity entity = new EntityImpl("Entity1"); entity.AddProperty(new PropertyImpl("Property1"){Type = "System.Int32"}); EntitySet entitySet = new EntitySetImpl(); entitySet.AddEntity(entity); IDatabase database = new Database("DB1"); new MappingSetImpl(database, entitySet); var presenter = new EntityPresenter(mainPanel, form); presenter.AttachToModel(entity); form.Raise(f => f.CreateNewTableFromEntity += null, form, new EventArgs()); Assert.That(database.Tables.Count, Is.EqualTo(1)); ITable table = database.Tables[0]; Assert.That(table.Name, Is.EqualTo("Entity1s")); Assert.That(table.Columns.Count, Is.EqualTo(1)); Assert.That(table.Columns[0].Name, Is.EqualTo("Property1")); }
public void The_Presenter_Fills_In_The_Form() { IMainPanel mainPanel = MockRepository.GenerateStub<IMainPanel>(); IEntityForm form = MockRepository.GenerateMock<IEntityForm>(); form.Expect(f => f.Mappings = null) .IgnoreArguments() .WhenCalled(action => Assert.That(((IEnumerable<Mapping>) action.Arguments[0]).Count(), Is.EqualTo(0))); form.Expect(f => f.SetAvailableTables(null)) .IgnoreArguments() .WhenCalled(action => Assert.That(((IEnumerable<ITable>)action.Arguments[0]).Count(), Is.EqualTo(0))); form.Expect(f => f.SetProperties(null)) .IgnoreArguments() .WhenCalled(action => Assert.That(((IEnumerable<Property>)action.Arguments[0]).Count(), Is.EqualTo(1))); form.Expect(f => f.SetAvailableEntities(null)) .IgnoreArguments() .WhenCalled(action => Assert.That(((IEnumerable<Entity>)action.Arguments[0]).Count(), Is.EqualTo(2))); form.Expect(f => f.SetChildEntities(null)) .IgnoreArguments() .WhenCalled(action => Assert.That(((IEnumerable<Entity>)action.Arguments[0]).Count(), Is.EqualTo(1))); Entity parentEntity = new EntityImpl("Parent"); Entity childEntity = new EntityImpl("Child"); Property property = new PropertyImpl("Prop1"); EntityKey key = new EntityKeyImpl(); Entity entity = new EntityImpl("Entity1") { Key = key }; entity.Parent = parentEntity; entity.AddChild(childEntity); entity.AddProperty(property); key.AddProperty(property); EntitySet es = new EntitySetImpl(); es.AddEntity(parentEntity); es.AddEntity(entity); es.AddEntity(childEntity); MappingSet ms = new MappingSetImpl(); ms.EntitySet = es; var presenter = new EntityPresenter(mainPanel, form); presenter.AttachToModel(entity); form.AssertWasCalled(f => f.EntityName = entity.Name); form.AssertWasCalled(f => f.Discriminator = entity.Discriminator); form.AssertWasCalled(f => f.ParentEntity = entity.Parent); form.AssertWasCalled(f => f.SetVirtualProperties(entity.Ex)); form.VerifyAllExpectations(); }
public static Reference GetReference() { Reference reference = new ReferenceImpl(); var mappingSet = new MappingSetImpl(); var entitySet = new EntitySetImpl(); entitySet.AddReference(reference); mappingSet.EntitySet = entitySet; return reference; }
public MappingSet GetEntities(IEnumerable<string> hbmFiles, ParseResults parseResults, IDatabase database) { EntitySet entities = new EntitySetImpl(); MappingSet mappingSet = new MappingSetImpl(database, entities); Dictionary<Class, ComponentSpecification> existingComponentSpecs = new Dictionary<Class, ComponentSpecification>(); List<hibernatemapping> mappingFiles = new List<hibernatemapping>(); List<ValidationEventArgs> mappingErrors = new List<ValidationEventArgs>(); List<NHibernateLoaderException.HbmXmlFile> errorFiles = new List<NHibernateLoaderException.HbmXmlFile>(); GetMappingFiles(hbmFiles, mappingFiles, mappingErrors, errorFiles); if (errorFiles.Count > 0) throw new NHibernateLoaderException(string.Format("Unsupported Elements found in HBM file."), errorFiles, mappingErrors); foreach (var hm in mappingFiles) { ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("AutoImport", hm.autoimport); ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("DefaultAccess", (TopLevelAccessTypes)Enum.Parse(typeof(TopLevelAccessTypes), hm.defaultaccess)); ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("DefaultCascade", (TopLevelCascadeTypes)Enum.Parse(typeof(TopLevelCascadeTypes), hm.defaultcascade.Replace("-", "_"), true)); ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("DefaultLazy", hm.defaultlazy); ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("ProjectNamespace", hm.@namespace); foreach (var hClass in hm.Classes()) { Entity newEntity; string @namespace; string name; if (IsNameFullyQualified(hClass.name, out @namespace, out name)) { newEntity = new EntityImpl(name); string currentNamespace = ArchAngel.Interfaces.SharedData.CurrentProject.GetUserOption("ProjectNamespace") == null ? "" : ArchAngel.Interfaces.SharedData.CurrentProject.GetUserOption("ProjectNamespace").ToString(); if (string.IsNullOrWhiteSpace(currentNamespace)) ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("ProjectNamespace", @namespace); } else newEntity = new EntityImpl(hClass.name); if (entities.GetEntity(newEntity.Name) != null) // This entity has already been added - the existing project probably has both HBM XML and Fluent mappings. continue; if (hClass.lazySpecified) newEntity.SetEntityLazy(hClass.lazy); else newEntity.SetEntityLazy(hm.defaultlazy); newEntity.SetEntitySqlWhereClause(hClass.where); newEntity.SetEntityDynamicInsert(hClass.dynamicinsert); newEntity.SetEntityDynamicUpdate(hClass.dynamicupdate); newEntity.SetEntityMutable(hClass.mutable); newEntity.SetEntityOptimisticLock((OptimisticLockModes)Enum.Parse(typeof(OptimisticLockModes), hClass.optimisticlock.ToString(), true)); newEntity.SetEntityProxy(hClass.proxy); newEntity.SetEntitySelectBeforeUpdate(hClass.selectbeforeupdate); newEntity.SetEntityBatchSize(hClass.batchsize); if (hClass.abstractSpecified) newEntity.IsAbstract = hClass.@abstract; newEntity.SetEntityPersister(hClass.persister); Mapping mapping = null; if (!newEntity.IsAbstract) mapping = CreateMappingFor(newEntity, hClass, database, hm.schema); entities.AddEntity(newEntity); if (mapping != null) mappingSet.AddMapping(mapping); #region Cache if (hClass.cache != null) { newEntity.Cache = new Cache(); switch (hClass.cache.include) { case cacheInclude.all: newEntity.Cache.Include = Cache.IncludeTypes.All; break; case cacheInclude.nonlazy: newEntity.Cache.Include = Cache.IncludeTypes.Non_Lazy; break; default: throw new NotImplementedException("Unexpected cache include value:" + hClass.cache.include.ToString()); } newEntity.Cache.Region = hClass.cache.region; switch (hClass.cache.usage) { case cacheUsage.nonstrictreadwrite: newEntity.Cache.Usage = Cache.UsageTypes.NonStrict_Read_Write; break; case cacheUsage.@readonly: newEntity.Cache.Usage = Cache.UsageTypes.Read_Only; break; case cacheUsage.readwrite: newEntity.Cache.Usage = Cache.UsageTypes.Read_Write; break; case cacheUsage.transactional: newEntity.Cache.Usage = Cache.UsageTypes.Transactional; break; default: throw new NotImplementedException("Unexpected cache usage value:" + hClass.cache.usage.ToString()); } } #endregion #region Discriminator if (hClass.discriminator != null) { ArchAngel.Providers.EntityModel.Model.EntityLayer.Discriminator d = new Discriminator() { AllowNull = !hClass.discriminator.notnull, ColumnName = hClass.discriminator.column, DiscriminatorType = string.IsNullOrWhiteSpace(hClass.discriminator.column) ? ArchAngel.Providers.EntityModel.Model.Enums.DiscriminatorTypes.Formula : ArchAngel.Providers.EntityModel.Model.Enums.DiscriminatorTypes.Column, Force = hClass.discriminator.force, Formula = hClass.discriminator.formula, Insert = hClass.discriminator.insert }; newEntity.Discriminator = d; newEntity.DiscriminatorValue = d.DiscriminatorType == Enums.DiscriminatorTypes.Column ? hClass.discriminator.column : hClass.discriminator.formula; //string columnName = hClass.discriminator.column; //throw new NotImplementedException("TODO: fixup discriminator stuff"); //Grouping g = new AndGrouping(); //IColumn column = newEntity.MappedTables().First().Columns.Single(c => c.Name == columnName); //ArchAngel.Providers.EntityModel.Model.DatabaseLayer.Discrimination.Operator op = ArchAngel.Providers.EntityModel.Model.DatabaseLayer.Discrimination.Operator.Equal; //string discriminatorValue = string.IsNullOrWhiteSpace(hClass.discriminatorvalue) ? "" : hClass.discriminatorvalue; //ExpressionValue value = new ExpressionValueImpl(discriminatorValue); //if (column != null && op != null && value != null) // g.AddCondition(new ConditionImpl(column, op, value)); //if (newEntity.Discriminator == null) // newEntity.Discriminator = new DiscriminatorImpl(); //newEntity.Discriminator.RootGrouping = g; } #endregion /////////////////////////////////////////////////// //foreach (var hProperty in hClass.Properties()) //{ // var property = CreateProperty(newEntity, mapping, hProperty); // SetPropertyInfoFromParsedCode(property, parseResults, hm.@namespace, mapping.FromTable.Schema, hClass.name); //} //foreach (var hComponent in hClass.Components()) //{ // ProcessComponent(hComponent, newEntity, mapping.FromTable, existingComponentSpecs, hm.@namespace, parseResults); //} /////////////////////////////////////////////////// id hId = hClass.Id(); if (hId != null) { var idProperty = CreateProperty(newEntity, mapping, hId); string schema = mapping == null ? null : mapping.FromTable.Schema; SetPropertyInfoFromParsedCode(idProperty, parseResults, hm.@namespace, schema, hClass.name); idProperty.IsKeyProperty = true; if (hId.generator == null) newEntity.Generator.ClassName = NHibernateHelper.MappingFiles.Version_2_2.GeneratorTypes.assigned.ToString(); else { newEntity.Generator.ClassName = hId.generator.@class; if (hId.generator.param != null) foreach (var param in hId.generator.param) newEntity.Generator.Parameters.Add(new EntityGenerator.Parameter(param.name, param.Text[0])); } } else { compositeid hCompId = hClass.CompositeId(); if (hCompId != null) { // Check if this is a component. If so, we need to do some additional processing. if (!string.IsNullOrEmpty(hCompId.@class)) { bool keyResult = ProcessComponentKey(hCompId, newEntity, mapping.FromTable, existingComponentSpecs, hm.@namespace, parseResults); if (keyResult == false) { // Fallback to composite key generation, log failure. ProcessCompositeKey(hm, hClass, newEntity, mapping, parseResults, hCompId); log.ErrorFormat("Could not create a component for composite key {0}", hCompId.name); } } else { // It is not a component. ProcessCompositeKey(hm, hClass, newEntity, mapping, parseResults, hCompId); } } } foreach (var hProperty in hClass.Properties()) { var property = CreateProperty(newEntity, mapping, hProperty); string schema = mapping == null ? null : mapping.FromTable.Schema; SetPropertyInfoFromParsedCode(property, parseResults, hm.@namespace, schema, hClass.name); property.SetPropertyFormula(hProperty.formula); property.SetPropertyGenerated((ArchAngel.Interfaces.NHibernateEnums.PropertyGeneratedTypes)Enum.Parse(typeof(ArchAngel.Interfaces.NHibernateEnums.PropertyGeneratedTypes), hProperty.generated.ToString())); if (hProperty.insertSpecified) property.SetPropertyInsert(hProperty.insert); property.SetPropertyOptimisticLock(hProperty.optimisticlock); if (hProperty.updateSpecified) property.SetPropertyUpdate(hProperty.update); } foreach (var hComponent in hClass.Components()) ProcessComponent(hComponent, newEntity, mapping.FromTable, existingComponentSpecs, hm.@namespace, parseResults); if (hClass.Version() != null) { // Create a property from the version node. ProcessVersionProperty(hm, hClass, newEntity, mapping, parseResults); } ProcessSubclasses(mappingSet, database, entities, newEntity, hClass.SubClasses(), hClass.schema.UnBackTick(), hClass.table.UnBackTick(), parseResults, hm.@namespace); ProcessJoinedSubclasses(mappingSet, database, entities, newEntity, hClass.JoinedSubClasses(), parseResults, hm.@namespace); ProcessUnionSubclasses(hClass, mappingSet, database, entities, newEntity, hClass.UnionSubClasses(), parseResults, hm.@namespace); } } List<string> processedClassNames = new List<string>(); // Second pass, to add missing foreign key columns and properties foreach (var hm in mappingFiles) { foreach (var hClass in hm.Classes()) { string className; if (!IsNameFullyQualified(hClass.name, out className)) className = hClass.name; if (processedClassNames.Contains(className)) continue; processedClassNames.Add(className); foreach (var hManyToOne in hClass.ManyToOnes()) { var fkColumnNames = ReferenceLoader.GetColumnNames(hManyToOne.column, hManyToOne.Columns()).ToList(); Entity entity = entities.Entities.Single(e => e.Name == className); bool found = false; foreach (var prop in entity.Properties) { IColumn mappedColumn = prop.MappedColumn(); if (mappedColumn != null) { string mappedColumnName = null; if (hManyToOne.column != null) mappedColumnName = hManyToOne.column; else if (hManyToOne.Columns().Count > 0) mappedColumnName = hManyToOne.Columns()[0].name; if (!string.IsNullOrEmpty(mappedColumnName) && mappedColumn.Name == mappedColumnName.UnBackTick()) { found = true; break; } } //else //{ // string gfh2 = string.Format("{0}.{1}", hClass.name, hManyToOne.column1.UnBackTick()); // gfh2 = ""; // // TODO: we should create a column for this property // Entity referencedEntity = entities.Entities.Single(e => e.Name == hManyToOne.@class); // Property referencedProperty = referencedEntity.Key.Properties.ElementAt(0); // IColumn tempColumn = referencedProperty.MappedColumn(); // IColumn newColumn = ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor.CreateColumnFromProperty(referencedProperty); // newColumn.Name = hManyToOne.column1.UnBackTick(); // ITable table = entity.MappedTables().First(); // table.AddColumn(newColumn); // prop.SetMappedColumn(newColumn); //} } if (!found) { string name; string manyToOneClassName = hManyToOne.@class != null ? hManyToOne.@class : hManyToOne.name; IsNameFullyQualified(manyToOneClassName, out name); Entity referencedEntity = entities.Entities.Single(e => e.Name == name); Property referencedProperty = referencedEntity.Key.Properties.ElementAt(0); IColumn newColumn = null; foreach (Table t in entity.MappedTables()) { string colName = null; if (hManyToOne.column != null) colName = hManyToOne.column; else if (hManyToOne.Columns().Count > 0) colName = hManyToOne.Columns()[0].name; newColumn = t.Columns.SingleOrDefault(c => !string.IsNullOrEmpty(colName) && c.Name == colName.UnBackTick()); if (newColumn != null) break; } if (newColumn == null) { newColumn = ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor.CreateColumnFromProperty(referencedProperty); ITable table = entity.MappedTables().First(); table.AddColumn(newColumn); } Property newProperty = ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor.CreatePropertyFromColumn(newColumn); //entity.Properties.SingleOrDefault(p => p.MappedColumn() == entity.AddProperty(newProperty); } //ITable table = entity.MappedTables().First(); } } } // Second pass, for references. var refProcessor = new ReferenceLoader(); refProcessor.ProcessReferences(mappingFiles, mappingSet); processedClassNames.Clear(); // Second pass, to add missing foreign key columns and properties foreach (var hm in mappingFiles) { foreach (var hClass in hm.Classes()) { string className; IsNameFullyQualified(hClass.name, out className); if (processedClassNames.Contains(className)) continue; processedClassNames.Add(className); foreach (var hManyToOne in hClass.ManyToOnes()) { var fkColumnNames = ReferenceLoader.GetColumnNames(hManyToOne.column, hManyToOne.Columns()).ToList(); Entity entity = entities.Entities.Single(e => e.Name == className); foreach (var prop in entity.Properties) { IColumn mappedColumn = prop.MappedColumn(); if (mappedColumn == null) { // TODO: we should create a column for this property string manyToOneClassName; IsNameFullyQualified(hManyToOne.@class, out manyToOneClassName); Entity referencedEntity = entities.Entities.Single(e => e.Name == manyToOneClassName); Property referencedProperty = referencedEntity.Key.Properties.ElementAt(0); IColumn tempColumn = referencedProperty.MappedColumn(); IColumn newColumn = ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor.CreateColumnFromProperty(referencedProperty); string newColName = ""; if (hManyToOne.column != null) newColName = hManyToOne.column; else if (hManyToOne.Columns().Count > 0) newColName = hManyToOne.Columns()[0].name; newColumn.Name = newColName.UnBackTick(); ITable table = entity.MappedTables().First(); if (table.Columns.Count(c => c.Name == newColumn.Name) == 0) table.AddColumn(newColumn); else newColumn = table.Columns.First(c => c.Name == newColumn.Name); prop.SetMappedColumn(newColumn); } } } } } return mappingSet; }
public static string UpdateNHibernateMappingFile(hibernatemapping hm, EntitySetImpl entitySet) { EntityMapper mapper = new EntityMapper(); foreach (var entity in entitySet.Entities) { var newClass = mapper.ProcessEntity(entity); hm.AddClass(newClass); } return hm.ToXml(); }
public void Triggering_The_RemoveEntity_Event_Removes_It_From_The_Model_And_Clears_The_Screen() { IMainPanel mainPanel = MockRepository.GenerateStub<IMainPanel>(); IEntityForm form = MockRepository.GenerateMock<IEntityForm>(); Entity entity = new EntityImpl("Entity1"); EntitySet entitySet = new EntitySetImpl(); entitySet.AddEntity(entity); var presenter = new EntityPresenter(mainPanel, form); presenter.AttachToModel(entity); form.Raise(f => f.RemoveEntity += null, form, new EventArgs()); Assert.That(entity.EntitySet, Is.Null); Assert.That(entitySet.Entities.Count(), Is.EqualTo(0)); // Assert that the main panel was instructed to clear the property grid. mainPanel.AssertWasCalled(m => m.ShowPropertyGrid(null)); }