MVC变量表之间的关系

时间:2017-11-08 18:03:52

标签: asp.net-mvc

我是MVC新手并且习惯使用Forms。我的问题是假设我有四个表的基本模型设置,包括以下字段;

    Branch
    ------
    Branch_Nu
    Branch_Address

    Orders
   -----
    Order_Nu
    Branch_Nu
    Product_Nu
    Customer_Nu
    totcost

    Products
    ---------
    Product_Nu
    Product
    Price

    Customer
    ----------
    Customer_Nu
    Name
    Address
    City
    St
    Zip

我对以下场景很感兴趣,

I want to see all orders for a branch; 
Branch->orders

I want to see all orders for a customer from a particular branch;
Branch->orders->products
         |-->customer

I want to see all orders for a customer regardless of branch they
purchased from; 
customer->orders->branch

I want to see all branches that sold a particular product;
products->orders->Branch

I want to see which customers bought a particular product;
products->orders->customer

问题是我是否可以使用不同的控制器方法使用相同的基本模型为不同的场景使用不同的控制器,或者我是否需要针对不同场景使用不同的模型,然后将其提交给不同的控制器方法?

如果我使用表单,我会在MVC中为每个场景设置不同的select语句和表单?

2 个答案:

答案 0 :(得分:0)

  

问题是我可以使用相同的基本模型为不同的场景使用不同的控制器

  

我是否需要针对不同场景的不同模型,然后将其提交给不同的控制器方法

你不需要它们,但一般来说,使用不同逻辑函数的模型往往(并不总是)打破Single Responsibility Principle

在您的实例中,您似乎正在通过不同的视图(而不是逻辑)查看相同的数据。订单是订单,为了在您网站上的任何位置查看目的,通常我使用相同的模型。 放置订单(不同的逻辑功能)我很可能会有一个新模型。

考虑到您正在查看相同的数据,这是MVC Templates最佳使用的典型示例。

  

如果我使用表单,我会在MVC中为每个场景设置不同的select语句和表单?

我可能会设计它:

I want to see all orders for a branch; 
Branch->orders
public class BranchController() { public ActionResult Orders() {}}

I want to see all orders for a customer from a particular branch;
Branch->orders->products
         |-->customer
public class BranchController() 
{ 
   public ActionResult Orders() {}
   public ActionResult CustomerOrders() {}
   public ActionResult ProductOrders() {}
}

// ETC

模型示例:

public class OrderVM
{
  //I would get rid of hungarian notation
  // https://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation
  public int OrderId { get; set; }
  public int BranchId { get; set; }
  public int ProductId { get; set; }
  public int CustomerId { get; set; }
}

然后在/views/shared/templates/display/OrderVM.cshtml中存储如何呈现html,以便在整个应用程序中使用它,允许每个控制器/区域覆盖。

答案 1 :(得分:0)

您的实体框架模型代表您的类之间的关系。在代码优先方法中,您的模型将决定数据库的设计。以以下三个类为例:

public class Cat 
{
  [Key]
  public int Id {get; set;} 

  public string Name {get; set;}

  //Foreign key to animalgroup table
  public int AnimalGroupId {get; set;}

  //navigation property to AnimalGroup
  //allows you to do myCat.AnimalGroup outside of this class to retrieve
  //the associated animal group
  public virtual AnimalGroup AnimalGroup {get; set;}
}

public class Dog
{ 
  [Key]
  public int Id {get; set;}

  public string Name {get; set;}

  public bool IsFluffy {get; set;}

  //Foreign key to animalgroup table
  public int AnimalGroupId {get; set;}

  //navigation property to AnimalGroup
  //allows you to do myDog.AnimalGroup outside of this class to retrieve
  //the associated animal group
  public virtual AnimalGroup AnimalGroup {get; set;}
}

public class AnimalGroup
{
  [Key]
  public int Id {get; set;}

  public string Name {get; set;}     

  public virtual ICollection<Cat> Cats {get; set;}

  public virtual ICollection<Dog> Dogs {get; set;
}

这表示两个一对多的关系。 AnimalGroup可以包含多个Cats,AnimalGroup可以包含多个Dogs。您可以编写查询来执行CRUD操作。一个非常简单的例子如下:

//create and save an animal group
AnimalGroup group = new AnimalGroup();
group.Name = "my animal group";
_dbContext.AnimalGroups.Add(group);
_dbContext.SaveChanges();

//create and save a cat associated with the animal group
Cat myCat = new Cat();
cat.Name = "kitty";
cat.AnimalGroupId = group.Id;
_dbContext.Cats.Add(myCat);

_dbContext.SaveChanges();
相关问题