Hi,
I developed a sample application in EF which has 3 tables
PersonDetails, BankDetails and FixedDepositDet ails. Please find the table structure below -
create table PersonDetails
-
(PersonId int Primary Key,
-
PersonName varchar(30))
-
-
create table BankDetails
-
(BankId int Primary Key,
-
BankName varchar(100),
-
BankBranch varchar(100),
-
BankLocation varchar(100))
-
-
create table FixedDepositDetails
-
(
-
Id int Primary Key,
-
FixedDepositNo varchar(30) not null,
-
Bank_Id int, -- references Bank Details
-
Person_Id int, -- references Person Details
-
DOD datetime,
-
DOM datetime,
-
DepositAmount decimal(9,2),
-
MaturityAmount decimal(9,2),
-
ROI decimal(3,2),
-
Period varchar(20),
-
Parent_Id int,
-
Constraint fk_BankId Foreign Key(Bank_Id) references BankDetails(BankId),
-
Constraint fk_PersonId Foreign Key(Person_Id) references PersonDetails(PersonId),
-
Constraint fk_ParentId Foreign Key(Parent_Id) references FixedDepositDetails(Id)
-
)
-
-
Now in the Entity framework I have specified as given below -
[Serializable]
-
public class PersonDetails
-
{
-
[Key]
-
public int PersonId { get; set; }
-
public String PersonName { get; set; }
-
}
-
-
[Serializable]
-
public class BankDetails
-
{
-
[Key]
-
public int BankId { get; set; }
-
public String BankName { get; set; }
-
public String BankBranch { get; set; }
-
public String BankLocation { get; set; }
-
}
-
-
[Serializable]
-
public class FixedDepositDetails
-
{
-
[Key]
-
public int Id { get; set; }
-
public String FixedDepositNo { get; set; }
-
-
[ForeignKey("Bank_Id")]
-
public int? Bank_Id { get; set; }
-
public BankDetails BankDetails { get; set; }
-
-
[ForeignKey("Person_Id")]
-
public int? Person_Id { get; set; }
-
public PersonDetails PersonDetails { get; set; }
-
-
public DateTime DOD { get; set; }
-
public DateTime DOM { get; set; }
-
public decimal DepositAmount { get; set; }
-
public decimal MaturityAmount { get; set; }
-
public decimal ROI { get; set; }
-
public String Period { get; set; }
-
-
[ForeignKey("Parent_Id")]
-
public int? Parent_Id { get; set; }
-
public FixedDepositDetails FixedDepositDetail { get; set; }
-
}
-
public class BaseDBContext : DbContext
-
{
-
public BaseDBContext(string ConnectionString)
-
: base(ConnectionString)
-
{
-
-
}
-
-
public DbSet<PersonDetails> PersonDetails { get; set; }
-
public DbSet<BankDetails> BankDetails { get; set; }
-
public DbSet<FixedDepositDetails> FixedDepositDetails { get; set; }
-
-
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
-
{
-
modelBuilder.Entity<FixedDepositDetails>()
-
.HasRequired(x => x.BankDetails)
-
.WithMany()
-
.HasForeignKey(y => y.Bank_Id);
-
-
modelBuilder.Entity<FixedDepositDetails>()
-
.HasRequired(x => x.PersonDetails)
-
.WithMany()
-
.HasForeignKey(y => y.Person_Id);
-
-
}
-
}
-
-
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
1 8372
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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...
|
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
|
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...
| |
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
|
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...
|
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?
|
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>
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |