The field must be a Date - asp.net-mvc-4

I have a situation with data field validation in MVC 4
this is my Model
public class Purchase
{
public Purchase()
{
PurchaseDetails = new List<PurchaseDetails>();
}
public int PurchaseID { get; set; }
[DisplayName("Compra N°")]
[Required]
public int PurchaseNumber { get; set; }
[DisplayName("Anulada")]
[Required]
public bool isAnulada { get; set; }
public int ProviderID { get; set; }
public virtual Provider Provider { get; set; }
[DisplayName("Nombres/Notas")]
[Required]
public string Notes { get; set; }
[DisplayName("Detalles de Compra")]
public string ProposalDetails { get; set; }
[DataType(DataType.Date), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[DisplayName("Creado")]
public DateTime TimeStamp { get; set; }
[DataType(DataType.Date), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[DisplayName("Pagado en Fecha")]
public DateTime PaidDate { get; set; }
[DisplayName("Impuesto a la Compra")]
[Range(0.00, 100.0, ErrorMessage = "Valor debe estar expresado en % Entre 0 y 100")]
public decimal AdvancePaymentTax { get; set; }
[DisplayName("Pagada")]
[Required]
public bool Paid { get; set; }
public virtual ICollection<PurchaseDetails> PurchaseDetails { get; set; }
}
When try Post or Create my purchase raise the error "The Field must be a Date" i don't know how, this malfunction appear in IExplorer an Chrome browser
Any Idea? Thanks for the answer....

Related

Calculate sum of decimal values from child class and display in parent class in one to many relationships in asp.net core web api

I am still new to asp.net core web api one to many relationships, I have two tables: Loan and loanHistories
Loan Model
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Loan Balance")]
private decimal _LoanBalance;
public decimal LoanBalance
{
get
{
return LoanAmount *interestRate;
}
set { _LoanBalance = value; }
}
[Display(Name = "Interest Rate")]
public decimal interestRate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Application Date")]
public DateTime ApplicationDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Disbursement Date")]
public DateTime DisbursmentDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Due Date")]
public DateTime DueDate { get; set; }
[Display(Name = "Defaulted")]
public bool Defaulted { get; set; }
[Display(Name = "Approved")]
public bool Approved { get; set; }
//Navigation property
public int CustomerID { get; set; }
public Customer customer { get; set; }
//public ICollection<LoanComments> loancomments { get; set; }
public ICollection<LoansHistories> loansHistories { get; set; }
}
LoanHistories model:
public class LoansHistories
{
[Key]
public int HistID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Repaid Amount")]
public decimal RePaidIn { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Repayement Date")]
public DateTime RepayementDateDate { get; set; }
[Display(Name = "No of paying interest only")]
public int NoOfPayinyingIntrestOnly { get; set; }
//Navigation properties
public int LoanID { get; set; }
public Loan loan { get; set; }
}
It generates the following Json data
[{"loanID":1,"loanAmount":1000.0000,"loanBalance":15000.000000,"interestRate":15.00,"applicationDate":"2022-03-28T00:00:00","disbursmentDate":"2022-03-28T00:00:00","dueDate":"2022-04-28T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[{"histID":1,"rePaidIn":500.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":1},{"histID":4,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-21T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":1},{"histID":5,"rePaidIn":50000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":1},{"histID":6,"rePaidIn":50000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":5,"loanID":1},{"histID":7,"rePaidIn":50000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":1},{"histID":8,"rePaidIn":50000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":1},{"histID":10,"rePaidIn":200.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":8,"loanID":1}]},{"loanID":2,"loanAmount":3000.0000,"loanBalance":45000.000000,"interestRate":15.00,"applicationDate":"2022-03-28T00:00:00","disbursmentDate":"2022-03-28T00:00:00","dueDate":"2022-03-28T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[{"histID":2,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-08T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":2},{"histID":9,"rePaidIn":50000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":2,"loanID":2}]},{"loanID":3,"loanAmount":1000.0000,"loanBalance":150.000000,"interestRate":0.15,"applicationDate":"2022-03-28T00:00:00","disbursmentDate":"2022-03-28T00:00:00","dueDate":"2022-03-28T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[]},{"loanID":4,"loanAmount":2000.0000,"loanBalance":2300.000000,"interestRate":1.15,"applicationDate":"2022-03-28T00:00:00","disbursmentDate":"2022-03-28T00:00:00","dueDate":"2022-03-28T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[{"histID":3,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":4},{"histID":11,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":3,"loanID":4},{"histID":12,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":6,"loanID":4},{"histID":13,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":6,"loanID":4},{"histID":14,"rePaidIn":2000.0000,"repayementDateDate":"2022-02-27T00:00:00","noOfPayinyingIntrestOnly":7,"loanID":4},{"histID":15,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":4},{"histID":16,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-14T00:00:00","noOfPayinyingIntrestOnly":6,"loanID":4},{"histID":17,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":3,"loanID":4},{"histID":18,"rePaidIn":2000.0000,"repayementDateDate":"2022-03-22T00:00:00","noOfPayinyingIntrestOnly":8,"loanID":4}]},{"loanID":5,"loanAmount":1000.0000,"loanBalance":1150.000000,"interestRate":1.15,"applicationDate":"2022-03-28T00:00:00","disbursmentDate":"2022-03-29T00:00:00","dueDate":"2021-04-29T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[]},{"loanID":8,"loanAmount":3000.0000,"loanBalance":3450.000000,"interestRate":1.15,"applicationDate":"2022-04-25T00:00:00","disbursmentDate":"2022-03-28T00:00:00","dueDate":"2022-04-26T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[]},{"loanID":9,"loanAmount":1000.0000,"loanBalance":1150.000000,"interestRate":1.15,"applicationDate":"2022-04-13T00:00:00","disbursmentDate":"2022-04-04T00:00:00","dueDate":"2022-04-25T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[]},{"loanID":10,"loanAmount":3000.0000,"loanBalance":3450.000000,"interestRate":1.15,"applicationDate":"2022-04-19T00:00:00","disbursmentDate":"2022-04-26T00:00:00","dueDate":"2022-04-25T00:00:00","defaulted":false,"approved":true,"customerID":40140676,"customer":null,"loansHistories":[{"histID":1012,"rePaidIn":300.0000,"repayementDateDate":"2022-04-05T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":10},{"histID":1013,"rePaidIn":150.0000,"repayementDateDate":"2022-04-19T00:00:00","noOfPayinyingIntrestOnly":2,"loanID":10},{"histID":1014,"rePaidIn":300.0000,"repayementDateDate":"2022-04-11T00:00:00","noOfPayinyingIntrestOnly":3,"loanID":10},{"histID":1015,"rePaidIn":150.0000,"repayementDateDate":"2022-04-11T00:00:00","noOfPayinyingIntrestOnly":4,"loanID":10},{"histID":1016,"rePaidIn":300.0000,"repayementDateDate":"2022-04-06T00:00:00","noOfPayinyingIntrestOnly":5,"loanID":10},{"histID":1017,"rePaidIn":150.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":6,"loanID":10},{"histID":1018,"rePaidIn":300.0000,"repayementDateDate":"2022-04-05T00:00:00","noOfPayinyingIntrestOnly":7,"loanID":10},{"histID":1019,"rePaidIn":150.0000,"repayementDateDate":"2022-04-19T00:00:00","noOfPayinyingIntrestOnly":8,"loanID":10},{"histID":1020,"rePaidIn":150.0000,"repayementDateDate":"2022-04-02T00:00:00","noOfPayinyingIntrestOnly":9,"loanID":10},{"histID":1021,"rePaidIn":150.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":10,"loanID":10},{"histID":1022,"rePaidIn":100.0000,"repayementDateDate":"2022-04-06T00:00:00","noOfPayinyingIntrestOnly":12,"loanID":10},{"histID":1023,"rePaidIn":350.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":13,"loanID":10},{"histID":1024,"rePaidIn":100.0000,"repayementDateDate":"2022-04-12T00:00:00","noOfPayinyingIntrestOnly":14,"loanID":10},{"histID":1025,"rePaidIn":350.0000,"repayementDateDate":"2022-04-11T00:00:00","noOfPayinyingIntrestOnly":6,"loanID":10},{"histID":1026,"rePaidIn":100.0000,"repayementDateDate":"2022-04-20T00:00:00","noOfPayinyingIntrestOnly":14,"loanID":10},{"histID":1027,"rePaidIn":1000.0000,"repayementDateDate":"2022-04-12T00:00:00","noOfPayinyingIntrestOnly":16,"loanID":10},{"histID":1028,"rePaidIn":1000.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":17,"loanID":10},{"histID":1029,"rePaidIn":1000.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":18,"loanID":10}]},{"loanID":11,"loanAmount":1000.0000,"loanBalance":1150.000000,"interestRate":1.15,"applicationDate":"2022-04-26T00:00:00","disbursmentDate":"2022-04-25T00:00:00","dueDate":"2022-04-05T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[{"histID":1030,"rePaidIn":150.0000,"repayementDateDate":"2022-04-05T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":11},{"histID":1031,"rePaidIn":150.0000,"repayementDateDate":"2022-04-12T00:00:00","noOfPayinyingIntrestOnly":5,"loanID":11}]},{"loanID":12,"loanAmount":1000.0000,"loanBalance":1150.000000,"interestRate":1.15,"applicationDate":"2022-04-24T00:00:00","disbursmentDate":"2022-04-24T00:00:00","dueDate":"2022-04-25T00:00:00","defaulted":false,"approved":true,"customerID":40140676,"customer":null,"loansHistories":[{"histID":1032,"rePaidIn":150.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":12},{"histID":1033,"rePaidIn":150.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":3,"loanID":12}]},{"loanID":13,"loanAmount":2000.0000,"loanBalance":2300.000000,"interestRate":1.15,"applicationDate":"2022-04-24T00:00:00","disbursmentDate":"2022-04-25T00:00:00","dueDate":"2022-05-03T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[{"histID":1034,"rePaidIn":300.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":13},{"histID":1035,"rePaidIn":300.0000,"repayementDateDate":"2022-04-12T00:00:00","noOfPayinyingIntrestOnly":9,"loanID":13}]},{"loanID":14,"loanAmount":1000.0000,"loanBalance":1150.000000,"interestRate":1.15,"applicationDate":"2022-04-25T00:00:00","disbursmentDate":"2022-04-06T00:00:00","dueDate":"2022-04-26T00:00:00","defaulted":false,"approved":true,"customerID":30290122,"customer":null,"loansHistories":[{"histID":1036,"rePaidIn":150.0000,"repayementDateDate":"2022-04-05T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":14},{"histID":1037,"rePaidIn":1000.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":4,"loanID":14},{"histID":1038,"rePaidIn":150.0000,"repayementDateDate":"2022-04-18T00:00:00","noOfPayinyingIntrestOnly":4,"loanID":14},{"histID":1039,"rePaidIn":1000.0000,"repayementDateDate":"2022-04-11T00:00:00","noOfPayinyingIntrestOnly":5,"loanID":14},{"histID":1040,"rePaidIn":100.0000,"repayementDateDate":"2022-04-19T00:00:00","noOfPayinyingIntrestOnly":6,"loanID":14},{"histID":1041,"rePaidIn":50.0000,"repayementDateDate":"2022-04-04T00:00:00","noOfPayinyingIntrestOnly":7,"loanID":14},{"histID":1042,"rePaidIn":50.0000,"repayementDateDate":"2022-04-05T00:00:00","noOfPayinyingIntrestOnly":8,"loanID":14},{"histID":1043,"rePaidIn":50.0000,"repayementDateDate":"2022-04-06T00:00:00","noOfPayinyingIntrestOnly":8,"loanID":14},{"histID":1044,"rePaidIn":1000.0000,"repayementDateDate":"2022-03-28T00:00:00","noOfPayinyingIntrestOnly":89,"loanID":14}]},{"loanID":1014,"loanAmount":5000.0000,"loanBalance":5750.000000,"interestRate":1.15,"applicationDate":"2022-03-28T00:00:00","disbursmentDate":"2022-04-11T00:00:00","dueDate":"2022-04-12T00:00:00","defaulted":false,"approved":true,"customerID":40140676,"customer":null,"loansHistories":[{"histID":2035,"rePaidIn":750.0000,"repayementDateDate":"2022-04-11T00:00:00","noOfPayinyingIntrestOnly":1,"loanID":1014},{"histID":2036,"rePaidIn":1000.0000,"repayementDateDate":"2022-04-12T00:00:00","noOfPayinyingIntrestOnly":2,"loanID":1014},{"histID":2037,"rePaidIn":750.0000,"repayementDateDate":"2022-03-30T00:00:00","noOfPayinyingIntrestOnly":3,"loanID":1014}]}]
Now I want to calculate the total sum of rePaidIn within loanHistories table and display in loan table Has TotalRepaidIn with heading [NotMapped]: I have tried from the following link .it seems close to answer my question but it does not
Here is my Controller
[HttpGet]
public async Task<ActionResult<IEnumerable<Loan>>> Getloans()
{
return await _context.loans.Include(lh => lh.loansHistories).ToListAsync();
}
Any solution will be highly appreciated. Thanks in advance
Here is a whole working demo:
Model:
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Loan Balance")]
private decimal _LoanBalance;
[Column(TypeName = "decimal(18,2)")]
public decimal LoanBalance
{
get
{
return LoanAmount * interestRate;
}
set { _LoanBalance = value; }
}
[Column(TypeName = "decimal(18,2)")]
[Display(Name = "Interest Rate")]
public decimal interestRate { get; set; }
//....
[Display(Name = "Approved")]
public bool Approved { get; set; }
//public ICollection<LoanComments> loancomments { get; set; }
public ICollection<LoansHistories> loansHistories { get; set; }
[NotMapped]
public decimal TotalRepaidIn { get; set; } //add this...
}
public class LoansHistories
{
[Key]
public int HistID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Display(Name = "Repaid Amount")]
public decimal RePaidIn { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Repayement Date")]
public DateTime RepayementDateDate { get; set; }
[Display(Name = "No of paying interest only")]
public int NoOfPayinyingIntrestOnly { get; set; }
public int LoanID { get; set; }
public Loan loan { get; set; }
}
Controller:
public async Task<ActionResult<IEnumerable<Loan>>> Getloans()
{
var data = await _context.loans.Include(lh => lh.loansHistories).Select(l=>new Loan()
{
TotalRepaidIn = l.loansHistories.Select(lh=>lh.RePaidIn).Sum(),
loansHistories = l.loansHistories,
ApplicationDate = l.ApplicationDate,
Defaulted=l.Defaulted,
DisbursmentDate= l.DisbursmentDate,
DueDate= l.DueDate,
LoanID=l.LoanID,
Approved=l.Approved,
interestRate=l.interestRate,
LoanAmount=l.LoanAmount
}).ToListAsync();
return data;
}
Update:
A better way is to change your model like below:
using System.Linq; //import this namespace...
public class Loan
{
[Key]
public int LoanID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal LoanAmount { get; set; }
private decimal _TotalRepaidIn;
[NotMapped]
public decimal TotalRepaidIn
{
get { return loansHistories.Sum(sum => sum.RePaidIn); }
set { _TotalRepaidIn = value; }
}
//other properties
public ICollection<LoansHistories> loansHistories { get; set; } = new List<LoansHistories>();
}
Controller:
public async Task<ActionResult<IEnumerable<Loan>>> Getloans()
{
return await _context.loans.Include(lh => lh.loansHistories).ToListAsync();
}

