I have a multiple foreign keys in my entity which points to a single parent entity. My question is, how do i get the Status Name and Country Name if they're point to the same entity?
public class LookupItem {
public int LookupItemID { get; set; }
public int ItemName { get; set; }
public List<Shippment> Shippment {get;set;}
}
public class Shippment {
public int ShippmentID { get; set; }
public int CountryID { get; set; } //points to LookupItem
public String CountryName { get; set; }
public int StatusID { get; set; } //points to LookupItem
public String StatusName { get; set; }
public LookupItem LookupItem { get; set; }
}
public class SampleController{
public void GetShippmentDetails() {
car shippment = new Shippment();
var record = (from c in db.Shippments
select c).First()
shippment.StatusName = record.LookupItem.ItemName //point to the same object??
shippment.CountryName = record.LookupItem.ItemName //point to the same object??
}
i just need to create 2 lookupitems in my shippments?
public class Shippment {
public int ShippmentID { get; set; }
public int CountryID { get; set; } //points to LookupItem
public String CountryName { get; set; }
public int StatusID { get; set; } //points to LookupItem
public String StatusName { get; set; }
public LookupItem LookupItemCountry { get; set; }
public LookupItem LookupItemStatus { get; set; }
}
public class Shippment {
[Key]
public int ShippmentID { get; set; }
[ForeinKey("LookupItemCountry")]
public int CountryID { get; set; } //points to LookupItem
[ForeinKey("LookupItemStatus ")]
public int StatusID { get; set; } //points to LookupItem
public LookupItem LookupItemCountry { get; set; }
public LookupItem LookupItemStatus { get; set; }
}
you don't need use 2 properties CountryName and StatusName.You can access name of them through LookupItemCountry and LookupItemStatus
Hope it helps for you.
Regards,
Quan Truong
Please, click 'Mark as answer' if you think my answer really help you. Thanks
vinx1127
Member
28 Points
69 Posts
Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from the...
Dec 19, 2012 11:12 PM|LINK
I have a multiple foreign keys in my entity which points to a single parent entity. My question is, how do i get the Status Name and Country Name if they're point to the same entity?
public class LookupItem { public int LookupItemID { get; set; } public int ItemName { get; set; } public List<Shippment> Shippment {get;set;} } public class Shippment { public int ShippmentID { get; set; } public int CountryID { get; set; } //points to LookupItem public String CountryName { get; set; } public int StatusID { get; set; } //points to LookupItem public String StatusName { get; set; } public LookupItem LookupItem { get; set; } } public class SampleController{ public void GetShippmentDetails() { car shippment = new Shippment(); var record = (from c in db.Shippments select c).First() shippment.StatusName = record.LookupItem.ItemName //point to the same object?? shippment.CountryName = record.LookupItem.ItemName //point to the same object?? }Can you provide some pointers and examples
kevinstone
Member
236 Points
48 Posts
Re: Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from...
Dec 19, 2012 11:28 PM|LINK
what you mean?
vinx1127
Member
28 Points
69 Posts
Re: Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from...
Dec 19, 2012 11:36 PM|LINK
How do we get the Status and Country if they're point to the same parent entity?
kevinstone
Member
236 Points
48 Posts
Re: Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from...
Dec 20, 2012 12:00 AM|LINK
First, im still confused about your data structure.
Your shipment class contains only one lookupitem, thus this lookupitem only ref to either status or country.
vinx1127
Member
28 Points
69 Posts
Re: Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from...
Dec 20, 2012 12:24 AM|LINK
i just need to create 2 lookupitems in my shippments?
public class Shippment { public int ShippmentID { get; set; } public int CountryID { get; set; } //points to LookupItem public String CountryName { get; set; } public int StatusID { get; set; } //points to LookupItem public String StatusName { get; set; } public LookupItem LookupItemCountry { get; set; } public LookupItem LookupItemStatus { get; set; } }kevinstone
Member
236 Points
48 Posts
Re: Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from...
Dec 20, 2012 01:59 AM|LINK
how about
public class Shippment {
public int ShippmentID { get; set; }
public LookupItem? Status{ get; set; }
public LookupItem? Country{ get; set; }
}
And you don't need countryID, countryName, StatusId and StatusName, coz all these data can be read from Status and Country. Dupe and waste!
quantt
Member
215 Points
38 Posts
Re: Multiple Foreign Keys Referencing to a Single Entity: How do we get the appropriate data from...
Dec 20, 2012 03:20 AM|LINK
you can set foreinkey for countryId and StatusId.
public class Shippment { [Key] public int ShippmentID { get; set; } [ForeinKey("LookupItemCountry")] public int CountryID { get; set; } //points to LookupItem [ForeinKey("LookupItemStatus ")] public int StatusID { get; set; } //points to LookupItem public LookupItem LookupItemCountry { get; set; } public LookupItem LookupItemStatus { get; set; } }you don't need use 2 properties CountryName and StatusName.You can access name of them through LookupItemCountry and LookupItemStatus
Hope it helps for you.
Quan Truong
Please, click 'Mark as answer' if you think my answer really help you. Thanks