Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting Nulls in my DbContext tables and would be grateful for a little help.

Model:
C#
[Table("tblCustomer")]
    public class Customer
    {
        [Key]
        public int CustomerID { get; set; }
        public virtual ICollection<bike> Bikes { get; set; }
        [Display(Name = "Customer")]
        public string CustomerFirstName { get; set; }
        [Display(Name = "Surname")]
        public string CustomerSurName { get; set; }
    }


DbContext:
C#
public class BikesDbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }


ConnectionString in App.Config:
HTML
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>
<connectionstrings>
    <add name="BikesDbContext" connectionString="server =(local)\SQLExpress;  AttachDbFilename=F:\Database\Bikes.mdf; database= db_Bikes; 
integrated security = SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>


Call in code:
C#
BikesDbContext db = new BikesDbContext();

      var Dataset = db.Customers.Where
                     (x => x.CustomerSurName.StartsWith("S"));


I am not seeing anything in the debug in db.Customers when I set a breakpoint at the var Dataset line and would be grateful for any help that anyone can give. The Database can be seen in the Server explorer and has entries in the table that begin with "S" in the Surname. It does appear to me that the DbContext is not really connected to the database.

Thanks

What I have tried:

I have tried all the above and am still getting Nulls in the Table from DbContext
Posted
Updated 9-May-18 12:09pm
v5

1 solution

After some Trial and Error, I have managed to find the errors.

Firstly, I didn't inherit from DbContext in my Context, therefore, my context class now looks like this

C#
public class BikesDbContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }


Notice the difference where I now correctly Inherit.

In addition, I found that I do not need to use the "AttachDbFilename" in the Connection String in the App.Config

This now provides a Dataset in the linq populated with customers starting with "S"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900