How to update not every fields of an object using Entity Framework and EntityState.Modified

Let’s assume that you have a collection of the properties to be excluded: var excluded = new[] { “property1”, “property2” }; With EF5 on .NET 4.5 you can do this: var entry = context.Entry(obj); entry.State = EntityState.Modified; foreach (var name in excluded) { entry.Property(name).IsModified = false; } This uses a new feature of EF5 on … Read more

EF Model First or Code First Approach?

This is too long question. You should break your problem into multiple separate questions next time. Code-first x Model-first x Database-first You are a database guy so the best approach for you is an incremental database-first approach where you define the stuff in DB (or VS Database tools) and update your model from the database. … Read more

Entity Framework 5 – DbContext Has Changes?

For EF 5 use DbContext‘s ChangeTracker: public bool HasUnsavedChanges() { return this.ChangeTracker.Entries().Any(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted); } For EF 6 use the ChangeTracker.HasChanges() method which will also detect changes in many to many relationships: public bool HasUnsavedChanges() { return this.ChangeTracker.HasChanges(); }

Entity Framework Code-first default data in database

You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways interface. Like: public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext-> And then you overwrite Seed method like: protected override void Seed(YourDbContext context) Whole example might look like: public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext> { protected override void Seed(EntitiesContext context) { List<Role> roles = new List<Role> { new Role {Id=1, … Read more

Setting a foreign key to null when using entity framework code first

I think the problem is that as far as the context is concerned, you haven’t actually changed anything. You can use the lazy loading approach previously suggested by using virtual, but since you haven’t requested that the Employee be loaded yet, it’s still null. You could try this: var forceLoad = project.Employee; project.Employee = null; … Read more

How to update only one table for model from database with Entity Framework?

The EDMX file is an XML file that is a combination of 3 different parts that make up the whole thing. If you right-click on your EDMX file and choose “Open with… XML Editor” you’ll see the 3 different sections: <edmx:ConceptualModels> <edmx:StorageModels> <edmx:Mappings> These sections can be edited manually, at your own risk! 🙂 That … Read more

Entity Framework: Querying Child Entities [duplicate]

The only way to get a collection of parents with a filtered children collection in a single database roundtrip is using a projection. It is not possible to use eager loading (Include) because it doesn’t support filtering, Include always loads the whole collection. The explicite loading way shown by @Daz requires one roundtrip per parent … Read more

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