sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
706 stars 230 forks source link

HasNoKey generated on StoredProcedureReturnTypes #754

Open erwin-faceit opened 2 years ago

erwin-faceit commented 2 years ago

In my database, I have a table named PerformanceCode. This is a simple table with a few fields, and a primary key. I also have a stored procedure named ValidPerformanceCode.

In order to prevent another viewmodel, I use the configuration:

Settings.StoredProcedureReturnTypes.Add("ValidPerformanceCodes", "PerformanceCode");

This generated the following code for the stored procedure (which is correct):

// dbo.ValidPerformanceCodes
public IQueryable<PerformanceCode> ValidPerformanceCodes(DateTime? fromDate, DateTime? toDate)
{
    return Set<PerformanceCode>()
        .FromSqlRaw("SELECT * FROM [dbo].[ValidPerformanceCodes]({0}, {1})", fromDate, toDate)
        .AsNoTracking();
}

It also generated the following configuration for the PerformanceCode table (which is also correct):

[GeneratedCode("EF.Reverse.POCO.Generator", "v3.6.0")]
// PerformanceCode
public partial class PerformanceCodeConfiguration : IEntityTypeConfiguration<PerformanceCode>
{
    public void Configure(EntityTypeBuilder<PerformanceCode> builder)
    {
        builder.ToTable("PerformanceCode", "dbo");
        builder.HasKey(x => x.Id).HasName("PK_PerformanceCode").IsClustered();

        ... rest omitted for brevity

However, it will also generate the following line in the OnModelCreating method:

modelBuilder.Entity<PerformanceCode>().HasNoKey();

The last line makes the application throw an exception, and it shouldn't be there.

For now, the workaround is to create partial class with custom configuration to disable the key first, and then add it again in OnModelCreatingPartial.

In my opinion, the HasNoKey line should only be created if the type is not already a known return type.

erwin-faceit commented 2 years ago

A better workaround is, of course, to exclude the stored procedure and add the generated code manually in a partial Entities class, which is what I've done now.

sjh37 commented 2 years ago

Thanks @erwin-faceit I will investigate this.

sjh37 commented 2 years ago

Hi @erwin-faceit Could you grab the latest code for EF.Reverse.POCO.v3.ttinclude and copy it over your EF.Reverse.POCO.v3.ttinclude file and that should fix this problem.

I'll close this case, but if it does not fix it, please let me know and I'll re-open it.

erwin-faceit commented 2 years ago

Alright, this fixes this specific case, but it opens a new one ;| (sorry)

Now, my Table Valued Functions from ANOTHER schema have no HasNoKeys anymore. Strange thing is that TVF's in the dbo schema are good.

sjh37 commented 2 years ago

hmm that is strange,