Table Name Mapping not working in EF 6 when using POCO classes

c# entity-framework-6 sql-server-2008-r2

Question

I am using System.ComponentModel.DataAnnotations.Schema.TableAttribute in class below, so class 'Order_Details' is mapped to table 'Order Details' in Northwind database.

There is an attribute [Table("Order Details"] just above class definition to map it to a database table.

The problem is when a LINQ query executes against the database, it tries to query 'Order_Details' table in the database, when it should be querying 'Order Details' table.

Question: Do I have to use some other attribute for mapping this class to 'Order Details' table in the database? This POCO class ( without the table attribute) was generated by a third-party code generator using Entity Framework.

[Table("Order Details")]
public class Order_Details
{
    [Key]
    [Column(Order = 0)]
    [Required]
    [Display(Name = "Order I D")]
    public Int32 OrderID { get; set; }

    [Key]
    [Column(Order = 1)]
    [Required]
    [Display(Name = "Product I D")]
    public Int32 ProductID { get; set; }

    [Required]
    [Display(Name = "Unit Price")]
    public Decimal UnitPrice { get; set; }

    [Required]
    [Display(Name = "Quantity")]
    public Int16 Quantity { get; set; }

    [Required]
    [Display(Name = "Discount")]
    public Decimal Discount { get; set; }

    // ComboBox
    public virtual Orders Orders { get; set; }
    public virtual Products Products { get; set; }

}

UPDATE 1:

Since there was no edmx file and it was using 'Code first fluent API' as suggested by Jeow Li Huan in his answer, I add a table mapping for 'Order Details' in modelcreating event and it worked then.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //added this in place of Table attribute and it works
            modelBuilder.Entity<Order_Details>().ToTable("Order Details");

            modelBuilder.Entity<Orders>().ToTable("Orders");
            modelBuilder.Entity<Products>().ToTable("Products");
            modelBuilder.Entity<Customers>().ToTable("Customers");
            modelBuilder.Entity<Employees>().ToTable("Employees");
            modelBuilder.Entity<Shippers>().ToTable("Shippers");
            modelBuilder.Entity<Suppliers>().ToTable("Suppliers");
            modelBuilder.Entity<Categories>().ToTable("Categories");

        }

UPDATE 2:

Also, another solution as suggested by Jeow, was to remove the model creating event and just use the original table attribute. This also worked.

1
2
9/6/2014 4:25:13 AM

Accepted Answer

Code first fluent API

Did the tool you use also generated a DbContext with OnModelCreating method that uses code to map your Order_Details to Order_Details table?

If there is, remove it.

Database/model first

Did the tool you use generate .edmx file that causes all the attributes on the class to be ignored? (Check under your connection string for metadata=res://*/Models.Model.csdl, which implies that .edmx is being used).

If there is .edmx, then you'll have to map the table using that file.

I think the old code first works this way

Did the tool store the edmx schema in the database? Look for tables you did not create in your database.

If there is edmx schema in the db, you'll have to modify that.

2
9/6/2014 2:33:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow