Address (Have StateID, CountryID ... Also have StateDetail (of type State) and CountryDetail (of type Country)
But relationship is not working: Please advice
public class State
{
[Key, Column(Order = 0)]
public string StateCode { get; set; }
[Key, Column(Order=1)]
public string CountryCode { get; set; }
public string StateName { get; set; }
public Country CountryDetails { get; set; }
public virtual ICollection<Country> Countries { get; set; }
}
public class Country
{
[Key]
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)]
[Display(Name="Country Code")]
public string CountryCode { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)]
[Display(Name = "Country Name")]
public string CountryName { get; set; }
public virtual ICollection<State> States { get; set; }
}
public class Address
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public long AddressID { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string Street3 { get; set; }
//[ForeignKey("StateDetail")]
//ForeignKey("CountryDetail")]
public string StateCode { get; set; }
public string ZipCodeA { get; set; }
public string ZipCodeB { get; set; }
[ForeignKey("CountryDetail")]
public string CountryCode { get; set; }
public State StateDetail { get; set; }
public Country CountryDetail { get; set; }
}
1)Please first you should add virtual for default foreign keys:
public class State
{
[Key, Column(Order = 0)]
public string StateCode { get; set; }
[Key, Column(Order=1)]
public string CountryCode { get; set; }
public string StateName { get; set; }
public virutal Country CountryDetails { get; set; }
public virtual ICollection<Country> Countries { get; set; }
}
2)ForeignKey should be applied on a ForeignKey properties:
public class Address
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public long AddressID { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string Street3 { get; set; }
public string StateCode { get; set; }
public string ZipCodeA { get; set; }
public string ZipCodeB { get; set; }
public string CountryCode { get; set; }
[ForeignKey("StateCode")]
public State StateDetail { get; set; }
[ForeignKey("CountryCode")]
public Country CountryDetail { get; set; }
}
ProgrammingF...
Member
36 Points
41 Posts
Entity Framework : Class foreign key issue
Dec 31, 2012 03:47 AM|LINK
I have following classes:
State (Defined by StateID & CountryID)
Country (Defined by CountryID)
Address (Have StateID, CountryID ... Also have StateDetail (of type State) and CountryDetail (of type Country)
But relationship is not working: Please advice
public class State { [Key, Column(Order = 0)] public string StateCode { get; set; } [Key, Column(Order=1)] public string CountryCode { get; set; } public string StateName { get; set; } public Country CountryDetails { get; set; } public virtual ICollection<Country> Countries { get; set; } }public class Country { [Key] [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)] [Display(Name="Country Code")] public string CountryCode { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)] [Display(Name = "Country Name")] public string CountryName { get; set; } public virtual ICollection<State> States { get; set; } }public class Address { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public long AddressID { get; set; } public string Street1 { get; set; } public string Street2 { get; set; } public string Street3 { get; set; } //[ForeignKey("StateDetail")] //ForeignKey("CountryDetail")] public string StateCode { get; set; } public string ZipCodeA { get; set; } public string ZipCodeB { get; set; } [ForeignKey("CountryDetail")] public string CountryCode { get; set; } public State StateDetail { get; set; } public Country CountryDetail { get; set; } }PS : I am new to Entity framework.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Entity Framework : Class foreign key issue
Jan 01, 2013 12:53 AM|LINK
Hi,
1)Please first you should add virtual for default foreign keys:
public class State { [Key, Column(Order = 0)] public string StateCode { get; set; } [Key, Column(Order=1)] public string CountryCode { get; set; } public string StateName { get; set; } public virutal Country CountryDetails { get; set; } public virtual ICollection<Country> Countries { get; set; } }2)ForeignKey should be applied on a ForeignKey properties:
public class Address { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public long AddressID { get; set; } public string Street1 { get; set; } public string Street2 { get; set; } public string Street3 { get; set; } public string StateCode { get; set; } public string ZipCodeA { get; set; } public string ZipCodeB { get; set; } public string CountryCode { get; set; } [ForeignKey("StateCode")] public State StateDetail { get; set; } [ForeignKey("CountryCode")] public Country CountryDetail { get; set; } }