Reputation: 711
I try to scaffold a controller for my ClientModel and it gives me an error called:
There was an error running the selected code generator: 'Unable to retrieve metadata for '/////.ClientModel'. Unable to determine composite primary key for ordering for type '/////.ClientModel'. Use the ColumnAttribute (link) or the HasKey method (link) to specify an order for composite primary keys.
I have three classes with all of them an One-To-One relationship with my Client class.
This is my ClientModel:
[Table("Client")]
public class ClientModel : PersonModel
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ClientId { get; set; }
[Required]
public string FirstName { get; set; }
public string Initials { get; set; }
[Required]
public string LastName { get; set; }
//... snip ...
[Required]
public long ClientIdentificationNumber { get; set; }
public virtual PassportDetailsModel Passport { get; set; }
public virtual MembershipClientValidationModel Membership { get; set; }
public virtual AccountClientModel Account { get; set; }
}
Passport class:
[Table("Passport")]
public class PassportDetailsModel
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid PassportDetailsId { get; set; }
[Required]
public string IssuingCountry { get; set; }
[Required]
public DateTime Issued { get; set; }
[Required]
public DateTime ExpirationDate { get; set; }
public virtual ClientModel Client { get; set; }
}
Account class:
[Table("AccountClient")]
public class AccountClientModel
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid AccountId { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public virtual ClientModel Client { get; set; }
}
Membership class:
[Table("MembershipClientValidation")]
public class MembershipClientValidationModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid MembershipClientValidationId { get; set; }
public DateTime MembershipStartDate { get; set; }
public DateTime MembershipExpiryDate { get; set; }
public bool IsMembershipValid { get; set; }
[Required]
public virtual ClientModel Client { get; set; }
}
I don't understand how to fix the 'unable to retrieve metadata for <class>
error. How do I link classes with composite keys?
21-09-2015 After reading some comments I deleted the
DatabaseGenerated.DatabaseOption.Identity
In my primary key's in the other classes (Passport, Membership and Account). So I added some properties to my ClientModel:
[ForeignKey("Passport")]
public Guid PassportDetailsId { get; set; }
public virtual PassportDetailsModel Passport { get; set; }
[ForeignKey("Membership")]
public Guid MembershipClientValidationId { get; set; }
public virtual MembershipClientValidationModel Membership { get; set; }
[ForeignKey("Account")]
public Guid AccountClientId { get; set; }
public virtual AccountClientModel Account { get; set; }
It still gives me the same error as it gave me above.
Upvotes: 1
Views: 8294
Reputation: 711
I fixed this problem by doing some simple changes.
I removed my three [ForeignKey("//")]
in my ClientModel. I added the Foreign keys to my Primary keys in my other 3 models with reference to my ClientModel.
What I did next was the major fix. I added [Required]
above the public ClientModel Client { get; set; }
in my other three models.
Now it works and scaffolds my ClientController without error(s). So to show an example of one of the other three models:
public class MembershipClientValidationModel
{
[Key]
[ForeignKey("Client")]
public Guid ClientId { get; set; }
public DateTime MembershipStartDate { get; set; }
public DateTime MembershipExpiryDate { get; set; }
public bool IsMembershipValid { get; set; }
[Required]
public virtual ClientModel Client { get; set; }
}
It just works.
Upvotes: 2