[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Keep getting a Database Error in my WCF MVC Website.

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
nickward26
Asked:
nickward26
  • 7
  • 4
10 Solutions
 
Matt BowlerDB team leadCommented:
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
 
nickward26Author Commented:
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
 
Matt BowlerDB team leadCommented:
:)

 No - you must have a login, or be a member of a group that has a login to get into SQL Server.
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
nickward26Author Commented:
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
 
nickward26Author Commented:
Ah, ok, would SQL compact work?
0
 
Matt BowlerDB team leadCommented:
Can you try your original connection string without the add name="ViewModelsContext" ?
0
 
nickward26Author Commented:
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
 
nickward26Author Commented:
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
 
nickward26Author Commented:
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
 
Matt BowlerDB team leadCommented:
Eureka!

:)
0
 
nickward26Author Commented:
The advise i was recieved was excellent and helped to solve my problem,
0

Featured Post

Industry Leaders: 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!

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now