You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clarify that FK shadow properties are usually created by convention #1016
Yeah I know. My request still stands. There's a page about shadow properties, which says what they're most useful for, but then fails a decent example.
WTF does that line of code do? It's not obvious to me.
99% of people won't bother to provide feedback when they struggle and will turn to google to find an answer. So when someone does provide feedback it shouldn't be disregarded.
Reacted by anth-git, Egor Potekhin, Flippie Coetser, Peyman Ebrahimi, Cédric Luthi, Musa and Jesús Mendoza
@mattfrear Trying to connect the dots here, maybe the confusion is that they are commonly used for FKs, but rarely explicitly defined for FKs. Hence showing API here to define an FK in shadow state would not be terribly useful since that's not a very common thing to do. Would it be clearer to say, "They are most often used for foreign key properties, where they are added to the model by convention when no foreign key property has been found by convention or configured explicitly."
With regard to not knowing what that line of code does, can you be more specific about what is ambiguous?
Would it be clearer to say, "They are most often used for foreign key properties, where they are added to the model by convention when no foreign key property has been found by convention or configured explicitly."
Yes, I think that would be an improvement.
With regard to not knowing what that line of code does, can you be more specific about what is ambiguous?
I have re-read the page and now it makes sense, so please disregard my earlier comment.
I landed on this page trying to solve a specific and frustrating problem (one to many relationship with a nullable int foreign key which should not be nullable), I was hoping this page would shed some light on my problem but I accept that I was perhaps looking in the wrong place. Sorry for being rude.
I think that the example from https://docs.microsoft.com/en-us/ef/core/modeling/relationships#foreign-key should be used for the FluentApi paragraph.
It seems to be quite valid usecase to have FK that doesn't follow convention but we want to configure it as a shadow property. So it would be much useful example than example of just creating some arbitrary "LastUpdated" property, that doesn't have any meaning, especially that you stated by yourself that shadow properties are are most often used for foreign key.
// Add the shadow property to the model
modelBuilder.Entity<Post>()
.Property<int>("BlogForeignKey");
// Use the shadow property as a foreign key
modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.HasForeignKey("BlogForeignKey");
Reacted by Pavel Voronin, Peyman Ebrahimi, Cédric Luthi and WenJun Chi
public partial class Continent
{
public string Code { get; set; }
public string Name { get; set; }
public ICollection<Country> Countries { get; set; }
}
Country model
public partial class Country
{
public Country() => Provinces = new HashSet<Province>();
public int Code { get; set; }
public string IsoA2 { get; set; }
public string IsoA3 { get; set; }
public string Name { get; set; }
public string Capital { get; set; }
public Continent Continent { get; set; }
public ICollection<Province> Provinces { get; set; }
}
public static void Initialize(NusaportContext context)
{
context.Database.EnsureCreated();
if (!context.Continents.Any())
{
var continents = new Continent[] {
new Continent { Code = "AF", Name = "Africa" },
.....
};
foreach (Continent c in continents)
{
context.Continents.Add(c);
}
context.SaveChanges();
}
if (!context.Countries.Any())
{
var countries = new Country[] {
new Country { Code = 111, IsoA2 = "ZZ", IsoA3 = "ZZZ", Name = "Example", Capital = "Example" }
};
foreach (Country cn in countries)
{
context.Countries.Add(cn);
}
return;
}
I need to insert continent_code's value while seeding data to database,
It's extremely frustrating, because I am trying to reduce volume of code. There's no explanation of what would happen if you call the .HasForeignKey(""BlogForeignKey") method without having explicitly defined the Shadow Property beforehand. How do I know whether or not I can omit the explicit property addition?
UPDATE: After understanding the problem better, I opened a separate issue about this: #2807
Activity
ajcvickers commentedon Oct 2, 2018
The fluent API docs for relationships and FKs is here: https://docs.microsoft.com/en-us/ef/core/modeling/relationships
mattfrear commentedon Oct 2, 2018
Yeah I know. My request still stands. There's a page about shadow properties, which says what they're most useful for, but then fails a decent example.
WTF does that line of code do? It's not obvious to me.
99% of people won't bother to provide feedback when they struggle and will turn to google to find an answer. So when someone does provide feedback it shouldn't be disregarded.
ajcvickers commentedon Oct 2, 2018
@mattfrear Trying to connect the dots here, maybe the confusion is that they are commonly used for FKs, but rarely explicitly defined for FKs. Hence showing API here to define an FK in shadow state would not be terribly useful since that's not a very common thing to do. Would it be clearer to say, "They are most often used for foreign key properties, where they are added to the model by convention when no foreign key property has been found by convention or configured explicitly."
With regard to not knowing what that line of code does, can you be more specific about what is ambiguous?
mattfrear commentedon Oct 2, 2018
Yes, I think that would be an improvement.
I have re-read the page and now it makes sense, so please disregard my earlier comment.
I landed on this page trying to solve a specific and frustrating problem (one to many relationship with a nullable int foreign key which should not be nullable), I was hoping this page would shed some light on my problem but I accept that I was perhaps looking in the wrong place. Sorry for being rude.
ajcvickers commentedon Oct 2, 2018
@mattfrear Np
Re-opening because I think clarifying that FK shadow properties are usually created by convention would be useful.
[-]Please provide a more useful example[/-][+]Clarify that FK shadow properties are usually created by convention[/+]anth-git commentedon Nov 2, 2018
I think that the example from https://docs.microsoft.com/en-us/ef/core/modeling/relationships#foreign-key should be used for the FluentApi paragraph.
It seems to be quite valid usecase to have FK that doesn't follow convention but we want to configure it as a shadow property. So it would be much useful example than example of just creating some arbitrary "LastUpdated" property, that doesn't have any meaning, especially that you stated by yourself that shadow properties are are most often used for foreign key.
Yuant-tobing commentedon Mar 10, 2019
How to seed data to shadow properties ?
Continent model
Country model
App context
App data seeder
I need to insert continent_code's value while seeding data to database,
ajcvickers commentedon Mar 11, 2019
@Yuant-tobing Use an anonymous type as shown in the docs.
4 remaining items
pha3z commentedon Oct 25, 2020
Can someone please explain this line of documentation?
"We recommend explicitly adding the shadow property to the model before using it as a foreign key (as shown below)."
It's on this page: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#shadow-foreign-key
It's extremely frustrating, because I am trying to reduce volume of code. There's no explanation of what would happen if you call the .HasForeignKey(""BlogForeignKey") method without having explicitly defined the Shadow Property beforehand. How do I know whether or not I can omit the explicit property addition?
UPDATE: After understanding the problem better, I opened a separate issue about this:
#2807
Document conventions and related model building concepts
Document conventions and related model building concepts
Document conventions and related model building concepts
Document conventions and related model building concepts