Value not inserting in DateTime column using Entity Framework

I am trying to insert value in DateTime column with Entity Framework but it throwing error on db.SaveChanges() function. There are 2 DateTime columns but exception throwing for only one (entry.OT_Date) with no maximum detail.
var entry = new OT_Request_N();
entry.Emp_ID = StaticFunctions.WindowUserId;
entry.slno = db.OT_Request_N.Max(x=>x.slno)+1;
entry.OT_Date= otD;
entry.OT_Time_Req = hourRequest;
entry.Remarks_Req = sError.ReqRemarks;
entry.Req_Sent_On = DateTime.Now;
entry.Supervisor_ID = sError.ManagerID;
entry.Approve_Status = 0;
db.OT_Request_N.Add(entry);
db.SaveChanges();
Here is SQL table Design
Here is class generated in edmx file
public partial class OT_Request_N
{
public int slno { get; set; }
public string Emp_ID { get; set; }
public Nullable<System.DateTime> Req_Sent_On { get; set; }
public Nullable<int> OT_Time_Req { get; set; }
public string Supervisor_ID { get; set; }
public string Remarks_Req { get; set; }
public string Remarks_App { get; set; }
public Nullable<int> OT_Time_App { get; set; }
public string Approved_By { get; set; }
public Nullable<int> Approve_Status { get; set; }
public Nullable<System.DateTime> Approved_On { get; set; }
public Nullable<int> Operations { get; set; }
public string AltReq_ID { get; set; }
public Nullable<bool> Is_alert_sent { get; set; }
public Nullable<System.DateTime> OT_Date { get; set; }
}

