NetCoreTemplates / react-spa

.NET 8 React Vite TypeScript SPA with Tailwind
BSD 3-Clause "New" or "Revised" License
26 stars 7 forks source link

EntityFramework operations fails if you create new project with "." in the title #1

Closed Redvo closed 3 months ago

Redvo commented 3 months ago

If you create new project like x new react-spa MyCompany.MyApp, the resulting generated block of code in Configure.Db.cs looks like this:

// $ dotnet ef migrations add CreateIdentitySchema
// $ dotnet ef database update
services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(connectionString, b => b.MigrationsAssembly(nameof(MyCompany.MyApp))));

Because of how nameof operator works, it only passes "MyApp" as assembly name, even though full assembly name is "MyCompany.MyApp".

This causes running any dotnet ef command fail with error Could not load file or assembly 'MyApp, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Changing the code to use string litteral with full assembly name or getting assembly name through reflection fixes the issue.

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(connectionString, b => b.MigrationsAssembly("MyCompany.MyApp")));
services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(connectionString, b => b.MigrationsAssembly(Assembly.GetAssembly(typeof(ConfigureDb))?.FullName)));

https://github.com/NetCoreTemplates/react-spa/blob/fda9be1e7d1c2b85ba4a221647f42c492736caa5/MyApp/Configure.Db.cs#L23

mythz commented 3 months ago

The project name only expects a single name, e.g. MyCompany or MyApp not a name with a namespace.

I recommend recreating the template with just MyCompany to avoid other tempting issues.