1

I am trying to implement the WorldWideImporters sample database in an ASP.NET MVC application.

Everything is working using just a test SQL database with a single table "Persons".

However when using the WorldWideImporters every table is sorted into categories: Application.Cities and Application.Countries or Sales.Orders and Sales.Customers.

Where I would normally write my model like this:

public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options)
        { }

        public DbSet<Person> Persons { get; set; }
    }

    public class Person
    {
        [Key]
        public int PersonID { get; set; }
        [Required]
        public string FirstName { get; set; }
    }

And my controller like this:

 public class PeronsController : Controller
    {
        private readonly DataContext _context;

        public PeronsController(DataContext context)
        {
            _context = context;
        }

        // GET: Orders
        public async Task<IActionResult> Index()
        {
            return View(await _context.Persons.ToListAsync());
        }
    }

I am no longer able when the database tables are sorted with a dot for instance Sales.Orders does not work in the above model and controllers. How can I correctly model and controller for this sample database?

This is what I would like to do:

public class ShopContext : DbContext
{
    public ShopContext(DbContextOptions<ShopContext> options) : base(options)
    { }

    public DbSet<City> Application.Cities { get; set; }
}

public class City
{
    [Key]
    public int CityID { get; set; }
    [Required]
    public string CityName { get; set; }
}
dwigt
  • 611
  • 1
  • 8
  • 18

1 Answers1

1

These prefixes before the dot are called the Schema

There are a few references explaining but basically you can do;

[Table("Customers", Schema = "Ordering")]       

And still call the table by it's name (without the part preceding the dot)

http://devproconnections.com/entity-framework/working-schema-names-entity-framework-code-first-design

Entity Framework and multiple schemas

Milney
  • 6,253
  • 2
  • 19
  • 33