/// <summary> /// The factory method for constructing the EntitySet object. /// </summary> /// <param name="name">The name of the EntitySet.</param> /// <param name="schema">The db schema. Can be null.</param> /// <param name="table">The db table. Can be null.</param> /// <param name="definingQuery"> /// The provider specific query that should be used to retrieve data for this EntitySet. Can be null. /// </param> /// <param name="entityType">The entity type of the entities that this entity set type contains.</param> /// <param name="metadataProperties"> /// Metadata properties that will be added to the newly created EntitySet. Can be null. /// </param> /// <returns>The EntitySet object.</returns> /// <exception cref="System.ArgumentException">Thrown if the name argument is null or empty string.</exception> /// <remarks>The newly created EntitySet will be read only.</remarks> public static EntitySet Create( string name, string schema, string table, string definingQuery, EntityType entityType, IEnumerable <MetadataProperty> metadataProperties) { Check.NotEmpty(name, "name"); Check.NotNull(entityType, "entityType"); var entitySet = new EntitySet(name, schema, table, definingQuery, entityType); if (metadataProperties != null) { entitySet.AddMetadataProperties(metadataProperties.ToList()); } entitySet.SetReadOnly(); return(entitySet); }
/// <summary> /// The factory method for constructing the EntitySet object. /// </summary> /// <param name="name">The name of the EntitySet.</param> /// <param name="schema">The db schema. Can be null.</param> /// <param name="table">The db table. Can be null.</param> /// <param name="definingQuery"> /// The provider specific query that should be used to retrieve data for this EntitySet. Can be null. /// </param> /// <param name="entityType">The entity type of the entities that this entity set type contains.</param> /// <param name="metadataProperties"> /// Metadata properties that will be added to the newly created EntitySet. Can be null. /// </param> /// <exception cref="System.ArgumentException">Thrown if the name argument is null or empty string.</exception> /// <notes>The newly created EntitySet will be read only.</notes> public static EntitySet Create( string name, string schema, string table, string definingQuery, EntityType entityType, IEnumerable<MetadataProperty> metadataProperties) { Check.NotEmpty(name, "name"); Check.NotNull(entityType, "entityType"); var entitySet = new EntitySet(name, schema, table, definingQuery, entityType); if (metadataProperties != null) { entitySet.AddMetadataProperties(metadataProperties.ToList()); } entitySet.SetReadOnly(); return entitySet; }
public void Executes_in_a_transaction_using_ExecutionStrategy() { var shaperMock = MockHelper.CreateShaperMock<object>(); shaperMock.Setup(m => m.GetEnumerator()).Returns( () => new DbEnumeratorShim<object>(((IEnumerable<object>)new[] { new object() }).GetEnumerator())); var objectResultMock = new Mock<ObjectResult<object>>(shaperMock.Object, null, null) { CallBase = true }; var entityType = new EntityType( "FakeEntityType", "FakeNamespace", DataSpace.CSpace, new[] { "key" }, new EdmMember[] { new EdmProperty("key") }); var entitySet = new EntitySet("FakeSet", "FakeSchema", "FakeTable", null, entityType); var entityContainer = new EntityContainer("FakeContainer", DataSpace.CSpace); entityContainer.AddEntitySetBase(entitySet); entitySet.ChangeEntityContainerWithoutCollectionFixup(entityContainer); entitySet.SetReadOnly(); var model = new EdmModel(DataSpace.CSpace); var omicMock = new Mock<DefaultObjectMappingItemCollection>(new EdmItemCollection(model), new ObjectItemCollection()) { CallBase = true }; var objectTypeMapping = new ObjectTypeMapping(entityType, entityType); objectTypeMapping.AddMemberMap( new ObjectPropertyMapping((EdmProperty)entityType.Members.First(), (EdmProperty)entityType.Members.First())); omicMock.Setup(m => m.GetMap(It.IsAny<GlobalItem>())).Returns(objectTypeMapping); var metadataWorkspaceMock = new Mock<MetadataWorkspace> { CallBase = true }; metadataWorkspaceMock.Setup(m => m.IsItemCollectionAlreadyRegistered(DataSpace.OSpace)).Returns(true); metadataWorkspaceMock.Setup(m => m.IsItemCollectionAlreadyRegistered(DataSpace.SSpace)).Returns(true); metadataWorkspaceMock.Setup(m => m.GetEntityContainer(It.IsAny<string>(), It.IsAny<DataSpace>())) .Returns(entityContainer); metadataWorkspaceMock.Setup(m => m.TryGetEntityContainer(It.IsAny<string>(), It.IsAny<DataSpace>(), out entityContainer)) .Returns(true); var storeItemCollection = new StoreItemCollection( GenericProviderFactory<DbProviderFactory>.Instance, new SqlProviderManifest("2008"), "System.Data.FakeSqlClient", "2008"); metadataWorkspaceMock.Setup(m => m.GetItemCollection(DataSpace.SSpace)).Returns(storeItemCollection); #pragma warning disable 618 metadataWorkspaceMock.Object.RegisterItemCollection(omicMock.Object); #pragma warning restore 618 var objectStateManagerMock = new Mock<ObjectStateManager>(metadataWorkspaceMock.Object) { CallBase = true }; var entityKey = new EntityKey(entitySet, "keyValue"); var entityEntry = objectStateManagerMock.Object.AddKeyEntry(entityKey, entitySet); var objectContextMock = Mock.Get( CreateObjectContext( metadataWorkspaceMock: metadataWorkspaceMock, objectStateManagerMock: objectStateManagerMock)); var entityWrapperMock = new Mock<IEntityWrapper>(); entityWrapperMock.Setup(m => m.EntityKey).Returns(entityKey); var entityWrapperFactoryMock = new Mock<EntityWrapperFactory>(); entityWrapperFactoryMock.Setup( m => m.WrapEntityUsingStateManagerGettingEntry( It.IsAny<object>(), It.IsAny<ObjectStateManager>(), out entityEntry)) .Returns(entityWrapperMock.Object); objectContextMock.Setup(m => m.EntityWrapperFactory).Returns(entityWrapperFactoryMock.Object); var executionPlanMock = new Mock<ObjectQueryExecutionPlan>( MockBehavior.Loose, null, null, null, MergeOption.NoTracking, false, null, null); executionPlanMock.Setup( m => m.Execute<object>(It.IsAny<ObjectContext>(), It.IsAny<ObjectParameterCollection>())) .Returns(objectResultMock.Object); objectContextMock.Setup( m => m.PrepareRefreshQuery(It.IsAny<RefreshMode>(), It.IsAny<EntitySet>(), It.IsAny<List<EntityKey>>(), It.IsAny<int>())) .Returns(new Tuple<ObjectQueryExecutionPlan, int>(executionPlanMock.Object, 1)); // Verify that ExecuteInTransaction calls ObjectQueryExecutionPlan.Execute objectContextMock.Setup( m => m.ExecuteInTransaction(It.IsAny<Func<ObjectResult<object>>>(), It.IsAny<IDbExecutionStrategy>(), It.IsAny<bool>(), It.IsAny<bool>())) .Returns<Func<ObjectResult<object>>, IDbExecutionStrategy, bool, bool>( (f, t, s, r) => { executionPlanMock.Verify( m => m.Execute<object>(It.IsAny<ObjectContext>(), It.IsAny<ObjectParameterCollection>()), Times.Never()); var result = f(); executionPlanMock.Verify( m => m.Execute<object>(It.IsAny<ObjectContext>(), It.IsAny<ObjectParameterCollection>()), Times.Once()); return result; }); // Verify that ExecutionStrategy.Execute calls ExecuteInTransaction var executionStrategyMock = new Mock<IDbExecutionStrategy>(); executionStrategyMock.Setup(m => m.Execute(It.IsAny<Func<ObjectResult<object>>>())) .Returns<Func<ObjectResult<object>>>( f => { objectContextMock.Verify( m => m.ExecuteInTransaction( It.IsAny<Func<ObjectResult<object>>>(), It.IsAny<IDbExecutionStrategy>(), false, true), Times.Never()); var result = f(); objectContextMock.Verify( m => m.ExecuteInTransaction( It.IsAny<Func<ObjectResult<object>>>(), It.IsAny<IDbExecutionStrategy>(), false, true), Times.Once()); return result; }); MutableResolver.AddResolver<Func<IDbExecutionStrategy>>(key => (Func<IDbExecutionStrategy>)(() => executionStrategyMock.Object)); try { objectContextMock.Object.Refresh(RefreshMode.StoreWins, new object()); } finally { MutableResolver.ClearResolvers(); } // Finally verify that ExecutionStrategy.Execute was called executionStrategyMock.Verify(m => m.Execute(It.IsAny<Func<ObjectResult<object>>>()), Times.Once()); }