For the 'providerInvariantName' argument, Entity Framework expects a non-empty string.

.net ado.net c# entity-framework

Question

Okay, so that may not be relevant to EF. The following is what I typed while I was attempting to utilize the code-first feature:-

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 class of the context:

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

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

as well as 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>

The problem occurs when I initialize the AddressBook object, and the database's name is "AddressBook." I've seen suggestions that the connection string include (providerName="System.Data.SqlClient"). But in my situation, it does not function. Am I overlooking anything here?

1
17
8/30/2010 9:49:08 AM

Accepted Answer

The System.Data.SqlClient should be added as the value of a new property called "ProviderName." similar to what is described 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>
36
8/30/2010 9:50:38 AM

Popular Answer

When I uploaded my website to the production server, I saw the same issue. the in my connection stringproviderName set property A connection string in the code was the root of the issue.machine.config named LocalSqlServer and an actor who made use of the connection string:

<roleManager>
   <providers>
   <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   ....

Therefore, before registering mine, all I needed to do was delete connection strings and role providers:

  <connectionStrings>
    <clear/>
    <add name="DbEntities" connectionString="..." providerName="System.Data.SqlClient" />
  </connectionStrings>
  ...
  <roleManager defaultProvider="DefaultRoleProvider" enabled="true">
    <providers>
      <clear/>
      <add name="DefaultRoleProvider" ... />
    </providers>
  </roleManager>


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow