0

I created a new model:

 public class AuthorDatacontext : DbContext
    {
        public DbSet<Author> Authors { get; set; }

        static AuthorDatacontext()
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AuthorDatacontext>());
        }
    }

A connection string:

  <add name="MyLibrary.Models.AuthorDatacontext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MyLibrary.Models.AuthorDatacontext.mdf" providerName = "System.Data.SqlClient"  />

and i used it in my controller:

var db = new AuthorDatacontext();
                db.Authors.Add(author);
                db.SaveChanges();

I did it before, in another project and it worked fine. But now i receive an error, trying saving a new author:

An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

What am i doing wrong? If i delete the connection and leave only the default connection, everything works fine.

The previous project had the same connection string:

<add name="MvcAuction.Models.AuctionsDataContext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MvcAuction.Models.AuctionsDataContext.mdf" providerName = "System.Data.SqlClient"  />

1 Answer 1

1

I think the problem is that the EF tries to create the MDF again from your project. Try deleting it first.

\MvcAuction.Models.AuctionsDataContext.mdf the EF tries to see whether this exists in that location. If you had used SQL server it would have migrated changes from CODE First to database.

Sign up to request clarification or add additional context in comments.

8 Comments

but its another project. Why does it look for it?
You need a database to initialize your EF Context
How i create it? And why i didnt have this problem in the first project?
You should try to provide more details. You should have your own database for this project, because the one from other project won't be deployed.
I understand that. But in the previous project i just created the connection and the EF and everything else was created automatically. So what is the difference? What i supposed to add here?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.