-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot set Foreign Key column name by Fluent API #6880
Comments
Can you share a full code listing (or project) that we can run to debug the issue? There isn't enough info here for us to work out what is going on. |
Considering this architecture class LTC
{
public int TestA { get; set; }
public string TestB { get; set; }
public Guid Id { get; set; }
protected ICollection<LTCLinks> _links { get; set; }
public IReadOnlyList<LTCLinks> Links
{
get
{
return _links.ToList().AsReadOnly();
}
}
}
class LTCLinks
{
public LTC Link1 { get; set; }
public LTC Link2 { get; set; }
public string Data { get; set; }
} with this configuration var ltcBuilder = modelBuilder.Entity(typeof(LTC));
ltcBuilder.Property(typeof(int), "TestA").IsRequired();
ltcBuilder.Property(typeof(string), "TestB").IsRequired();
ltcBuilder.HasKey("Id");
ltcBuilder.Ignore("_links");
var ltcLinkBuilder = modelBuilder.Entity(typeof(LTCLinks));
ltcBuilder.Property(typeof(string), "Data").IsRequired();
ltcLinkBuilder.HasOne(typeof(LTC).FullName, "Link1").WithMany().IsRequired().OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Cascade);
ltcLinkBuilder.HasOne(typeof(LTC).FullName, "Link2").WithMany().IsRequired(false).OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);
ltcLinkBuilder.HasKey("Data", "Link1ID", "Link2ID"); The two foreign keys for LTCLinks are created as shadow properties by EF. How can I define their name in fluent API ? |
@cdie I am not sure if by "creating explicit foreign key" you are referring to defining a shadow property explicitly with a call like I am also curious about some things you are doing in your code, e.g. why not use the generic versions of the model building methods and lambda expressions to identify properties? It can lead to easier to read code. |
@divega when I said "creating explicit foreign key", I was talking about adding 2 Guids C# properties for defining foreign keys for About the using of generic versions, the code above was just a little example of how when proceed, because our architecture was previously used without EF, so we've defined our own attributes to create migrations automaticly by reflection. So I created an example and adapted it to try to retranscript at best what we're doing by reflection for our own business objects. I wanna be sure that it's (or not) possible to fix foreign key name with non generics methods, so this is why I designed such example. |
How to name the FK column
|
@divega - Should we close this issue now? |
@smitpatel thanks for poaching. |
I'm trying to give my Foreign Key column the name I want (not talking about the constraint name).
If I set my foreignKey with the builder.Property,
when I create relationship, I have an error on migration :
"The navigation property 'LettreClePrincipale' cannot be added to the entity type 'LettreCleAssociation' because a property with the same name already exists on entity type 'LettreCleAssociation'."
If I don't set my foreignKey with the builder.Property, this error is gone, the key is correctly created, but I cannot set name during the process :
I don't have ForSqlServerHasName or ForSqliteHasName extensions method here. How can I proceed to achieve this ?
The text was updated successfully, but these errors were encountered: