Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Expecting non-empty string for 'providerInvariantName' parameter

Ok, this may not be related to EF. I am trying to use the code-first feature and following is what I wrote:-

var modelBuilder = new ModelBuilder();
            var model = modelBuilder.CreateModel();
            using (AddressBook context = new AddressBook(model))
            {
                var contact = new Contact
                {
                    ContactID = 10000,
                    FirstName = "Brian",
                    LastName = "Lara",
                    ModifiedDate = DateTime.Now,
                    AddDate = DateTime.Now,
                    Title = "Mr."

                };
                context.contacts.Add(contact);
                int result = context.SaveChanges();
                Console.WriteLine("Result :- "+ result.ToString());

            }

The context class:-

public class AddressBook : DbContext
    {
        public AddressBook()
        { }
        public AddressBook(DbModel AddressBook)
            : base(AddressBook)
        {

        }
        public DbSet<Contact> contacts { get; set; }
        public DbSet<Address> Addresses { get; set; }
    }

and the connection string:-

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
    <add name="AddressBook"  connectionString="Data Source=MyMachine;Initial Catalog=AddressBook;Integrated Security=True;MultipleActiveResultSets=True;providerName=System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

So, the database name is "AddressBook" and the error happens when I initialize the AddressBook object. I see people suggesting to add (providerName="System.Data.SqlClient") to the connection string. But It does not work in my case. Am I missing anything here?

like image 926
Ashish Gupta Avatar asked Aug 30 '10 09:08

Ashish Gupta


1 Answers

You should add the System.Data.SqlClient as the value to a new attribute named "ProviderName". Just like below :-

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
    <add name="AddressBook" providerName="System.Data.SqlClient"  connectionString="Data Source=MyMachine;Initial Catalog=AddressBook;Integrated Security=True;MultipleActiveResultSets=True;"/>
    </connectionStrings>
</configuration>
like image 125
Ashish Gupta Avatar answered Oct 04 '22 15:10

Ashish Gupta