0

Problem I'm dealing with, is weird error :

SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

it happens whenever any method with db is called, like:

foo()
{
    return _dbContext.data.ToList();
}

File db was created automatically based on code first approach and connection string:

<add name="Context" 
     connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\RestApi.Services.Context.mdf;Initial Catalog=RestApi.Services.Context;Integrated Security=True" 
     providerName="System.Data.SqlClient" />

I can connect with no errors to that file by VS server explorer.

On same PC I've runned several others apps with file db without any errors.
When I watch "Context" var in debbuger it seems connected to proper DB, but have error on workspaceID.

Now I'm stuck and don't have any idea how to fix it. :(

UnityConfig:

public static void RegisterComponents()
{
        var container = new UnityContainer();
        container.RegisterType<Context, Context>(new PerThreadLifetimeManager());
        container.RegisterType<IXService, XService>();
        DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
}

Context class:

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

    public DbSet<X> Xs{ get; set; }
    public DbSet<Z> Zs{ get; set; }
    public DbSet<Y> Ys{ get; set; }
}

EDIT

File Db was not the problem, I've tried with normal db connection string, and errors still occur.

I've tried many different solutions and no one works :(

now after change in UnityConfig:

        public static void RegisterComponents()
        {
            var container = new UnityContainer();

            container.RegisterType<DbContext, Context>();
            container.RegisterType<IXService, XService>();

            DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }

first db read:

InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

next ones:

NullReferenceException: Object reference not set to an instance of an object.

EDIT2

Docker support was causing problems ;/ I'll upload solution when I find one

3
  • AFAIK MSSQL does not support having a file as the database, as a matter of fact, almost no database allows this, except SQLite Commented Dec 4, 2019 at 9:31
  • Hello!Welcome to SO. Does this answer your question?stackoverflow.com/questions/48516702/… Commented Dec 4, 2019 at 9:32
  • @VenkataShivaram no, this guy have different problem, his unity cannot resolve his depencencies. when i dont get any error from unity Commented Dec 4, 2019 at 16:37

1 Answer 1

0

make sure that connectionstring data source is valid server name. if server name is valid then follow this instruction, this may help you.

Open "SQL Server Configuration Manager"

Now Click on "SQL Server Network Configuration" and Click on "Protocols for Name"

Right Click on "TCP/IP" (make sure it is Enabled) Click on Properties

Now Select "IP Addresses" Tab -and- Go to the last entry "IP All"

Enter "TCP Port" 1433.

Now Restart "SQL Server .Name." using "services.msc" (winKey + r)

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

1 Comment

already TCP/IP was enabled, and that port was setted for IPAll(and all others too) :(

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.