Click here to Skip to main content
14,575,978 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,
some times (5-10%) when i try to execute EF Query this error appear :
System.Exception: خطأ عند محاولة ملء مصدر البيانات. تم طرح الاستثناء التالي: 
Failed to connect to the database. To learn more, see the exception details. 

Exception details:
 The default DbConfiguration instance was used by the Entity Framework before an attempt was made to set an instance of 'DbConfiguration'. The 'DbConfiguration' instance must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information. ---> DevExpress.XtraReports.DataRetrievalException: خطأ عند محاولة ملء مصدر البيانات. تم طرح الاستثناء التالي: 
Failed to connect to the database. To learn more, see the exception details. 

Exception details:
 The default DbConfiguration instance was used by the Entity Framework before an attempt was made to set an instance of 'DbConfiguration'. The 'DbConfiguration' instance must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information. ---> DevExpress.DataAccess.EntityFramework.EFConnectionException: Failed to connect to the database. To learn more, see the exception details. 

Exception details:
 The default DbConfiguration instance was used by the Entity Framework before an attempt was made to set an instance of 'DbConfiguration'. The 'DbConfiguration' instance must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information. ---> System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before an attempt was made to set an instance of 'DbConfiguration'. The 'DbConfiguration' instance must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.


it dosn't happens with one query but different queries

What I have tried:

i can't follow this error because it Suddenly happens but i checked MySQL service and it work.

Where do I start tracking and finding a solution to this problem?

Update 1

i use this function to set custom connection parameters
public static mrsalesdbEntities OpenConn()
        {
            mrsalesdbEntities MrSalesContext = new mrsalesdbEntities();
            MrSalesContext.Database.CommandTimeout = 999999;
            MrSalesContext.ChangeDatabase
               (
                   initialCatalog: myconn.database,
                   port: Convert.ToUInt32( myconn.port),
                   userId: myconn.uid,
                   password: myconn.password,
                   dataSource: myconn.server
            );
            return MrSalesContext;
        }

mrsalesdbEntities DB1 = ConnectionTools.OpenConn();


Update 2

i tried to add my custom configs but also it still read the connection parameters from App.config gile

public class FE6CodeConfig : DbConfiguration
{
    public FE6CodeConfig()
    {
        SetDefaultConnectionFactory(new LocalDbConnectionFactory(myconn.database,myconn.mrsales_Coonn));
    }
}

//[DbConfigurationType(typeof(FE6CodeConfig))]
[DbConfigurationType("MrSales.MrSModels.FE6CodeConfig, MrSales")]
Posted
Updated 3-Jun-20 2:42am
v4
Comments
Golden Basim 1-Jun-20 13:35pm
   
i use this function to set custom connection parameters
public static mrsalesdbEntities OpenConn()
        {
            mrsalesdbEntities MrSalesContext = new mrsalesdbEntities();
            MrSalesContext.Database.CommandTimeout = 999999;
            MrSalesContext.ChangeDatabase
               (
                   initialCatalog: myconn.database,
                   port: Convert.ToUInt32( myconn.port),
                   userId: myconn.uid,
                   password: myconn.password,
                   dataSource: myconn.server
            );
            return MrSalesContext;
        }

mrsalesdbEntities DB1 = ConnectionTools.OpenConn();

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

There's 2 possible ways to set up EF configuration:
1. Code-based configuration - EF6 | Microsoft Docs[^]
2. Configuration File Settings - EF6 | Microsoft Docs[^]

If you're using code-based configuration, then...
Quote:
A class derived from DbConfiguration might look like this:
C#

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;

namespace MyNamespace
{
    public class MyConfiguration : DbConfiguration
    {
        public MyConfiguration()
        {
            SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
            SetDefaultConnectionFactory(new LocalDbConnectionFactory("mssqllocaldb"));
        }
    }
}


I'd suggest to read this article: Jon Schneider's Tech Blog: Fix: “The default DbConfiguration instance was used by the Entity Framework before the '...' type was discovered” in LINQPad[^]
   
Comments
Golden Basim 2-Jun-20 9:08am
   
i updated my context file to

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MrSales.MrSModels
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;
    using System.Data.Common;


    public partial class mrsalesdbEntities : DbContext
    {
        public mrsalesdbEntities()
            : base("name=mrsalesdbEntities")
        {
        }


        public mrsalesdbEntities(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<account_items> account_items { get; set; }


also i updated the app.config file to:

<entityFramework codeConfigurationType="MrSales.MrSModels.mrsalesdbEntities, MrSales.MrSModels">


but this error appear :
The DbConfiguration type 'MrSales.MrSModels.mrsalesdbEntities, MrSales.MrSModels' specified in the application config file could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100