2017-11-08 10 views
0

私はMVCを初めて使い、Formsの使用に慣れています。私の質問は、私は次のフィールドを持つ4つのテーブルの基本的なモデルのセットアップがあると仮定します。MVCテーブル間の関係の比較

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では各シナリオごとに異なる選択ステートメントとフォームがありますか?

答えて

0

私は同じ基本的なモデルを使用して、異なるシナリオに異なるコントローラを使用することができている質問

はい

私はその後、別に提出され、異なるシナリオのための異なるモデルが必要なのかコントローラのメソッド

これらは必要ありませんが、一般的にdiffeレンタル論理機能は、必ずしもそうではなく、Single Responsibility Principleを壊す傾向があります。

異なるビュー(ロジックではない)で同じデータを表示しているようです。オーダーはオーダーであり、サイトのどこからでも見ることができます。一般に、私は同じモデルを使用します。 注文(別の論理関数)私はおそらく新しいモデルを持っています。

これは、同じデータを表示していることを考慮すると、MVC Templatesの最高の使用例です。

フォームを使用していた場合、MVCでは各シナリオごとに異なる選択ステートメントとフォームがありますか?

私はおそらくそれが好きなデザインになります。

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

次に保存 HTMLはそれがアプリケーション全体で使用することができます/views/shared/templates/display/OrderVM.cshtmlにレンダリングする方法コントローラ/エリアごとのオーバーライドも可能です。

0

エンティティフレームワークモデルは、クラス間の関係を表します。コードの最初のアプローチでは、モデルによってデータベースの設計が決まります。たとえば、次の3つのクラスを取ってください。

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

これは2対1の関係を表します。 AnimalGroupは複数の猫を含むことができ、AnimalGroupは多くの犬を含むことができる。 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(); 
関連する問題