question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foreign Keys not named correctly on code generation

See original GitHub issue

Similar to my last one, sort of.

Same three tables: image

in the context, it generates:

modelBuilder.Entity<EFCoreDesignTools.JoinSiteInvestigator>()
    .HasOne(x => x.Site)
    .WithOne()
    .HasForeignKey("Site_Id")
    .IsRequired().OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<EFCoreDesignTools.JoinSiteInvestigator>()
    .HasOne(x => x.Investigator)
    .WithOne()
    .HasForeignKey("Investigator_Id")
    .IsRequired().OnDelete(DeleteBehavior.Cascade);

.HasForeignKey("Site_Id") and .HasForeignKey("Investigator_Id") should be generated as “SiteId” and “InvestigatorId” respectively.

I’m not sure if this is an issue with how the relationships are set up, or an issue with the generator?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
msawczyncommented, Aug 20, 2018

Yes, EF6 will create the needed database structures without messing up your object model. EFCore will eventually get there, but it’s slow going.

The ideal case is that you model your classes like you would model any classes. Persistence is something EF takes care of, and you shouldn’t really have to care how it does it. That’s why I rail against putting foreign key properties in a class – you wouldn’t put them there if it weren’t persistent, would you? – and in using the LINQ Join statement … too databasey.

EFDesigner’s philosophy is that you’re modeling classes, not tables. Sure, there are a few properties that point to the fact that persistence is done via database tables, but that’s because we don’t live in a perfect world. Except for the connection string and maybe one or two other properties, the defaults serve well enough that you shouldn’t really have to mess with them.

I use Tangible’s editor. It says that it doesn’t like string interpolation (the $“string” thing), but that doesn’t matter … at runtime it just works, since the .NET version at runtime is more up-to-date than then one they use at edit time. (They should update that.)

0reactions
johnhenleycommented, Nov 26, 2018

Can you reopen this issue?

I have started evaluating EFDesigner, and run into a similar issue with the “_Id” being generated as the default suffix for a foreign key. In my situation, the relationship is a simple one, not a many-to-many.

According to Microsoft docs, “ID” is the “standard” for the foreign key suffix as specified here (see section “Renaming a Foreign Key That Is Not Defined in the Model”): https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/types-and-properties

It would be nice to have the suffix definable in the .efmodel properties, so that “_Id” or “ID” or “Id” can be specified as the default, and even could be overridden in each association perhaps.

In the meantime, I guess the solution is to copy and modify the .ttinclude directly in a given project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 common foreign key mistakes (and how to avoid them)
3 common foreign key mistakes (and how to avoid them) · 1. Mismatched data types · 2. Dangling foreign keys · 3. Not...
Read more >
Code generation fails when foreign key references a ...
There is a common misconception in jOOQ's code generation. In most RDBMS, a foreign key can reference any UNIQUE key, not only primary...
Read more >
Why is MySQL not properly naming my foreign key?
You are confusing the FOREIGN KEY and the INDEX that makes it work. Pay attention - there is NO expression name (which looks...
Read more >
Primary Key and Foreign Key Errors to Avoid
Unfortunately, in practice many databases have foreign key errors – about 50% of databases, we've found. Here are some common foreign key ......
Read more >
Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found