Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException was unhandled by user code HResult=-2146233079

I'm using Entityframework with repository pattern using database. I'm getting the following error when I try to connect to the database

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException was unhandled by user code HResult=-2146233079

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

How do I fix this?

like image 704
Muhammad Nasir Avatar asked Oct 24 '25 14:10

Muhammad Nasir


1 Answers

If you are trying to connect to an existent database but EF try to do code first you should follow this easy steps to prevents this error.

  1. Remove your current connection string in your web.config
  2. Find your .edmx file and make double click how you can see in the next image. enter image description here
  3. If you see this screen after click on "Update Model From database..." enter image description here Make sure you mark "Save connection setting in App.Config as"
  4. After that if you .edmx file are in a class library you should copy the connection string to your main project.

On other side if you don't see the screen of step 4 make sure that you have removed all connection string of your project.

like image 71
Ferri Avatar answered Oct 27 '25 04:10

Ferri