How to construct Order By Expression dynamically in Entity Framework? [duplicate]

I found a solution with the help of Jon Skeet‘s old answer. public static class QueryHelper { private static readonly MethodInfo OrderByMethod = typeof (Queryable).GetMethods().Single(method => method.Name == “OrderBy” && method.GetParameters().Length == 2); private static readonly MethodInfo OrderByDescendingMethod = typeof (Queryable).GetMethods().Single(method => method.Name == “OrderByDescending” && method.GetParameters().Length == 2); public static bool PropertyExists<T>(this IQueryable<T> source, … Read more

Validate data using DataAnnotations with WPF & Entity Framework?

You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more

Entity Framework Many to many through containing object

You can map private properties in EF code-first. Here is a nice description how to do it. In your case it is about the mapping of Subscriber._subscribedList. What you can’t do is this (in the context’s override of OnModelCreating): modelBuilder.Entity<Subscriber>().HasMany(x => x._subscribedList); It won’t compile, because _subscribedList is private. What you can do is create … Read more

Why is my Entity Framework Code First proxy collection null and why can’t I set it?

As you correctly observed in the answer to your own question, removing the “virtual” keyword from the collection properties works around the problem, by preventing the Entity Framework from creating a change tracking proxy. However, this is not a solution for many people, because change tracking proxies can be really convenient and can help prevent … Read more

Entity Framework – Get List of Tables

This sample code from post What Tables Are In My EF Model? And My Database? using (var dbContext = new YourDbContext()) { var metadata = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace; var tables = metadata.GetItemCollection(DataSpace.SSpace) .GetItems<EntityContainer>() .Single() .BaseEntitySets .OfType<EntitySet>() .Where(s => !s.MetadataProperties.Contains(“Type”) || s.MetadataProperties[“Type”].ToString() == “Tables”); foreach (var table in tables) { var tableName = table.MetadataProperties.Contains(“Table”) && table.MetadataProperties[“Table”].Value != null … Read more

Net Core: Using OData in Onion Architecture, Convert Query Parameters Into Linq

It’s been ages since I wrote this function, but the links might help. GetAll() just returns an IEnumerable. [Route(“”), HttpGet] public IHttpActionResult Get(ODataQueryOptions<Language> queryOptions) { Log.Info($”{nameof(Get)} (ODataQueryOptions)”); //OData directly with normal WebAPI //https://blogs.msdn.microsoft.com/webdev/2013/02/25/translating-odata-queries-to-hql/ //https://stackoverflow.com/questions/10781309/asp-net-mvc-4-webapi-manually-handle-odata-queries //https://blogs.msdn.microsoft.com/odatateam/2014/07/04/tutorial-sample-using-odatauriparser-for-odata-v4/ //OData without entity framework, but full-on odata controller //http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/ //OData tips and tricks //https://blogs.msdn.microsoft.com/davidhardin/2014/12/17/web-api-odata-v4-lessons-learned/ return Ok(queryOptions.ApplyTo(_repository.GetAll().AsQueryable())); }

How to write this EF Mock setup code as a reusable Generic Boilerplate?

Tim Larson already offered a great solution for this boilerplate code in his blog: public static class DbSetMocking { private static Mock<DbSet<T>> CreateMockSet<T>(IQueryable<T> data) where T : class { var queryableData = data.AsQueryable(); var mockSet = new Mock<DbSet<T>>(); mockSet.As<IQueryable<T>>().Setup(m => m.Provider) .Returns(queryableData.Provider); mockSet.As<IQueryable<T>>().Setup(m => m.Expression) .Returns(queryableData.Expression); mockSet.As<IQueryable<T>>().Setup(m => m.ElementType) .Returns(queryableData.ElementType); mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()) .Returns(queryableData.GetEnumerator()); return … Read more