0

I made very simple application in WPF using MVVM pattern.

All data in SQL Server 2012 and I use Entity Framework code-first to manipulate it.

But when I start application, it starts very slow and shows this error:

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: 26 - Error Locating Server/Instance Specified

Also I have my ViewModel in App.xaml specified like this:

<viewModel:MainViewModel x:Key="MainViewModel"/>

and this line is underlined with above error.

DataContext:

DataContext="{StaticResource MainViewModel}"

UPD

VieModel class

class MainViewModel : ViewModelBase
{

    public BookContext db;

    public Book book;

    public ObservableCollection<Book> Books { get; set; }



    public MainViewModel()
    {

        db = new BookContext();

        db.Books.Load();

        Books = new ObservableCollection<Book>(db.Books);


        simpleCommand = new RelayCommand(DoSimpleCommand);



    }


    public int ID
    {
        get { return book.ID; }
        set
        {
            if (value == book.ID) return;

            book.ID = value;
            RaisePropertyChanged();

        }
    }

    public string Title
    {
        get { return book.Title; }
        set
        {
            if(value==book.Title) return;

            book.Title = value;
            RaisePropertyChanged();
        }
    }

    public string Author
    {
        get { return book.Author; }
        set
        {
            if(value==book.Author) return;

            book.Author = value;
            RaisePropertyChanged();

        }
    }

    public int Count
    {
        get { return book.Count; }
        set
        {
            if(value==book.Count) return;

            book.Count = value;
            RaisePropertyChanged();

        }
    }


    private RelayCommand simpleCommand;

    public ICommand SimpleCommand
    {
        get { return simpleCommand; }

    }

    private void DoSimpleCommand()
    {
        MessageBox.Show("Button is Clicked");
    }


    public override void Cleanup()
    {
        db.Dispose();
    }
}
panda007
  • 17
  • 1
  • 8
  • Make sure the name of the SQL Server instance you are trying to connect to is correct. If it is, make sure that the instance is actually running. – R. Richards Jul 10 '16 at 11:38
  • @R.Richards All is correct, server name is the same name and server is running. I found solution when datacontext is setted in constructor, not in XAML. But I want to set in XAML – panda007 Jul 10 '16 at 11:43
  • This information is not enough to understand your problem. Please share the viewmodel class. – Yusuf Tarık Günaydın Jul 10 '16 at 12:28
  • Does `BookContext` class need some initialization? When you define the view model class in App.xaml it is created very early in the program and some necessary initialization might not have occurred yet. – Yusuf Tarık Günaydın Jul 10 '16 at 13:02
  • I initialize it in constructor. I think its enough. I use like this in another program with EF and all works fine. – panda007 Jul 10 '16 at 13:08
  • Try this: `DataContext = Resources["MainViewModel"];`. – jsanalytics Jul 10 '16 at 15:10
  • @jstreet The problem is when I write `` in `App.xaml`, straight away it being underlined. – panda007 Jul 10 '16 at 15:33
  • @panda007: I understand that, and if you want to know, it is a little "unorthodox" to define your `ViewModel` as a resource, like that.... I also get the warning, however when i run the app it works.... but only if i declare it as resource in the `Window`, not the `App`. In any case try it and see if it works, although i wouldn't recommend doing it the way you're doing. – jsanalytics Jul 10 '16 at 15:37
  • @jstreet I also get an error when write `DataContext = Resources["MainViewModel"]` in Window. It says ` Missing quotes around attribute values.` and I can't fix it – panda007 Jul 10 '16 at 15:48
  • @panda007 That is supposed to go in your code behind, not in your XAML. – jsanalytics Jul 10 '16 at 15:52
  • @jstreet I wrote in code behind, in constructor of `MainWindow`, but no data appears in datagrid. But when I write `DataContext = new MainViewModel();` it works fine. So is it right in MVVM write in code-behind? – panda007 Jul 10 '16 at 15:57
  • @panda007 yes, the way you did it now is compatible with MVVM. You could also declare your `ViewModel` in XAML as well, but NOT as a `Resource` retrieving data from the database, that was the only problem. And VS is telling you that right away....:O) – jsanalytics Jul 10 '16 at 16:02
  • @jstreet OK. Thnak you very much!! – panda007 Jul 10 '16 at 16:06
  • The dupe target answer describes what is happening and why, and what you can do to fix it. In the end, it doesn't really matter, so fixing it is optional. –  Jul 11 '16 at 15:02

0 Answers0