15

I using "Entity Framework DbContext" at the moment I have got exception towars.dbo was not found. This is very strange because in my website I all the time ask about towar.dbo but no towars.dbo Do you know where is a problem?

- InnerException    {"Invalid object name 'dbo.Towars'."}   System.Exception {System.Data.SqlClient.SqlException}

My all things about Towar (of course different place in my program):

public class ProductController : Controller
{
    //
    // GET: /Product/
        public ITowarRepository repository;

        public ProductController(ITowarRepository productRepository) 
        {
        repository = productRepository;
        }

        public ViewResult List()
        {
            return View(repository.Towar);
        }

}


public interface ITowarRepository
    {
        IQueryable<Towar> Towar { get; }
    }

public DbSet<Towar> Towar { get; set; }

public class EFTowarRepository : ITowarRepository
    {
        public EFDbContext context = new EFDbContext();
        public IQueryable<Towar> Towar
        {
            get { return context.Towar; }
        }
    }
public class Towar
    {
        [Key]
        public int Id_tow { get; set; }
        public string Nazwa { get; set; }
        public string Opis { get; set; }
        public decimal Cena { get; set; }
        public int Id_kat { get; set; }
    }
RPD
  • 494
  • 3
  • 8
  • 24

4 Answers4

14

Add the following line to your context:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
Forty-Two
  • 7,535
  • 2
  • 37
  • 54
9

You can tell EF to map to the table Towar by overriding the OnModelCreating method in your DBContext class with fluent API like this:

public class EFDbContext : DbContext
{
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
      modelBuilder.Entity<Towar>().ToTable("Towar");
   }
}

Now EF will look for Towar table instead of Towars. If you do not have these tables created, there is some other problem you are having.

FranciscoBouza
  • 590
  • 6
  • 19
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks for help :) I don't understand why they create this stupid thing in Visual Studio but I`am very glad for your help. – RPD Aug 03 '12 at 16:14
6

EF Code First automatically pluralizes the table names. Use a [Table] attribute to explicitly map the entity to a table name:

[Table("Towary")]
public class Towary
{
   // Whatever properties
}

It looks like there's a way to disable pluralization gobally too, see Entity Framework Code First naming conventions - back to plural table names?.

Community
  • 1
  • 1
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • Error 1 'Name' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static. C:\Users\Rafal\Desktop\MVC ksiązka\moj projekt\sklep\SportsStore\Entities\Product.cs 9 12 SportsStore.Domain – RPD Aug 03 '12 at 16:04
  • Sorry, I read the wrong docs. It should be just `[Table("Towary")]`. – Anders Abel Aug 03 '12 at 16:07
3
using System.Data.Entity.ModelConfiguration.Conventions;

namespace MVCDemo.Models

     {
         public class EmployeeContext : DbContext

         protected override void OnModelCreating(DbModelBuilder modelBuilder)
         {
             modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
         }
}

For the sake of completeness @forty-two

Crismogram
  • 906
  • 15
  • 27