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); } 
  • Connection string on the server points to the desired database? - tym32167 2:26 pm
  • @ tym32167, yes, and creates additional tables in it. - LokenGarvel '
  • @ tym32167, MyClasses but this name I do not use in the project, anywhere. - LokenGarvel '
  • 2
    I understand that you are not using the Database First approach on a manually created database? EF has its own class name logic; try explicitly specifying table names through the DataAnnotations attribute - TableAttribute for the class MyClass: [Table("MyClass")] or with the [Table("MyClass", Schema="dbo")] scheme. - Alex Krass
  • You use MyClass in the data context, which generates the database schema at startup, replacing MyClass with the plural MyClasses - tym32167

1 answer 1

Thank you @Alex Krass for the comment. [Table("MyClass", Schema="dbo")] solved the problem.