2
votes

I am working on a project where we use EF for data access and WPF and MVVM for user interface.

I am binding a view model to the data context of a window. This view model has a default constructor defined as following:

 public KonumVM()
    {
        LocationOperations = new LocationOperations();
        LocationNames = new ObservableCollection<string>();
        Corporations= new ObservableCollection<Kurum>();
        //Corporations= LocationOperations.GetCorpValues();

        //foreach (var corp in Corporations)
        //{
        //    LocationNames.Add(corp.Name);
        //}
    }

The commented lines are where I query the database. If the lines are not commented, when I try to edit the XAML code of the window that binds to this View Model I get an exception, I think it is a XAML Load Failure, which blocks designer. The exception is at the end of the message. When I comment the database querying lines, the designer is fine, no exceptions.

What should I do? Should I move the database access code to some other place other than default constructor?

Cannot open database "DemirbaşEntityLibrary.DemirbaşContext" requested by the login. The login failed. Login failed for user 'Rfid\Mert'.
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)

2
If you don't need connectivity in the designer try looking at this SO QuestionMark Hall
It seems like the one I'm looking for but what should be the dependency object argument in the method where "this" is used? I'm using MVVM. DesignerProperties.GetIsInDesignMode(this)Mert Akcakaya
This may be more to your needs stackoverflow.com/questions/2498521/…Mark Hall
Yeah, assigning DataContext in the code-behind solved my issue. Thank you!Mert Akcakaya

2 Answers

0
votes

Go through the connection string used for the database to connect. If you are using MSSQL database for your website then refer to the sample connection string which is given below:

where Data Source=xxxx; = IP address of the server Initial Catalog=xxxxxx = database name User ID=xxxxxx = database user name Password=#xxxxx#" = db password

Make sure that you are able to connect to the SQL Server Management Studio remotely with the database details(Username and password).

If you are unable to connect to the database with the username and password which you have used in the connection string, then try to reset the password of the database user and try to connect it. It will resolve the issue...

You can also look at the following link that may help you: http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/

0
votes

The solution came by moving my context binding code to code-behind file, so XAML parser does not try to access DB while I am designing the UI.

this.DataContext = new MyViewModel();

If you don't like the code-behind approach (for some reason), you can move it back to your XAML code when you finish designing UI.