Unable to determine the principal end of an association , One to One relation of SQL

This is my error :
Unable to determine the principal end of an association between the types 'SANTEK.Web.Models.SantekOrder' and 'SANTEK.Web.Models.OrderMoneySum'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
What is my mistake logically ?? I mean to build one-to-one relation.
My model classes are here :
[Table("Orders")]
public class SantekOrder
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SantekOrderId { get; set; }
[Required, StringLength(25)]
[DisplayName("Çatdırılacaq şəxs")]
public string DelivaryManName { get; set; }
[DisplayName("Çatdırılacaq şəxs istifadəçidir?")]
public bool DeliveryManIsUser { get; set; }
[Required, StringLength(25)]
[DisplayName("Çatdırılacaq şəxs ad və soyadı")]
public string DelivaryManSurname { get; set; }
[Required, StringLength(10)]
[DisplayName("Çatdırılacaq şəxs tefon nömrəsi")]
public string DelivaryManTel { get; set; }
[DisplayName("Çatdırılacaq ünvan istifadəçinin ünvandır?")]
public bool IsUserAddress { get; set; }
[Required, StringLength(150)]
[DisplayName("Çatdırılacaq şəxsin ünvanı?")]
public string DelivaryManAddress { get; set; }
public int SanTekUserId { get; set; }
public int OrderMoneySumId { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy-hh-mm-ss}",
ApplyFormatInEditMode = true)]
public DateTime Time { get; set; }
[ForeignKey("SanTekUserId")]
public virtual SanTekUser SanUser { get; set; }
[ForeignKey("SanTekUserId")]
public virtual OrderMoneySum OrderMoneySum { get; set; }
}
This is my OrdersDetail Table to hold all product and their quantity
[Table("OrderedProducts")]
public class OrderMoneySum
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int OrderMoneySumId { get; set; }
[Required]
public int SantekOrderId { get; set; }
[Required]
public int AllProductId { get; set; }
[DisplayName("Miqdarı")]
public int Quantity { get; set; }
[ForeignKey("SantekOrderId")]
public virtual SantekOrder SantekOrder { get; set; }
[ForeignKey("SantekOrderId")]
public virtual AllProduct AllProduct { get; set; }
}

