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
- 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)
- )
Expand|Select|Wrap|Line Numbers
- [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);
- }
- }
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