Witam,
Oglądałem taki kurs ASP.NET MVC i pokazane jest jak wyciągnąć dane z bazy danych. Jakoś wygląda to nadzwyczaj prosto bo nawet nie trzeba nigdzie tej bazy wrzucać i synchronizować?
Ogólnie autor robi to tak:
Tworzy model i dodaje tam kluczowy kod: [Table("tblUsers")] - bo tak się nazywa tabela w SQL "tblUsers"
[Table("tblUsers")]
public class User
{
public int id { get; set; }
public string name { get; set; }
public string surname { get; set; }
public string email { get; set; }
}
Kontroler, który pokazuje tabelę:
public ActionResult Table(int id)
{
UserContext userContext = new UserContext();
User user = userContext.Users.Single(usr => usr.id == id);
return View(user);
}
No i Vidok, który opisuję tą tabelę.
Następnie kolejny model UserContext:
namespace MVCDemo.Models
{
public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }
}
}
I teraz oczywiście musi być zainstalowane Entity a do tego dopisuje w Web.Config:
<connectionStrings>
<add name="UserContext"
connectionString ="server=HP\SQLEXPRESS; database=Sample; integrated security=SSPI"
providerName ="System.Data.SqlClient"/>
</connectionStrings>
oraz do Global.asax.cs:
Database.SetInitializer<MVCDemo.Models.UserContext>(null);
Moje pytanie brzmi czy to ma prawo zadziałać bo ja to próbowałem podpiąć do mojej bazy danych krok po kroku jak w tutorialu no i niestety biała strona żadnych danych z bazy.