vendredi 4 février 2022

System.InvalidOperationException was unhandled by user

I am using EF6 (Entity Framework 6) and "designed based approach".

The connection string 'SampleViewEntities' in the application's configuration file does not contain the required providerName attribute.

I want to fetch record using repository namespace.

App.config file connection string in repository namespace.

<connectionStrings>
<add name="SampleViewEntities"  connectionString="metadata=res://*/SampleView.csdl|res://*/SampleView.ssdl|res://*/SampleView.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server;initial catalog=Mydatabase;user id=xxxx;password=xxxxxxxx;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

and web.config file connection sting

<connectionStrings>
    <add name="SampleViewEntities"  connectionString="data source=server;initial catalog=Mydatabase;user id=xxxx;password=xxxxxxxx;MultipleActiveResultSets=True;"/>
  </connectionStrings>

I an use the SingleConnection class for making connection in main namespace

public class SingleConnection
    {
        
        public static string Connect()
        {
            string sqlString = System.Configuration.ConfigurationManager.ConnectionStrings["SampleViewEntities"].ConnectionString;

            //Build an Entity Framework connection string
            EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
            {
                Provider = "System.Data.SqlClient",
                Metadata = "res://*/SampleView.csdl|res://*/SampleView.ssdl|res://*/SampleView.msl",
                ProviderConnectionString = sqlString.ToString()
            };
            return entityString.ConnectionString;
        }
    }

Use for connection Global.asax.cs

using Repository;
public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            YourViewsEntities2022.ConnectionString = SingleConnection.ConString;
            string sqlString = System.Configuration.ConfigurationManager.ConnectionStrings["SampleViewEntities"].ConnectionString;
            WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(sqlString, "System.Data.SqlClient", "UserProfile", "ID", "EmailID", autoCreateTables: false);


            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }

It establish database connection properly and login with user id and password but fetch data from repository the showing exception "System.InvalidOperationException" , and The connection string 'SampleViewEntities' in the application's configuration file does not contain the required providerName attribute.

Aucun commentaire:

Enregistrer un commentaire