none
Navigation Property in Entity Framework RRS feed

  • Question

  • Hi,

    I am trying to develop an application by using EF, I am using AdventureWorks DB for storing Data, In that I am using Customers and CustomerAddress as tables. In that we have FK relationship between the two. When I specify the FK relationship I get an error as

    The navigation property 'CustomerID' is not a declared property on type 'CustomerAddress'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

    Please find my attached coding and Please let me know where am I doing wrong.

     class ModelContext : DbContext
        {
            public ModelContext(string connection)
                : base(connection)
            {
            }
    
            public DbSet<Customers> Customers { get; set; }
            public DbSet<CustomerAddress> Contact { get; set; }
    
            protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<CustomerAddress>()
                    .HasRequired(a => a.Customer)
                    .WithMany()
                    .HasForeignKey(u => u.CustomerID);
            }
        }
    
        [Serializable]
        [Table("Customer", Schema = "salesLT")]
        public class Customers
        {
            [Key]
            public int CustomerID { get; set; }
            public int NameStyle { get; set; }
            public string Title { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Suffix { get; set; }
            public string CompanyName { get; set; }
            public string SalesPerson { get; set; }
            public string EmailAddress { get; set; }
            public string MiddleName { get; set; }
            public string Phone { get; set; }
            public string PasswordHash { get; set; }
            public string PasswordSalt { get; set; }
            public Guid rowguid { get; set; }
            public DateTime ModifiedDate { get; set; }
    
            ICollection<CustomerAddress> lstContactCollections { get; set; }
        }
    
        [Serializable]
        [Table("CustomerAddress", Schema = "salesLT")]
        public class CustomerAddress
        {
            public Guid rowguid { get; set; }
            [Key, ForeignKey("CustomerID")] 
            public int? CustomerID { get; set; }
            public Customers Customer { get; set; }
            public int AddressID { get; set; }
            public string AddressType { get; set; }
            public DateTime ModifiedDate { get; set; }
            
        }

    Regards,

    Raghul

    Friday, April 27, 2012 10:37 AM

Answers

  • Hi RaghulVarmaMS!

    you should assign the ForeignKeyAttribute to the Navigation Property

    please change the CustomerAddress Class Declaretion fellowing

        [Serializable]
        [Table("CustomerAddress", Schema = "salesLT")]
        public class CustomerAddress
        {
            [Key]
            public Guid rowguid { get; set; }
            public int? CustomerID { get; set; }
            [ForeignKey("CustomerID")] 
            public Customers Customer { get; set; }
            public int AddressID { get; set; }
            public string AddressType { get; set; }
            public DateTime ModifiedDate { get; set; }
            
        }


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.


    Saturday, April 28, 2012 7:21 AM

All replies

  • Looks like you are using your foreign key attribute wrong, try removing it completely. I think what you actually want is this:

     [Serializable]
        [Table("CustomerAddress", Schema = "salesLT")]
        public class CustomerAddress
        {
            [Key]
            public Guid rowguid { get; set; }
            public int CustomerID { get; set; }
            public Customers Customer { get; set; }
            public int AddressID { get; set; }
            public string AddressType { get; set; }
            public DateTime ModifiedDate { get; set; }
            
        }
    this will allow you to bind a required customer using the CustomerID property as a Foreign Key with the modelbuilder.

    Saturday, April 28, 2012 4:34 AM
  • Hi RaghulVarmaMS!

    you should assign the ForeignKeyAttribute to the Navigation Property

    please change the CustomerAddress Class Declaretion fellowing

        [Serializable]
        [Table("CustomerAddress", Schema = "salesLT")]
        public class CustomerAddress
        {
            [Key]
            public Guid rowguid { get; set; }
            public int? CustomerID { get; set; }
            [ForeignKey("CustomerID")] 
            public Customers Customer { get; set; }
            public int AddressID { get; set; }
            public string AddressType { get; set; }
            public DateTime ModifiedDate { get; set; }
            
        }


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.


    Saturday, April 28, 2012 7:21 AM
  • Hi RaghulVarmaMS,

    Welcome to MSDN Forum.

    @Matthew LIN's code is correct, the FK declaration is wrong in the code. ForeignKey attribute which gave the target to "CustomerID" need to be added on navigation property. If you want to add the attribute on the "CustomerID", the target need to be the navigation property "Customer". Furthermore, you have declared foreign key with data annotation, so, the foreign key configuration in fluent api is not needed.


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 30, 2012 3:08 AM
    Moderator
  • Hi Allen,

    Thanks for your tip. I was able to solve my issue. Now when I try to do an include operation with  the same entities I am facing a issue. It states that

    A specified Include path is not valid. The EntityType 'EntityFramework_DBSet.Customers' does not declare a navigation property with the name 'CustomerAddress'.

    Please find my coding

      public List<Customers> GetTopTenCustomers()
    {
    ModelContext objModelContext = new ModelContext(System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks"].ToString());
    var GetTopTenCust = (from CD in objModelContext.Customers.Include("CustomerAddress")
    select CD).Take(10);
    return GetTopTenCust.ToList();
    }

    Regards,

    Raghul
    Monday, April 30, 2012 5:58 AM
  • Hi RaghulVarmaMS,

    Please look at the Customer entity, the navigation property for CustomerAddress named "lstContactCollections", not "CustomerAddress", so the exception was thrown. Please modify the path as "lstContactCollections ".

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 30, 2012 6:05 AM
    Moderator