Barry Doyle Barry Doyle - 3 months ago 48
ASP.NET (C#) Question

System.Data.SqlClient.SqlException: Invalid object name 'dbo.Slider'

I'm developing an

asp.NET
application using
Entity Framework
and I'm getting the following
Exception
whenever I try to create a record in my
dbo.Slider
table:

System.Data.Entity.Infrastructure.DbUpdateException:
An error occurred while updating the entries.
See the inner exception for details. ---> System.Data.Entity.Core.UpdateException:
An error occurred while updating the entries.
See the inner exception for details. ---> System.Data.SqlClient.SqlException:
Invalid object name 'dbo.Slider'. at ...


I'm using an
Entity Framework Service
called
efService
that I created and call on my code-behind form:

protected Services.EFService efService = new Services.EFService();


I try to create the slider on a button click and here's the section of code I use before it breaks in the button:

slider.DateCreated = DateTime.Now;
slider.Caption = txbPhotoCaptionCreate.Text;
slider.IsPublic = cbxPhotoPublicCreate.Checked;
slider.Path = string.Empty;
slider.ThumbnailPath = string.Empty;
slider.DisplayPath = string.Empty;

int sliderId = efService.CreateSlider(slider); // It breaks here


Here is the
CreateSlider efService
that I've made:

public int CreateSlider(Models.EF.Slider slider)
{
DB.Sliders.Add(slider);
DB.SaveChanges();

return slider.Id;
}


This all works on my other tables which have pretty much the exact same functionality.

Here's the SQL I use to create my
Slider
table:

CREATE TABLE [dbo].[Slider] (
[Id] INT IDENTITY(1,1) NOT NULL,
[Caption] VARCHAR (50) NOT NULL,
[Position] INT NOT NULL DEFAULT 1,
[IsPublic] BIT NOT NULL DEFAULT 1,
[Path] NVARCHAR (250) NULL,
[ThumbnailPath] NVARCHAR (250) NULL,
[DisplayPath] NVARCHAR (250) NULL,
[DateCreated] DATETIME NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);


There are no build errors, the whole application runs fine except when I try to create a record for
Slider
. Through a lot of debugging I can conclude that the problem occurs when the
DB.SaveChanges()
occurs.

Answer

As I stated in the comments, you need to make sure that the context in pointing to the same database as the one which you created the table in.

You can do that by debuging the method CreateSlider and checking the DB object, it has a Database property which should hold the excepted ConnectionString.