I realised that I could have spent the last twenty years putting all my tech knowledge online. Well, I didn't, but as they say, the best time to plant trees is 20 years ago, the second best time is now. So here's all the random programming stuff I've come across that might be helpful. Note: this isn't really questions and answers - more just answers from me!

ASP.NET Core Web Forms App: A namespace cannot directly contain members such as fields

0 votes

After creating a new Razor web forms app I scaffolded in an existing database:

Scaffold-DbContext "Server=<server>;Database=<db-name>;User ID=<username>;Password=<password>;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

And kept getting an error on the newly created entries:

A namespace cannot directly contain members such as fields 

After ensuring I had added Entity Framework to the project using NuGet, I still got the error.

So it turns out I was using a namespace like this:

using System;
using System.Collections.Generic;

namespace web-app3.Models
    public partial class TblStories

But this isn't allowed - no hyphens. I had to rename my project to webapp3 and it was all fine.

asked Nov 20, 2018 in .NET by Ralpharama (380 points)

Please log in or register to answer this question.