By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

Declaring Navigation Property in Entity Framework

P: 90
Hi,

I developed a sample application in EF which has 3 tables
PersonDetails, BankDetails and FixedDepositDetails. Please find the table structure below

Expand|Select|Wrap|Line Numbers
  1. create table PersonDetails
  2. (PersonId int Primary Key,
  3. PersonName varchar(30))
  4.  
  5. create table BankDetails
  6. (BankId int Primary Key,
  7. BankName varchar(100),
  8. BankBranch varchar(100),
  9. BankLocation varchar(100))
  10.  
  11. create table FixedDepositDetails
  12. (
  13. Id int Primary Key,
  14. FixedDepositNo varchar(30) not null,
  15. Bank_Id int, -- references Bank Details
  16. Person_Id int, -- references Person Details
  17. DOD datetime,
  18. DOM datetime,
  19. DepositAmount decimal(9,2),
  20. MaturityAmount decimal(9,2),
  21. ROI decimal(3,2),
  22. Period varchar(20),
  23. Parent_Id int,
  24. Constraint fk_BankId Foreign Key(Bank_Id) references BankDetails(BankId),
  25. Constraint fk_PersonId Foreign Key(Person_Id) references PersonDetails(PersonId),
  26. Constraint fk_ParentId Foreign Key(Parent_Id) references FixedDepositDetails(Id)
  27. )
  28.  
  29.  
Now in the Entity framework I have specified as given below

Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2.     public class PersonDetails
  3.     {
  4.         [Key]
  5.         public int PersonId { get; set; }
  6.         public String PersonName { get; set; }
  7.     }
  8.  
  9.     [Serializable]
  10.     public class BankDetails
  11.     {
  12.         [Key]
  13.         public int BankId { get; set; }
  14.         public String BankName { get; set; }
  15.         public String BankBranch { get; set; }
  16.         public String BankLocation { get; set; }
  17.     }
  18.  
  19.     [Serializable]
  20.     public class FixedDepositDetails
  21.     {
  22.         [Key]
  23.         public int Id { get; set; }
  24.         public String FixedDepositNo { get; set; }
  25.  
  26.         [ForeignKey("Bank_Id")]
  27.         public int? Bank_Id { get; set; }
  28.         public BankDetails BankDetails { get; set; }
  29.  
  30.         [ForeignKey("Person_Id")]
  31.         public int? Person_Id { get; set; }
  32.         public PersonDetails PersonDetails { get; set; }
  33.  
  34.         public DateTime DOD { get; set; }
  35.         public DateTime DOM { get; set; }
  36.         public decimal DepositAmount { get; set; }
  37.         public decimal MaturityAmount { get; set; }
  38.         public decimal ROI { get; set; }
  39.         public String Period { get; set; }
  40.  
  41.         [ForeignKey("Parent_Id")]
  42.         public int? Parent_Id { get; set; }
  43.         public FixedDepositDetails FixedDepositDetail { get; set; }
  44.     }
  45.  public class BaseDBContext : DbContext
  46.     {
  47.         public BaseDBContext(string ConnectionString)
  48.             : base(ConnectionString)
  49.         {
  50.  
  51.         }
  52.  
  53.         public DbSet<PersonDetails> PersonDetails { get; set; }
  54.         public DbSet<BankDetails> BankDetails { get; set; }
  55.         public DbSet<FixedDepositDetails> FixedDepositDetails { get; set; }
  56.  
  57.         protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
  58.         {
  59.             modelBuilder.Entity<FixedDepositDetails>()
  60.                 .HasRequired(x => x.BankDetails)
  61.                 .WithMany()
  62.                 .HasForeignKey(y => y.Bank_Id);
  63.  
  64.             modelBuilder.Entity<FixedDepositDetails>()
  65.                .HasRequired(x => x.PersonDetails)
  66.                .WithMany()
  67.                .HasForeignKey(y => y.Person_Id);
  68.  
  69.         }
  70.     }
  71.  
  72.  
But when I run the application I get an error as

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

If I am not wrong I think I have made some mistakes when creating the model. Can some some please let me know where I am doing the mistake? If some one is able to provide me with a solution Please explain me as what you are doing and why that solution needs to be done?

Regards,

Raghul
Nov 3 '12 #1
Share this Question
Share on Google+
1 Reply


P: 90
Made a Mistake when adding the Foreign Key. Just need to add
BankDetails instead of Bank_Id
PersonDetails instead of Person_Id
FixedDepositDetail instead of Parent_Id
in the foreign key attribute.
Nov 5 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.