Solved

Keep getting a Database Error in my WCF MVC Website.

Posted on 2013-06-10
11
411 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

822 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