public void Can_get_entity_builder_for_entity_type_name() { var model = new Model(); var modelBuilder = new BasicModelBuilder(model); var entityBuilder = modelBuilder.Entity(typeof(Customer).FullName); Assert.NotNull(entityBuilder); Assert.NotNull(model.FindEntityType(typeof(Customer).FullName)); }
public void Can_add_and_remove_entity_by_type() { var model = new Model(); Assert.Null(model.FindEntityType(typeof(Customer))); Assert.Null(model.RemoveEntityType(new EntityType(typeof(Customer), model))); var entityType = model.AddEntityType(typeof(Customer)); Assert.Equal(typeof(Customer), entityType.ClrType); Assert.NotNull(model.FindEntityType(typeof(Customer))); Assert.Same(model, entityType.Model); Assert.Same(entityType, model.GetOrAddEntityType(typeof(Customer))); Assert.Equal(new[] { entityType }, model.EntityTypes.ToArray()); Assert.Same(entityType, model.RemoveEntityType(entityType)); Assert.Null(model.RemoveEntityType(entityType)); Assert.Null(model.FindEntityType(typeof(Customer))); }
public virtual IModel ConstructRelationalModel() { var relationalModel = new Microsoft.Data.Entity.Metadata.Model(); foreach (var table in _tables.Values) { relationalModel.AddEntityType(table.Id); } foreach (var tc in _tableColumns.Values) { var entityType = relationalModel.FindEntityType(tc.TableId); if (entityType == null) { _logger.LogWarning( Strings.CannotFindTableForColumn(tc.Id, tc.TableId)); continue; } // IModel will not allow Properties to be created without a Type, so map to CLR type here. // This means if we come across a column with a SQL Server type which we can't map we will ignore it. // Note: foreign key properties appear just like any other property in the relational model. Type clrPropertyType; if (!SqlServerTypeMapping._sqlTypeToClrTypeMap.TryGetValue(tc.DataType, out clrPropertyType)) { _logger.LogWarning( Strings.CannotFindTypeMappingForColumn(tc.Id, tc.DataType)); continue; } if (tc.IsNullable) { clrPropertyType = clrPropertyType.MakeNullable(); } var relationalProperty = entityType.AddProperty(tc.Id, clrPropertyType, shadowProperty: true); _relationalColumnIdToRelationalPropertyMap[tc.Id] = relationalProperty; } return(relationalModel); }
public void Can_get_entity_by_name() { var model = new Model(); var entityType = model.GetOrAddEntityType(typeof(Customer).FullName); Assert.Same(entityType, model.GetEntityType(typeof(Customer).FullName)); Assert.Same(entityType, model.FindEntityType(typeof(Customer).FullName)); Assert.Null(model.FindEntityType(typeof(string))); Assert.Equal( Strings.EntityTypeNotFound("String"), Assert.Throws<ModelItemNotFoundException>(() => model.GetEntityType("String")).Message); }