473,549 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring Navigation Property in Entity Framework

90 New Member
Hi,

I developed a sample application in EF which has 3 tables
PersonDetails, BankDetails and FixedDepositDet ails. 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 'FixedDepositDe tails'. 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 8372
raghulvarma
90 New Member
Made a Mistake when adding the Foreign Key. Just need to add
BankDetails instead of Bank_Id
PersonDetails instead of Person_Id
FixedDepositDet ail 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
1612
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 well? Thanks in advance.
0
2658
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 to generate the model from existing database, went back to my empty page and put a GridView on it. I right clicked on the GridView and went to show...
9
2249
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 Framework. Though I am quite comfortable with ADO.NET and VB.NET. In your opinion, should I take a few days and learn it and utilize these...
1
1376
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
1971
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. Creating a Invoice Entity based on the Invoice and adding a mapping to both tables: I can't validate the model it says "Two entities with different keys...
5
3404
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
2343
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 know I *could* store the data dict values in SQL: for various reasons that's not the path I want to take. I have some lookups, which are based on...
1
1957
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 use overall?
1
3824
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. <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <% #Bind("Title") %> </ItemTemplate> </asp:Repeater>
3
2446
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 services will have some classes and enums that are contained in a namespace of their own. When it comes to the data access (entity framework), I...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6041
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.