1

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.

zed
  • 2,298
  • 4
  • 27
  • 44
Pragadees
  • 71
  • 1
  • 12
  • 1
    in EF6, as far as I know, 1-on-1 relationships will use the convention PK=FK, so you have FK constraints, but these will consider the Id's. If you add a Customer type in your transaction, you will be able to get the details, as long as you lazily/explicitly load them. – DevilSuichiro Nov 18 '15 at 09:40
  • can you explain be still I cant Understand – Pragadees Nov 18 '15 at 10:02
  • This might help:http://stackoverflow.com/questions/15483019/entity-framework-code-first-how-to-annotate-a-foreign-key-for-a-default-valu – Daniel Stackenland Nov 18 '15 at 10:16
  • I have another doubt if i fetch a record of Transaction using ef will all columns of Customer will it be fetched ? – Pragadees Nov 18 '15 at 11:03

1 Answers1

0

I finally did by Foreignkey constraint My Model looks like this now

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; }
   public ICollection<Transaction> Transactions { get; set; }
}

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 ICollection<Transaction> Transactions { get; set; }
}

So Customer and Merchants will have a collection of transactions

and in transaction table

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; }
[ForeignKey("Customerid")]
public Customer customer {get; set;}
[ForeignKey("Merchantid")]
public Merchant merchant {get; set;}
}
Pragadees
  • 71
  • 1
  • 12