473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Declaring Navigation Property in Entity Framework

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
1 8331
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

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

Similar topics

1
by: TOMERDR | last post by:
Hi, I have Visual studio.NET 2005 on XP proffessional Is it possible to me to use LINQ and ADO.NET Entity Framework? Can LINQ and ADO.NET Entity Framework be used with express editions as...
0
by: Andy B | last post by:
I wanted to try out the ado.net entity framework model and see how it was different from linq to sql entities. I put a page in a web application project, added an ado.net entity model and told it...
9
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
1
by: Cirene | last post by:
Do you have any good book recommendations for learning Entity Framework, DAAB, and/or Tiered Architecture (business objects, etc)? I am a VB.net developer, but C# would be ok I guess.... Thanks
3
by: chris.kennedy | last post by:
I am really struggling to create an entity which maps to 2 "base" entities. I have a customer and invoice entity which are based in tables in a one to many relationship. I have tried: 1....
5
by: John | last post by:
Hi I have installed vs2008/.net3.5 SP1 (details below) but I still can't see ADO.Net Entity Framework under 'New Item' in my WinForm vb.net solution. What am I doing wrong? Thanks Regards
1
by: markla | last post by:
Hi, I have an Entity data model built in Entity Framework, which sources data primarily from an MS SQL 2008 database, and sources some static (data dictionary) values from code-based objects. I...
1
by: Andy B | last post by:
I have to learn how to do one or the other: linq to sql or ado.net entity framework. There will be overhead no matter what way I go since I don't know either one. Which one would be the best one to...
1
by: Andy B | last post by:
I have the following code and can't quite figure out why it doesn't work. in the first section, I am trying to take the data from a entity framework function import and use it inside a repeater. ...
3
by: Andy B | last post by:
I have about 12 services that will be used on the website I am working on. A few of them are News, Events, Venues (that work directly with Events), Mailing lists and some others. Each one of these...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.