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

Using EF Core ThenInclude() on Junction tables

but I get an ICollection in the last lambda displayed, so I obviously need the Select() No, you don’t. EF Core Include / ThenInclude totally replace the need of Select / SelectMany used in EF6. Both they have separate overloads for collection and reference type navigation properties. If you use the overload with collection, ThenInclude … Read more

EF Code First DBContext and Transactions

Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { // Do something context.SaveChanges(); // Do something else context.SaveChanges(); scope.Complete(); }

EF Code First giving me error Cannot insert explicit value for identity column in table ‘People’ when IDENTITY_INSERT is set to OFF. [duplicate]

I get the following error: Cannot insert explicit value for identity column in table ‘People’ when IDENTITY_INSERT is set to OFF. I think that the IDENTITY_INSERT is the Auto Increment functionality which is off. So, check the field PersonId in the database to see if it is an identity. Besides, maybe this will fix your … Read more

How to create index in Entity Framework 6.2 with fluent configuration

Well 26.10.2017 Entity Framework 6.2 was officially released. It includes a possibility to define indexes with ease via Fluent API. Ho it is to use was already announced in the beta of 6.2. Now you can use the HasIndex() method, followed by IsUnique() if it should be an unique index. Just a small comparison (before/after) … 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

What is the syntax for self referencing foreign keys in EF Code First?

Something like this will work: public class Contact { public int Id {get;set;} public string Name {get;set;} public int? SpouseId {get;set;} [ForeignKey(“SpouseId”)] public Contact Spouse {get;set;} } ForeignKeyAttribute has been added to System.ComponentModel.DataAnnotations by CTP5 assembly. Update I: CTP5 Bug: Due to a bug in CTP5, creating an Independent Self Referencing Associations throws an exception. … Read more

MetadataException when using Entity Framework Entity Connection [duplicate]

Found the problem. The standard metadata string looks like this: metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl And this works fine in most cases. However, in some (including mine) Entity Framework get confused and does not know which dll to look in. Therefore, change the metadata string to: metadata=res://nameOfDll/Model.csdl|res://nameOfDll/Model.ssdl|res://nameOfDll/Model.msl And it will work. It was this link that got me on … Read more