Modelling polymorphic associations database-first vs code-first

I personally stick with Database first when using EF on any schema that is this level of complexity. I have had issues with complex schemas in regards to code first. Maybe the newer versions are a little better, but worrying how to try and code complex relationships seems less straight forward then allowing the engine … Read more

Entity Framework Code First Using Guid as Identity with another Identity Column

This ended up working for me, Entity Framework 5. Turn off automatic migrations Migrate to create the initial table, no frills Declare the ClusterId as Identity (annotation) [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public override int ClusterId { get; set; } Migrate Declare the pk property Id as Identity after the other one has been updated [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public override Guid … Read more

Automatic Migrations for ASP.NET SimpleMembershipProvider

update-database -verbose doesn’t work because your model has been changed after your data table already existed. First, make sure there are no changes to the UserProfile class. Then, run: Add-Migration InitialMigrations -IgnoreChanges This should generate a blank “InitialMigration” file. Now, add any desired changes to the UserProfile class. Once changes are added, run the update … Read more

DbContext AutoDetectChangesEnabled set to false detecting changes

Setting AutoDetectChangesEnabled to false doesn’t disable change tracking. (That’s what the AsNoTracking() extension method would do.) It just disables the automatic call of DetectChanges that would otherwise occur in many DbContext API methods. But DetectChanges isn’t the only method that participates in change tracking. However, if you don’t call it manually at the right places … Read more

EF5 Getting this error message: Model compatibility cannot be checked because the database does not contain model metadata

I found the code working by changing static LaundryShopContext() { Database.SetInitializer<LaundryShopContext>( new DropCreateDatabaseIfModelChanges<LaundryShopContext>()); } into static LaundryShopContext() { Database.SetInitializer<LaundryShopContext>( new DropCreateDatabaseAlways<LaundryShopContext>()); }

Use Entity framework I want to include only first children objects and not child of child(sub of sub)

A downvote brought this answer back into my attention (thank you). I now see that a big part of it is nonsense. Sure enough, the reason for the endless loop is relationship fixup. But you can’t stop EF from doing that. Even when using AsNoTracking, EF performs relationship fixup in the objects that are materialized … Read more

get date part only from datetime value using entity framework

You can compare just specified parts: context.tblvalue.Any(x => x.date.Year == data.Year && x.date.Month == data.Month && x.date.Day == data.Day); EDIT: You can also try the following: context.tblvalue.Any(x => EntityFunctions.TruncateTime(x.date) == data.Date); Another approach: context.tblvalue.Any(x => SqlFunctions.DatePart(“year”, x.date) == data.Year && SqlFunctions.DatePart(“month”, x.date) == data.Month && SqlFunctions.DatePart(“day”, x.date) == data.Day);