I have three enities
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Customerid { get; set; }
public string Name { get; set; }
public string email { get; set; }
public string Phoneno { get; set; }
}
and second Enitity
public class Merchant
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Merchantid { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string MerchantName { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string BusinessName { get; set; }
[Required]
public string OfficePhno { get; set; }
}
public class Transaction
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TransactionId { get; set; }
public int Merchantid {get; set;}
public int Customerid {get; set;}
public int Amount { get; set; }
public Customer customer {get; set;}
public Merchant merchant {get; set;}
}
in above Transaction table has two IDs Merchant ID and Customer ID i want this to foreign keys of customer and merchant table, please explain me how to add the foreign key Constraints I went through many examples but was not able to get the answer for this
and another doubt is if i add Customer type inside Transaction table will I able to fetch details of customer in transaction table in Entity Framework
I am trying to add Constraint by follwing code
[ForeignKey("Customerid")]
pubic virtual Customer customer {get; set;}
I am getting an exception as below
Additional information: The ForeignKeyAttribute on property 'Customer' on type 'Dutch.Models.Transaction' is not valid. The foreign key name 'Customerid' was not found on the dependent type 'Dutch.Models.Transaction'. The Name value should be a comma separated list of foreign key property names.