Entity Framework 6 Foreign Key using strings and not to source model ID

asp.net-mvc c# entity-framework entity-framework-6

Question

I have the following models, listed in short form:

public class Item
{
public int id { get; set; }
public string itemId { get; set; }
... more properties ...
public virtual List<Detail> Details { get; set;}
}

public class Detail
{
[Key]
public int id { get; set; }

public string itemId { get; set; }
... more properties ...

// Navigation property
[ForeignKey("itemId")]
public virtual Item Item { get; set; }
}

I'll useitemId It will generate the FK as an int, but it will link.Item.ID to Detail.itemId I want it to link.Item.itemId to Detail.itemId

Although I'm sure I'm overlooking something in the decorations, it appears that EF prefers to always use the default ID.

I want to do it this way since the source data is connected by a string ID, which I can translate to a variable.int however the restriction that I'd prefer each table to have its own continuesPK before I can be certain that the underlying data is reliable enough.

1
1
2/1/2016 3:26:42 PM

Popular Answer

ZZZ_tmp
2
2/8/2017 12:28:13 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow