2

I have class like this:


public class Target
{
    [Key]
    [Column("TargetId")]
    public Guid Id { get; set; }

    [ForeignKey("TargetInventory")]
    public Guid? TargetInventoryId { get; set; }
    public virtual TargetInventory TargetInventory { get; set; }
}

On DbContext only this:


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

On database I have FK named 'FK_Target_TargetInventory' and field 'TargetInventoryId'.

With EF 4 it worked but on 4.1 it generates invalid field name 'TargetInventory_Id' instead of 'TargetInventoryId'. Exception:

Load operation failed for query 'GetAll'. An error occurred while executing the command definition. See the inner exception for details. Inner exception message: Invalid column name 'TargetInventory_Id'.

What could be wrong?

Edit 1

Code tag is not rendering well (I think you can understand what it means): modelBuilder.Conventions.Remove<<'IncludeMetadataConvention'>>(); I havent used fluent before and it worked, I don't want to have logic outside main class. Now I will try various tests as I found similiar code working in different class.

1 Answers1

0

What convention are you removing? Don't touch conventions. They are responsible for automatic pairing of FK property with navigation property and they are also responsible for converting data annotations to mapping. If your code should mean that you are removing all conventions you must configure everything with fluent API.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Hi, I updated my post. Also about conventions remove, I added that line to get rid of exception related to DB recreate or smth like that. – Aurimas Neverauskas Jun 27 '11 at 09:49
  • As this relationship is two-way one-to-many it works as you have wrote here [link](http://stackoverflow.com/questions/6228600/ef-code-first-many-to-many-and-one-to-many). There're places in code that are one-way and it works without FLUENT. So what happened that I must to use it now? – Aurimas Neverauskas Jun 27 '11 at 15:04