2

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.

4
  • 3
    You showed everything but the code that actually tries to query the database. Commented May 11, 2015 at 18:13
  • I simply use function from context like ViewBag.venuesList = db.Venues; It gives me sql query like {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]}. db variable is intialized as private StecSportsDBContext db = new StecSportsDBContext(); Commented May 11, 2015 at 18:16
  • Please edit your question with that code rather than adding a comment. Commented May 11, 2015 at 18:17
  • Make absolutely sure that you're accessing exactly the same database at runtime that you think you are. Check full path of the database at runtime. I spent two full days fighting this issue in the past week. Commented Mar 15, 2017 at 14:51

1 Answer 1

1

You have to enumerate your query. You can use .ToList() to get your results.

ViewBag.venuesList = db.Venues.ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

Actually db.Venues.Count() returns 0. I think something is wrong with my connection string.
after instantiating your context, check your connection string to make sure it is picking it up from the config correctly: db.Database.Connection.ConnectionString
It's ok. This same as I've puted into Web.config file.
did you get anywhere after this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.