public override void SetValue(TEntity entity, object value) { // The base.GetValue gets the entityReference<TEntity> class var entityCollection = base.GetValue(entity) as IEntitySource; // The The foreign key mapping comes from the value passed to this method IForeignKeyMapping foreignKeyMapping = (IForeignKeyMapping)value; var entityCollectionType = foreignKeyMapping.ForeignKeyProperty.DeclaringType; // TJT: Clean the EntitySet access up! var entitySetSource = _entityMapping.Model.DataContext.EntitySets[entityCollectionType]; var keyName = foreignKeyMapping.ForeignKeyProperty.Name; var keyValue = _entityMapping.GetColumn(entity, foreignKeyMapping.ReferenceKeyProperty.Name); if (!entityCollection.HasSource) { entityCollection.SetSource(entitySetSource, keyName, keyValue); } else { throw new InvalidOperationException("Entity reference source already set."); } }
public void GetColumn() { var mapping = new DataContextMapping <AccessNorthwind>(); mapping.Provider = new ProviderAttribute(typeof(AccessDbProvider)); var categoryMapping = new EntityMapping <Category>(); categoryMapping.TableAttribute = new TableAttribute { Name = "Categories" }; categoryMapping.Column(o => o.CategoryID, o => o.PrimaryKey()); categoryMapping.Column(o => o.CategoryName, o => o.NeverUpdateCheck()); categoryMapping.Association(o => o.Products, o => o.Keys("CategoryID", "CategoryID")); mapping.Add(categoryMapping); var mappingSource = new FluentMappingSource(o => mapping); //mappingSource.Add(mapping); var column = categoryMapping.GetColumn(o => o.CategoryID); Assert.IsTrue(column.IsPrimaryKey); }