0

This is my method with the linq expression:

    FantasySport _db = new FantasySport();

    [AllowAnonymous]
    public ActionResult Register()
    {
        var q = from t in _db.User
                 select t;
        foreach (var item in q) {
            var a = item;
        }
        return View();
    }

This gives me 0 rows back. If i put a breakpoint on the row var a = item; it won't even stop there because there is no items in q. This is the data in my database:

enter image description here

Here is my dbset class:

namespace SportsFantasy.Models
{
   public class FantasySport : DbContext
   {
      public DbSet<UserProfile> User { get; set; }
      public DbSet<Team> Team { get; set; }
      public DbSet<TeamPlayer> TeamPlayer { get; set; }
      public DbSet<Player> Player { get; set; }
      public DbSet<Games> Games { get; set; }
      public DbSet<League> League { get; set; }
  }
}

What is the problem? Ive been looking for a answere a long time now. Why doesn't i get the values from the database? It's my first time doing a code-first EF project so it could be some easy thing i just missed.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78
  • Just to double check, are you sure EF is connecting to the correct database? No error seems to indicate that there actually is no data in the table it selects from. – Joachim Isaksson May 25 '14 at 18:21
  • I don't know if it is the right one. How do i know? – Daniel Gustafsson May 25 '14 at 18:24
  • @GertArnold i know i dont return it. But i don't do that because it doesn't get any values. I have a breakpoint right there and there is no value to return. – Daniel Gustafsson May 25 '14 at 18:27
  • Well, to be correct. The loop is executed, but you don't do anything with the result. You have to return the query result to the view. See [this](http://www.asp.net/mvc/tutorials/older-versions/views/asp-net-mvc-views-overview-cs). – Gert Arnold May 25 '14 at 18:30
  • @GertArnold i know that i have to return it to use the data. But my question was about to get the data out of the database and that is the problem that i want to solve. When i debugg the loop it doens't get any data from the database so there is nothing to return. – Daniel Gustafsson May 25 '14 at 18:38
  • @JoachimIsaksson how do i know if it's the right connectionstring? – Daniel Gustafsson May 25 '14 at 18:56
  • It should be the same connection string you're seeing in Visual Studio when you're displaying the data in the user table. – Joachim Isaksson May 25 '14 at 18:57
  • Okej, It is named defaultconnection in the usertable and in the connectionstring so then it should be thesame? @JoachimIsaksson – Daniel Gustafsson May 25 '14 at 18:59
  • It's the data source part that should be the same, the name of the connection is just a name for it that may be set freely by the user. – Joachim Isaksson May 25 '14 at 19:00
  • Data Source=(LocalDb)\v11.0;AttachDbFilename="C:\Users\Daniel\Documents\Visual Studio 2012\Projects\SportsFantasy\SportsFantasy\App_Data\SportsFantasy.mdf";Initial Catalog=SportsFantasy;Integrated Security=True They are a litte bit different but im not sure it matters. @JoachimIsaksson – Daniel Gustafsson May 25 '14 at 19:11
  • @DavidG you mean the class in the bottom of my question? or do you mean anything else? – Daniel Gustafsson May 25 '14 at 19:18
  • @DanielGustafsson Then it'd depend on what [`DataDirectory`](http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented) is set to. – Joachim Isaksson May 25 '14 at 19:40
  • @JoachimIsaksson i changed the connection string so that is writes the whole path instead of datadirectory thing but its still the same – Daniel Gustafsson May 25 '14 at 19:56

0 Answers0