Entity Framework: I set the foreign key, SaveChanges then access the navigation property, but it doesn’t load the related entity. Why not?

To ensure that lazy loading of a navigation property will work after you’ve created the parent you must not create the Survey with the new operator but create it by means of the context instance because it will instantiate a dynamic proxy that is capable to lazily load the related Client. That’s what the DbSet<T>.Create() … Read more

Code First Migration with Connection Strings

On the package manager console type: Get-Help Update-Database Relevant part: Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [<Com monParameters>] So you can do a Update-Database -ConnectionStringName “MyConnectionString” and it should work like a charm. You also have a MigrateDatabaseToLatestVersion database initializer, if you set it (via Database.SetInitializer()), … Read more

Non-static method requires a target. Entity Framework 5 Code First

The problem boiled down to the query. My original question had this query: var allPartners = DbContext.User .Include(u => u.Businesses) .Where(u => u.Businesses.Any(x => x.Id == currentBusinessId)) .ToList(); Which wasn’t quite accurate, I had in fact removed the error in an attempt to ask my question succinctly. The query was actually: var currentBusiness = GetBusiness(); … Read more

EF Core Error – No project was found. Change the current working directory or use the –project option

sometimes you need to change the current directory in console/terminal eg: PM> cd E:\Projects\CrossTest\ PM> dotnet ef migrations add InitialMigration and Align your package versions. Either use preview1 packages or preview2. Mix of those are not supported.

How to Seed Users and Roles with Code First Migration using Identity ASP.NET Core

My way of doing this is to create a class in models namespace. public class SampleData { public static void Initialize(IServiceProvider serviceProvider) { var context = serviceProvider.GetService<ApplicationDbContext>(); string[] roles = new string[] { “Owner”, “Administrator”, “Manager”, “Editor”, “Buyer”, “Business”, “Seller”, “Subscriber” }; foreach (string role in roles) { var roleStore = new RoleStore<IdentityRole>(context); if (!context.Roles.Any(r … Read more

Composite Key with EF 4.1 Code First

You can mark both ActivityID and ActivityName properties with Key annotation or you can use fluent API as described by @taylonr. Edit: This should work – composite key defined with annotations requires explicit column order: public class ActivityType { [Key, Column(Order = 0)] public int ActivityID { get; set; } [Key, Column(Order = 1)] [Required(ErrorMessage … Read more

How to delete and recreate from scratch an existing EF Code First database

If I’m understanding it right… If you want to start clean: 1) Manually delete your DB – wherever it is (I’m assuming you have your connection sorted), or empty it, but easier/safer is to delete it all together – as there is system __MigrationHistory table – you need that removed too. 2) Remove all migration … Read more

EntityType ‘IdentityUserLogin’ has no key defined. Define the key for this EntityType

In my case I had inherited from the IdentityDbContext correctly (with my own custom types and key defined) but had inadvertantly removed the call to the base class’s OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // I had removed this /// Rest of on model creating here. } Which then fixed up my missing … Read more