Test
Test

Reputation: 315

Why is entity framework trying to find these non-existant columns?

public partial class User {

         public int Id { get; set; }

         public Nullable<int> InvoiceAddress_Id { get; set; }

         public Nullable<int> MailAddress_Id { get; set; }


         public virtual Address Address { get; set; }

         public virtual Address Address1 { get; set; }
}

When I try to retrieve user using Linq and Entity framework, I get;

Invalid column name 'Address_Id1'.
Invalid column name 'Address1_Id1'.
Invalid column name 'Address_Id'.
Invalid column name 'Address_Id1'.

I have no idea why it's coming up like that and doing search for any of these columns in the solution gives nothing! I have a feeling it has something to do with foreign keys.

 public partial class Address
{
    public Address()
    {
        this.User = new HashSet<User>();
        this.User1 = new HashSet<User>();
    }
    [Key]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string City { get; set; }

    public virtual ICollection<Customer> User { get; set; }
    public virtual ICollection<Customer> User1 { get; set; }

}

Here is how I'm using entity framework:

CustomDbContext db = new CustomDbContext ();
var user =  db.User.First(a => a.Id != 0);

Here's the updated code:

public partial class User
{

    public int Id { get; set; }

    public Nullable<int> InvoiceAddress_Id { get; set; }
    public Nullable<int> MailAddress_Id { get; set; }

    public virtual Address InvoiceAddress_ { get; set; }
    public virtual Address MailAddress_ { get; set; }
}

Here's the error:

Invalid column name 'Address_Id'.
Invalid column name 'Address_Id1'.

Upvotes: 0

Views: 107

Answers (1)

JotaBe
JotaBe

Reputation: 39015

The problem is quite clear: nor your code (attributes, fluent API), neither the EF conventions are configuring the FKs.

If you want to use conventions, you have to adjust the names of the properties and the foreign keys, so that they can be configured. Where you have:

public Nullable<int> InvoiceAddress_Id { get; set; }
public virtual Address Address { get; set; }

You should have:

public Nullable<int> InvoiceAddressId { get; set; }
public virtual Address InvoiceAddress { get; set; }

Note that the FK name is the navigation property name + "Id"

Other option is to implement OnModelCreating of your DbContext and configure the FKs using the fluent API:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>()
            .HasOptional(u => u.Address)
            .WithMany();
            .HasForeingKey(a => a.InvoiceAddres_Id);
    }

Or use the [ForeignKey("")] attribute:

The annotation may be placed on the foreign key property and specify the associated navigation property name, or placed on a navigation property and specify the associated foreign key name

I.e.

[ForeignKey("Address")]
public Nullable<int> InvoiceAddress_Id { get; set; }

or

[ForeignKey("InvoiceAddress_Id")]
public virtual Address Address { get; set; }

Upvotes: 2

Related Questions