A Houghton
A Houghton

Reputation: 392

MVC Model creating a table in database rather than using existing

I'm trying to simply access information in a existing database using vb.net MVC. The issue is, when i run the code, it creates a table in the database, with a name similar to the table I'm attempting to access with an 's' appended on the end.

e.g:

Existing table: VXPartsData.

Table created after running application: VXPartsDatas

I've managed to do this in c sharp quite easily, but don't understand where I'm going wrong using VB. If someone could please point me in the right direction, thanks

Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.ComponentModel.DataAnnotations
Imports System.Data.Entity


 Namespace Models

<Table(Name:="dbo.VXPartsData")>
    Public Class VXPartsData

    <Key()>
    Public Property PartNo As String
    Public Property CustPart As String
    Public Property ShortDesc As String
    Public Property Part As String
    Public Property Price As Decimal
    Public Property DiscCode As String
    Public Property Kit As Boolean
    Public Property VXCODE As String

End Class

Public Class ProductContext
    Inherits DbContext
    Public Property VXPartsDatas As DbSet(Of VXPartsData)
End Class


End Namespace

Controller:

Imports System.Data.Entity
Imports Videx_PB2.Models

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Private db As New ProductContext

    Function Index() As ActionResult

        Return View(db.VXPartsDatas.ToList())

    End Function

End Class

Connection String:

 <connectionStrings>
    <add name="ProductContext" connectionString="server=(localdb)\MSSQLLocalDB; database=VidexPB; integrated security=SSPI" providerName="System.Data.sqlClient" />
  </connectionStrings>

Upvotes: 0

Views: 85

Answers (1)

user449689
user449689

Reputation: 3154

In your ProductContext class, add the following code:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ...

    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    ...

    base.OnModelCreating(modelBuilder);
}

EF will not pluralize table names

Upvotes: 1

Related Questions