I'm working on simple ASP .NET MVC Application. I have a simple local database in it. This database contains a few tables. I made model, view and controller for one of this table. I also made DBContext class and Connection string. Everything seems work fine except that using context from controller return empty set. I'm sure that table contains at least one record. I don't know what is wrong.
There's my Table Schema:
CREATE TABLE [dbo].[SportsVenues] (
[Id] INT NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[Description] TEXT NOT NULL,
[Country] NVARCHAR (100) NULL,
[City] NVARCHAR (100) NULL,
[Street] NVARCHAR (100) NULL,
[Area] DECIMAL (18) NULL,
[PictureUrl] NVARCHAR (100) NULL,
[OppeningYear] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
There's model:
[Table("SportsVenues")]
public class SportsVenue
{
[Column("Id")]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Description")]
public string Description { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Street { get; set; }
public double Area { get; set; }
public DateTime OppeningYear { get; set; }
}
There's Database Context class:
public class StecSportsDBContext : DbContext
{
public DbSet<StecSports.Models.Trick> Tricks { get; set; }
public DbSet<StecSports.Models.Event> Events { get; set; }
public DbSet<StecSports.Models.SportsVenue> Venues { get; set; }
}
And there's my connection string:
<add name="StecSportsDBContext"
connectionString="Data Source=|DataDirectory|\StecSports.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
I've also checked query send to database, and after type it in new query page it return data as expected. I'm using visual studio 2012 on windows 7. How can I get data stored in database? What I'm doing wrong?
As Eric J. requested In controller I simply use property venues from DbContext class:
ViewBag.venuesList = db.Venues;
which is initialized as:
private StecSportsDBContext db = new StecSportsDBContext();
And it gives me sql query like this:
{SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[Country] AS [Country],
[Extent1].[City] AS [City],
[Extent1].[Street] AS [Street],
[Extent1].[Area] AS [Area],
[Extent1].[OppeningYear] AS [OppeningYear]
FROM [SportsVenues] AS [Extent1]}
I'm sure this query works and return right data, because I copied it and run on database server.