Solved

Keep getting a Database Error in my WCF MVC Website.

Posted on 2013-06-10
11
407 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
Comment Utility
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
Comment Utility
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
Comment Utility
:)

 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
Comment Utility
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
Comment Utility
Ah, ok, would SQL compact work?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Assisted Solution

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

Assisted Solution

by:nickward26
nickward26 earned 0 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Eureka!

:)
0
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

12 Experts available now in Live!

Get 1:1 Help Now