EntityFramework-Reverse-POCO-Code-First-Generator icon indicating copy to clipboard operation
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard

TableRename of view does not generate ToTable line in configuration map

Open WallyHartshorn opened this issue 4 years ago • 0 comments

I have a SQL view named vwSiteNotification.

I have a TableFilter array that includes that view:

    TableFilter = (Table t) =>
    {
        String[] tableNames = {
            "Sites",
            "Notifications",
            "vwSiteNotification"
        };

        return tableNames.Contains(t.Name, StringComparer.OrdinalIgnoreCase);
    };

I also have a TableRename function that, among other things, renames vwSiteNotification to be SiteNotificationView:

    TableRename = (string name, string schema, bool isView) =>
    {
        if (name == "Counties") {
            name = "County";
        }

        if (name.EndsWith("s")) {
            name = name.Remove(name.Length -1, 1);
        }

        if (isView)
		{
			if (name.StartsWith("vw")) {
				name = name.Remove(0, 2);
				name = name + "View";
			}
		}

        return name;
    };

When I save the *.tt file, it generates the various class files, including the SiteNotificationViewMap:

    [System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.33.0.0")]
    public partial class SiteNotificationViewMap : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<SiteNotificationView>
    {
        public SiteNotificationViewMap()
            : this("dbo")
        {
        }

        public SiteNotificationViewMap(string schema)
        {
            HasKey(x => new { x.SiteId, x.SiteName, x.NotificationId, x.NotificationDate });

            Property(x => x.SiteId).IsRequired();
            Property(x => x.SiteName).IsRequired().HasMaxLength(50);
            Property(x => x.NotificationId).IsRequired();
            Property(x => x.NotificationDate).IsRequired();
            Property(x => x.WebsitesCount).IsOptional();
            Property(x => x.LocationsCount).IsOptional();
            Property(x => x.DocumentsCount).IsOptional();
            InitializePartial();
        }
        partial void InitializePartial();
    }

However, when I run my application, it fails when it actually tries to access the view:

System.Data.SqlClient.SqlException Invalid object name 'dbo.SiteNotificationViews'

Apparently it knows to convert vwSiteNotification to SiteNotificationView (or "SiteNotificationViews" for the full dbSet), but it doesn't know how to convert the other way.

If I manually insert this line at the start of the SiteNotificationViewMap(string schema) constructor, then it works properly: ToTable("vwSiteNotification", schema);

Of course, whenever I save the *.tt file again, that line gets removed.

What am I doing wrong? I've tried comparing my *.tt file to one that works correctly in a different project, but I've been unable to find a difference that seems relevant. (This *.tt file was originally generated by version 2.33, but apparently the extension in my Visual Studio has updated itself to 3.3. I haven't yet talked to my boss about purchasing a license for it, so I don't know what effect that might have on anything.)

WallyHartshorn avatar Jan 15 '21 17:01 WallyHartshorn