ASP.NET MVC使用DbSet与数据库使用点表示法

时间:2017-08-22 11:25:31

标签: asp.net-mvc

我正在尝试在ASP.NET MVC应用程序中实现WorldWideImporters示例数据库。

使用单个表“Persons”的测试SQL数据库,一切正常。

然而,当使用WorldWideImporters时,每个表都按类别排序:Application.Cities和Application.Countries或Sales.Orders和Sales.Customers。

我通常会在这里编写我的模型:

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; }
    }

我的控制器是这样的:

 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());
        }
    }

我不再能够在数据库表中使用点进行排序,例如Sales.Orders在上述模型和控制器中不起作用。如何为此示例数据库正确建模和控制器?

这就是我想做的事情:

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; }
}

1 个答案:

答案 0 :(得分:1)

点之前的这些前缀称为架构

有一些参考资料解释,但基本上你可以做;

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

仍然按照它的名称调用表格(没有点之前的部分)

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

Entity Framework and multiple schemas