Good day! There is an mvc project in which you need to get data from a table on MS SQL server, connectionString looks like this:
<connectionStrings> <remove name="LocalSqlServer" /> <add name="PrimaryConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source = MSI\SQLEXPRESS; Initial Catalog= MyBase;Integrated Security=True;" /> </connectionStrings> I get the data from the table, but for some reason not from the one I created, but from the one that is created on the server when the application is started, the actual question is how to get the data from my (filled) table, and not what the studio gives me.
Presentation Code:
@using Domain @model IEnumerable <MyClass> @{ Layout = null; } <!DOCTYPE html> <html> <head> <title> Index </title> </head> <body> <div> <h3>Шапка</h3> <table> <tr class="header"> <td><p>Владелец</p></td> <td><p>Название</p></td> <td><p>Описание</p></td> <td><p>Дата</p></td> <td></td> </tr> @foreach (MyClass one in Model) { <tr> <td><p>@one.ParentId</p></td> <td><p>@one.Title</p></td> <td><p>@one.Description</p></td> <td><p>@one.Created</p></td> </tr> } </table> </div> </body> </html> Actually controller:
Repository repository = new Repository(); return View(repository.GetMyClass()); And Repository:
private EFContext context; public Repository() { context = new EFContext(ConfigurationManager.ConnectionStrings[0].ConnectionString); } public IEnumerable<MyClass> GetMyClass() { return context.Collection; } public MyClass GetById(int Id) { return context.Collection.FirstOrDefault(x => x.Id == Id); }
[Table("MyClass")]or with the[Table("MyClass", Schema="dbo")]scheme. - Alex KrassMyClassin the data context, which generates the database schema at startup, replacingMyClasswith the pluralMyClasses- tym32167