Entity Framework 6 with SQLite 3 Code First – Won’t create tables

Edit (12.08.2021) This post/lib is about EF 6 not EF Core. If you use EF Core, code first for SQLite is supported. Initial (05.04.2015) I started with the code from fried and created a project which lets you use CodeFirst. The project is available open source on GitHub or as NuGet Package. If you encounter … Read more

Application can’t scaffold items

In my case I moved my connection strings out of the Web.config to <connectionStrings configSource=”ConnectionStrings.config”/> that when I started getting the error when I was trying to scaffold. There was an error running the selected code generator: ‘Exception has been thrown by the target of an invocation.’ Moving my connection strings back to the Web.config … Read more

Error: No Entity Framework provider found for the ADO.NET provider with invariant name ‘System.Data.SqlClient’

I had this problem in a situation where I have a model project that has the references to both EntityFramework and the .SqlServer assemblies, and a separate UI project that uses ASP.NET MVC. I wanted to upgrade from EF 4.1 to 6.1. I actually had to do part of what was described here: http://robsneuron.blogspot.com/2013/11/entity-framework-upgrade-to-6.html. I … Read more

How can I use use Entity Framework to do a MERGE when I don’t know if the record exists?

I use AddOrUpdate in this situation. However, I believe it does query the database first in order to decide to issue an insert or update. context.MyEntities.AddOrUpdate(e => e.Id, entity); Update: I ran through my debug log files. First it runs: SELECT TOP (2) … WHERE 1 = [Extent1].[Id] Then it runs either: INSERT [dbo].[TestTable](…) VALUES … Read more

How do I remove underscore of foreign key fields in code first by convention

I finally found an answer for this, by writing a custom convention. This convention works in EF 6.0 RC1 (code from last week), so I think it’s likely to continue to work after EF 6.0 is released. With this approach, the standard EF conventions identify the independent associations (IAs), and then create the EdmProperty for … Read more

Is it possible to set a unique constraint using Entity Framework Code First?

It appears that the unique constraint feature that was scheduled to release with Version 6 got pushed to 6.1. With EF 6.1, you can define a constraint using the Index attribute as shown below: [Index(“IX_FirstAndSecond”, 1, IsUnique = true)] public int FirstColumn { get; set; } [Index(“IX_FirstAndSecond”, 2, IsUnique = true)] public int SecondColumn { … Read more

Entity Framework 6: audit/track changes

If using EF6’s DbContext you can use ChangeTracker in SaveChanges override to find added/modified entities of custom type, for example IAuditedEntity. public interface IAuditedEntity { string CreatedBy { get; set; } DateTime CreatedAt { get; set; } string LastModifiedBy { get; set; } DateTime LastModifiedAt { get; set; } } public override int SaveChanges() { … Read more

Adding ADO.Net Entity Framework gives “The project’s target framework does not contain Entity Framework runtime assemblies”

I know that almost all the other answers recommends to change the target framework, but some users (including me) needs to use .NET 6.0 instead of .NET Framework, so that solution its not valid for us. I was able to create the models by using Paul Sinnema’s link and using SQL authentication instead of Windows … Read more

Save detached entity in Entity Framework 6

Yes, this is correct. This article describes various ways of adding and attaching entities, and it provides this example: var existingBlog = new Blog { BlogId = 1, Name = “ADO.NET Blog” }; using (var context = new BloggingContext()) { // The next step implicitly attaches the entity context.Entry(existingBlog).State = EntityState.Modified; // Do some more … Read more