public void Sets_of_subtypes_can_still_be_sorted() { using (var context = new SimpleModelContext()) { var featuredProducts = context.Set <FeaturedProduct>(); featuredProducts.Attach( new FeaturedProduct { Id = 3 }); featuredProducts.Attach( new FeaturedProduct { Id = 1 }); featuredProducts.Attach( new FeaturedProduct { Id = 4 }); var bindingList = featuredProducts.Local.ToBindingList(); ((IBindingList)bindingList).ApplySort( TypeDescriptor.GetProperties(typeof(Product))["Id"], ListSortDirection.Ascending); Assert.Equal(1, bindingList[0].Id); Assert.Equal(3, bindingList[1].Id); Assert.Equal(4, bindingList[2].Id); } }
public void Non_generic_Set_throws_when_attempting_to_create_set_for_value_type() { using (var context = new SimpleModelContext()) { Assert.Throws <InvalidOperationException>(() => context.Set(typeof(int))).ValidateMessage( "DbSet_EntityTypeNotInModel", typeof(int).Name); } }
public void Non_generic_SQL_query_for_entity_can_be_executed_multiple_times() { using (var context = new SimpleModelContext()) { var query = context.Set(typeof(Product)).SqlQuery("select * from Products"); Assert.True(query.ToList <Product>().SequenceEqual(query.ToList <Product>())); } }
/// <summary> /// Asserts that an operation on a set throws if the type of the set is not valid for the model. /// </summary> /// <param name="entityType"> The type of entity for which the set is for. </param> /// <param name="test"> The test to run on the set. </param> private void ThrowsForInvalidTypeNonGeneric(Type entityType, Action <DbSet> test) { using (var context = new SimpleModelContext()) { var set = context.Set(entityType); Assert.Throws <InvalidOperationException>(() => test(set)).ValidateMessage( "DbSet_EntityTypeNotInModel", entityType.Name); } }
/// <summary> /// Asserts that an operation on a set throws if the type of the set is not valid for the model. /// </summary> /// <typeparam name="TEntity"> The type of entity for which a set should be created. </typeparam> /// <param name="test"> The test to run on the set. </param> private void ThrowsForInvalidType <TEntity>(Action <IDbSet <TEntity> > test) where TEntity : class { using (var context = new SimpleModelContext()) { var set = context.Set <TEntity>(); Assert.Throws <InvalidOperationException>(() => test(set)).ValidateMessage( "DbSet_EntityTypeNotInModel", typeof(TEntity).Name); } }
public void Generic_DbContext_Entries_can_be_used_with_a_derived_type() { using (var context = new SimpleModelContext()) { context.Products.Include(p => p.Category).Load(); var entries = context.ChangeTracker.Entries <FeaturedProduct>().ToList(); Assert.NotEqual(0, entries.Count); Assert.Equal(context.Set <FeaturedProduct>().Local.Count, entries.Count); } }
SQL_query_can_be_used_to_materialize_derived_entities_into_a_set_even_when_base_entities_are_returned() { using (var context = new SimpleModelContext()) { var products = context.Set <FeaturedProduct>().SqlQuery("select * from Products").ToList(); Assert.Equal(7, products.Count); Assert.Equal(products.Count, context.Products.Local.Count); ValidateBovril(products.Single(d => d.Name == "Bovril")); ValidateCadillac(products.Single(d => d.Name == "Cadillac")); } }
public void Non_generic_SQL_query_with_parameters_can_be_used_to_materialize_entities_into_a_set() { using (var context = new SimpleModelContext()) { var products = context.Set(typeof(Product)).SqlQuery( "select * from Products where Id < {0} and CategoryId = {1}", 4, "Beverages").ToList <Product>(); Assert.Equal(1, context.Products.Local.Count); ValidateBovril(products); } }
public void SQL_query_can_be_used_to_materialize_derived_entities_into_a_set() { using (var context = new SimpleModelContext()) { var products = context.Set <FeaturedProduct>().SqlQuery( "select * from Products where Discriminator = 'FeaturedProduct'").ToList(); Assert.Equal(1, products.Count); Assert.Equal(products.Count, context.Products.Local.Count); ValidateCadillac(products.Single()); } }
SQL_query_identity_resolution_fails_when_type_returned_from_query_is_different_from_type_in_state_manager() { using (var context = new SimpleModelContext()) { context.Products.Load(); var query = context.Set <Product>().SqlQuery("select * from Products"); Assert.Throws <NotSupportedException>(() => query.ToList()).ValidateMessage( "Materializer_RecyclingEntity", "SimpleModelContext.Products", "SimpleModel.Product", "SimpleModel.FeaturedProduct", "EntitySet=Products;Id=7"); } }
public void SQL_query_uses_identity_resolution() { using (var context = new SimpleModelContext()) { var trackedProducts = context.Products.Where(p => !(p is FeaturedProduct)).OrderBy(p => p.Id).ToList(); var products = context.Set <Product>().SqlQuery("select * from Products").OrderBy(p => p.Id).ToList(); Assert.Equal(6, trackedProducts.Count); Assert.Equal(7, products.Count); Assert.Equal(products.Count, context.Products.Local.Count); products.Remove(products.Single(d => d.Name == "Cadillac")); Assert.True(products.SequenceEqual(trackedProducts)); } }
public void Non_generic_Set_throws_only_when_used_if_type_not_in_the_model_even_if_type_is_anonymous() { var anon = new { Id = 4, Name = "" }; using (var ctx = new SimpleModelContext()) { var set = ctx.Set(anon.GetType()); Assert.Throws <InvalidOperationException>(() => set.Add(anon)).ValidateMessage( "DbSet_EntityTypeNotInModel", anon.GetType().Name); } }
public void Non_generic_SQL_query_can_be_used_to_materialize_entities_into_a_set() { using (var context = new SimpleModelContext()) { var productsQuery = context.Set(typeof(Product)).SqlQuery("select * from Products"); // Quick check that creating the query object does not execute the query. Assert.Equal(0, context.Products.Local.Count); var products = productsQuery.ToList <Product>(); Assert.Equal(7, products.Count); Assert.Equal(products.Count, context.Products.Local.Count); ValidateBovril(products.Single(d => d.Name == "Bovril")); CadillacIsNotFeaturedProduct(products.Single(d => d.Name == "Cadillac")); } }