how I define my model using lazyloading = true

How I define my model using lazyloading = true in MVC 4? I am trying to load my foreign keys. Now my query is coming null. In my web api query comes right.
I did not use the "virtual".
My Model:
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int intClienteFornecedorId { get; set; }
[MaxLength(50)]
[Display(Name = "Nome:")]
[UniqueClienteFornecedorNome(ErrorMessage = "Já existe um Cliente ou Fornecedor cadastrado com esse Nome.")]
[Required(ErrorMessage = "É obrigatório informar o Nome.")]
public string strClienteFornecedorNome { get; set; }
[MaxLength(2)]
[Display(Name = "Pessoa:")]
public string strClienteFornecedorPessoa{ get; set; }
[ForeignKey("Estado")]
[Display(Name = "Estado:")]
public string strUFId { get; set; }
public virtual Estado Estado { get; set; }
[MaxLength(9)]
[Display(Name = "CEP:")]
public string strClienteFornecedorCEP { get; set; }
[MaxLength(70)]
[Display(Name = "Site:")]
public string strClienteFornecedorSite { get; set; }
[MaxLength(250)]
[Display(Name = "Observação:")]
public string strClienteFornecedorObservacao { get; set; }
[MaxLength(1)]
[Display(Name = "Situação:")]
public string strClienteFornecedorSituacao { get; set; }
[ForeignKey("Usuario")]
public int intUsuarioId { get; set; }
public virtual Usuario Usuario { get; set; }
public DateTime? dtClienteFornecedorDataAlteracao { get; set; }
public DateTime? dtClienteFornecedorDataCriacao { get; set; }
[NotMapped]
public bool ckbClienteFornecedor { get; set; }
[NotMapped]
public bool? hdMaisInformacoes { get; set; }
[NotMapped]
public bool? hdObservacao { get; set; }
public virtual ICollection<Contato> Contatos { get; set; }
public virtual ICollection<ClienteFornecedor_Contato> ClienteFornecedoresContatos { get; set; }
WEB API:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
public List<ClienteFornecedor> GetClientesFornecedoresByintUsuarioId(int intUsuarioId)
{
//Validate Input
if (intUsuarioId.IsEmpty())
return GetAllClienteFornecedor();
_DatabaseContext.ContextOptions.LazyLoadingEnabled = false;
return (from clienteFornecedor in _DatabaseContext.ClienteFornecedor
where clienteFornecedor.intUsuarioId == intUsuarioId
select clienteFornecedor).Include("Usuario").Include("UF").OrderBy(c => c.strClienteFornecedorNome).ToList();
}
In the web api returns two records, but in my web application the second record returns null why?
Could anyone help me please?
The second record is null, anyone know why?
JSON:
[{"$id":"1","intClienteFornecedorId":8,"strClienteFornecedorNome":"CPFL","strClienteFornecedorPessoa":"PJ","strClienteFornecedorCPF":null,"strClienteFornecedorCNPJ":"11.111.111/1111-11","strClienteFornecedorTelefone":null,"strClienteFornecedorTelefone2":null,"strClienteFornecedorEmail":null,"strClienteFornecedorEndereco":null,"strClienteFornecedorComplemento":null,"strClienteFornecedorBairro":null,"strClienteFornecedorCidade":"SP","strUFId":"SP","strClienteFornecedorCEP":null,"strClienteFornecedorSite":null,"strClienteFornecedorObservacao":null,"strClienteFornecedorSituacao":"A","intUsuarioId":1,"dtClienteFornecedorDataAlteracao":"2013-10-30T11:29:33.303","dtClienteFornecedorDataCriacao":"2013-10-30T11:29:33.303","Categoria":[],"UF":{"$id":"2","strUFId":"SP","strUFNome":"São Paulo","Contato":[],"ClienteFornecedor":[{"$ref":"1"}],"EntityKey":{"$id":"3","EntitySetName":"UF","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"strUFId","Type":"System.String","Value":"SP"}]}},"Usuario":{"$id":"4","intUsuarioId":1,"strUsuarioNome":"Danielle Ramos Tonhai","strUsuarioEmail":"danielle.ramos#unidax.com.br","strUsuarioSenha":"12345678","strUsuarioAdministrador":"S","strUsuarioSituacao":"A","intUsuarioPaiId":0,"dtUsuarioDataAlteracao":"2013-10-28T15:16:28.233","dtUsuarioDataCriacao":"2013-10-24T16:42:26","bitUsuarioConfirmado":true,"CartaoCredito":[],"Categoria":[],"ContaBancaria":[],"ContaContabil":[],"Contato":[],"Entidade":[],"Evento":[],"FormaPagamento":[],"ClienteFornecedor":[{"$ref":"1"},{"$id":"5","intClienteFornecedorId":9,"strClienteFornecedorNome":"Danielle","strClienteFornecedorPessoa":"PF","strClienteFornecedorCPF":"111.111.111-11","strClienteFornecedorCNPJ":null,"strClienteFornecedorTelefone":null,"strClienteFornecedorTelefone2":null,"strClienteFornecedorEmail":null,"strClienteFornecedorEndereco":null,"strClienteFornecedorComplemento":null,"strClienteFornecedorBairro":null,"strClienteFornecedorCidade":null,"strUFId":null,"strClienteFornecedorCEP":null,"strClienteFornecedorSite":null,"strClienteFornecedorObservacao":null,"strClienteFornecedorSituacao":"A","intUsuarioId":1,"dtClienteFornecedorDataAlteracao":"2013-10-30T11:29:52.203","dtClienteFornecedorDataCriacao":"2013-10-30T11:29:52.203","Categoria":[],"UF":null,"Usuario":{"$ref":"4"},"Contato":[],"EntityKey":{"$id":"6","EntitySetName":"ClienteFornecedor","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intClienteFornecedorId","Type":"System.Int32","Value":"9"}]}}],"EntityKey":{"$id":"7","EntitySetName":"Usuario","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},"Contato":[],"EntityKey":{"$id":"8","EntitySetName":"ClienteFornecedor","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intClienteFornecedorId","Type":"System.Int32","Value":"8"}]}},{"$ref":"5"}]

code first with different entities

I have three model classes and three views. Models are given below
public class BASLPApplicationFormModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int BASLPApplicationID { get; set; }
public string BASLPApplicationNo { get; set; }
public string ApplicantName { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date of Birth", Prompt = "DD/MM/YYYY")]
public DateTime DateOfBirth { get; set; }
[Required]
[Display(Name = "Email-ID")]
public string EmailID { get; set; }
}
and second
public class DPPHIApplicationFormModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int DPPHIApplicationID { get; set; }
public string DPPHIapplicationNo { get; set; }
[Required]
[Display(Name = "Name of Applicant")]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required]
[Display(Name = "Expansion of Initial")]
[DataType(DataType.Text)]
public string InitialExpansion { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date of Birth", Prompt = "MM/DD/YYYY")]
public DateTime DateOfBirth { get; set; }
[Required]
[Display(Name = "Email-ID")]
[DataType(DataType.EmailAddress)]
public string EmailID { get; set; }
}
and third
public class DTYDHHApplicationFormModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int DTYDHHApplicationID { get; set; }
public string DTYDHHAapplicationNo { get; set; }
[Required]
[Display(Name = "Name of Applicant")]
[DataType(DataType.Text)]
public string name { get; set; }
[RegularExpression("^([0-9]+)$", ErrorMessage = "Invalid Phone Number")]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Land Line Number")]
public string Phone { get; set; }
[Required]
[Display(Name = "Email-ID")]
[DataType(DataType.EmailAddress)]
public string EmailID { get; set; }
}
Email model ,
[Table("Emails")]
public class UserEmails
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UserEmailID { get; set; }
}
Here user interacts with any of the forms and submitting their forms accordingly.
I would like to get all Email entries to my Emails table using code first approach and please advice me how can achieve.
Take a look at this serie of three blogposts:
http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx
http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx
Hope this helps