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

asp.net c# entity-framework sql-server

Question

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.

1
1
8/15/2016 11:15:49 AM

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

1
8/15/2016 9:44:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow