cancel
Showing results for 
Search instead for 
Did you mean: 

EF Code First Not Returning Any Results

Former Member
0 Kudos
4,007

I am testing EF6 code first against a simple table in an existing SQL Anywhere 12.0.1 database. I tested DB first and had no problems querying with that method. But my attempts with code first won't work.

Here's the table:

CREATE TABLE "DBA"."COUNTRY" (
    "country_id"                     char(2) NOT NULL
   ,"name"                           char(30) NOT NULL
   ,"country_code"                   varchar(3) NULL
   ,PRIMARY KEY ("country_id") 
)
go

Imagine the data in the table as 'CA','Canada','CAN' 'US','United States','USA' 'FR','France','FR' etc...

The model class:

<Table("COUNTRY")>
Partial Public Class COUNTRY
    <Key>
    Public Property country_id As String
    Public Property name As String
    Public Property country_code As String
End Class

And the DbContext class:

Partial Public Class MyEntities
    Inherits DbContext
Public Sub New() MyBase.New("name=MyConnStr") End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder) Data.Entity.Database.SetInitializer(Of MyEntities)(Nothing) MyBase.OnModelCreating(modelBuilder) End Sub
Public Overridable Property Country() As DbSet(Of COUNTRY)

End Class

When I attempt to do code like the following: Dim db As New MyEntities Dim lstCountry = db.Country.ToList()

lstCountry.Count returns zero... What am I doing wrong?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member

Oh man... I'm so stupid. 🙂

I just needed to add the schema to table attribute. <Table("COUNTRY", Schema:="dba")>

So... anyway to assign a default schema to EF mappings?

jeff_albion
Product and Topic Expert
Product and Topic Expert

See: http://stackoverflow.com/questions/9562883/can-i-change-the-default-schema-name-in-entity-framework-...

This method exists in EF6:

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("dba");
    }
}