Solved

Keep getting a Database Error in my WCF MVC Website.

Posted on 2013-06-10
11
408 Views
Last Modified: 2013-06-22
Hello,

I have a WCF and Mvc project, the project has been split up into different sections;

DataAccess project - this is essentially a class library which has all the database functionality in (i.e. Connection strings, Database file)

Wcf middleware - this is the WCF service project.

Website

The project is essentially a Mvc Form with some textboxes and a submit button.

The problem i have is everytime I debug the project, i keep getting this error;

Cannot open database "ViewModelsContext" requested by the login. The login failed.
Login failed for user 'NicksPC\Nick.Ward'.


this is my connection string;

<add name="ViewModelsContext" connectionString="Server=.\SQLEXPRESS;AttachDBFilename=C:\Development\DevProjects\Uploader\Main\Source\FrontEnd\Web\Uploader.DataAdapter\App_Data\UploaderDB.mdf;Database=UploaderDB;Trusted_Connection=Yes" providerName="System.Data.SqlClient"/>

    public class ViewModelsContext : DbContext
    {
        public ViewModelsContext()
            : base("ViewModelsContext")
        {
            Database.SetInitializer<ViewModelsContext>(null);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
           base.OnModelCreating(modelBuilder);
        }
       
        public DbSet<Uploader.Customer> Customers { get; set; }
    }
}

The connection string is stored in the App.Config file which is in the DataAccess Project

The Middleware project calls the DataAccess layer, shown;

        public Customer SaveCustomerDetails(RetailUploader.Customer customerDetails)
        {
            CustomerData cust = new CustomerData();
            //cust.SaveCustomers(customerDetails);
            return cust.SaveCustomers(customerDetails);
        }

In the website project, it calls the WCF service, shown;

        [HttpPost]
        public ActionResult Upload(Customer cust)
        {
            if (ModelState.IsValid)
            {
                CustomerDataUpload test = new CustomerDataUpload();

                test.SaveCustomerDetails(cust);
            }
            return View();
        }

I have also mapped the model in the textbox controls, sample shown;

<%= Html.TextBoxFor(model => model.Title, new { id = "first_name", @class = "uploader_form_input required minlength alpha" })%>

I am just abit baffled as to why it keeps complaining about the Database Connection.

Thanks,
Nick Ward
0
Comment
Question by:nickward26
  • 7
  • 4
11 Comments
 
LVL 9

Accepted Solution

by:
MattSQL earned 80 total points
ID: 39236711
The key component here is:

Login failed for user 'NicksPC\Nick.Ward'

Are you able to confirm that this windows account is mapped to a login in SQL Server, and has the required access to the ViewModelsContext database?
0
 

Assisted Solution

by:nickward26
nickward26 earned 0 total points
ID: 39236900
That could be the issue, but is there any easy way of just simply using a local sql server instance without having to mess with logins?
0
 
LVL 9

Assisted Solution

by:MattSQL
MattSQL earned 80 total points
ID: 39236912
:)

 No - you must have a login, or be a member of a group that has a login to get into SQL Server.
0
 

Assisted Solution

by:nickward26
nickward26 earned 0 total points
ID: 39236914
Hi,

I can confirm that I have tested this on a seperate MVC project, this was just an MVC website without seperating the project and that works.

But, it just seems that when I seperate the Database Functionality and put all this in a seperate project called DataAccess, could that the cause of the error?

It seems to be reading the App.Config file where the connection string is and I am passing in a WCF service in the Website project to call the Database.

So, could there be anything wrong in my connection string?

I have changed my connection string and doesnt seem to be making any difference.

<add name="ViewModelsContext" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|UploaderDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

Thanks,
Nick
0
 

Assisted Solution

by:nickward26
nickward26 earned 0 total points
ID: 39236916
Ah, ok, would SQL compact work?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 9

Assisted Solution

by:MattSQL
MattSQL earned 80 total points
ID: 39236917
Can you try your original connection string without the add name="ViewModelsContext" ?
0
 

Assisted Solution

by:nickward26
nickward26 earned 0 total points
ID: 39236943
i have tried that and still getting the error about my login credentials.

  <connectionStrings>
    <connectionString="Server=.\SQLEXPRESS;AttachDBFilename=C:\Development\DevProjects\Uploader\Main\Source\FrontEnd\Web\Uploader.DataAdapter\App_Data\UploaderDB.mdf;Database=UploaderDB;Trusted_Connection=Yes" providerName="System.Data.SqlClient"/>
  </connectionStrings>
0
 

Assisted Solution

by:nickward26
nickward26 earned 0 total points
ID: 39237005
Hi,

I have commented out the connection string in the App.Config file and have hard coded the connection string in the ViewModelsContext class, shown below;

    public class ViewModelsContext : DbContext
    {
        string connectionString = @"Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|UploaderDB.mdf;Integrated Security=True;User Instance=True";

        public ViewModelsContext()
            : base()
        {
            Database.SetInitializer<ViewModelsContext>(null);
            Database.Connection.ConnectionString = connectionString;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
           base.OnModelCreating(modelBuilder);
        }
       
        public DbSet<Uploader.Customer> Customers { get; set; }
    }

Now im getting a different error message (below);

An attempt to attach an auto-named database for file C:\Development\DevProjects\Uploader\Main\Source\FrontEnd\Web\Uploader.Website\App_Data\UploaderDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
0
 

Assisted Solution

by:nickward26
nickward26 earned 0 total points
ID: 39237053
Hi,

I have finally got it working now, basically, there was a problem reading the connection string from the App.Config file so I hardcoded the connection string in the ViewModelsContext class and now it works!

   public class ViewModelsContext : DbContext
    {
        string connectionString = @"Server=.\SQLEXPRESS;AttachDbFilename=C:\Development\DevProjects\Uploader\Main\Source\FrontEnd\Web\Uploader.DataAdapter\App_Data\UploaderDB.mdf;Integrated Security=SSPI;User Instance=True";

        public ViewModelsContext()
            : base("ViewModelsContext")
        {
            Database.SetInitializer<ViewModelsContext>(null);
            Database.Connection.ConnectionString = connectionString;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
           base.OnModelCreating(modelBuilder);
        }
       
        public DbSet<Uploader.Customer> Customers { get; set; }
    }
}
0
 
LVL 9

Assisted Solution

by:MattSQL
MattSQL earned 80 total points
ID: 39239287
Eureka!

:)
0
 

Author Closing Comment

by:nickward26
ID: 39267759
The advise i was recieved was excellent and helped to solve my problem,
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now