[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Keep getting a Database Error in my WCF MVC Website.

Posted on 2013-06-10
11
Medium Priority
?
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 10

Accepted Solution

by:
Matt Bowler earned 320 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 10

Assisted Solution

by:Matt Bowler
Matt Bowler earned 320 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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 10

Assisted Solution

by:Matt Bowler
Matt Bowler earned 320 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 10

Assisted Solution

by:Matt Bowler
Matt Bowler earned 320 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
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.

649 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