The relationship could not be changed because one or more of the foreign-key properties is non-nullable

You should delete old child items thisParent.ChildItems one by one manually. Entity Framework doesn’t do that for you. It finally cannot decide what you want to do with the old child items – if you want to throw them away or if you want to keep and assign them to other parent entities. You must … Read more

What’s the difference(s) between .ToList(), .AsEnumerable(), AsQueryable()?

There is a lot to say about this. Let me focus on AsEnumerable and AsQueryable and mention ToList() along the way. What do these methods do? AsEnumerable and AsQueryable cast or convert to IEnumerable or IQueryable, respectively. I say cast or convert with a reason: When the source object already implements the target interface, the … Read more

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea? [closed]

You can have multiple contexts for single database. It can be useful for example if your database contains multiple database schemas and you want to handle each of them as separate self contained area. The problem is when you want to use code first to create your database – only single context in your application … Read more

Unique Key constraints for multiple columns in Entity Framework

With Entity Framework 6.1, you can now do this: [Index(“IX_FirstAndSecond”, 1, IsUnique = true)] public int FirstColumn { get; set; } [Index(“IX_FirstAndSecond”, 2, IsUnique = true)] public int SecondColumn { get; set; } The second parameter in the attribute is where you can specify the order of the columns in the index. More information: MSDN

Reset Entity-Framework Migrations

You need to : Delete the state: Delete the migrations folder in your project; And Delete the __MigrationHistory table in your database (may be under system tables); Then Run the following command in the Package Manager Console: Enable-Migrations -EnableAutomaticMigrations -Force Use with or without -EnableAutomaticMigrations And finally, you can run: Add-Migration Initial

How do I delete multiple rows in Entity Framework (without foreach)

EntityFramework 6 has made this a bit easier with .RemoveRange(). Example: db.People.RemoveRange(db.People.Where(x => x.State == “CA”)); db.SaveChanges(); Warning! Do not use this on large datasets! EF pulls all the data into memory, THEN deletes it. For smaller data sets this might not be an issue but generally avoid this style of delete unless you can … Read more

EF Migrations: Rollback last applied migration?

I want to add some clarification to this thread: Update-Database -TargetMigration:”name_of_migration” What you are doing above is saying that you want to rollback all migrations UNTIL you’re left with the migration specified. Thus, if you use GET-MIGRATIONS and you find that you have A, B, C, D, and E, then using this command will rollback … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)