2014-03-29 93 views
1

我正在設計一個多租戶網站,首先使用EF6代碼,MVC和其他來自MS堆棧的網站。多租戶和EF6設計

我想爲每個租戶公告。很簡單,我的EF代碼第一類將是這個樣子:

class Announcement 
{ 
    public Announcement() 
    { 
     DateCreated = DateTime.Now; 
    } 

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

    public DateTime DateCreated { get; set; } 

    public string Title { get; set; } 

    public string Message { get; set; } 

    public virtual Tenant Tenant { get; set; } 

    public ApplicationUser Author { get; set; } 
} 

我的設計的問題是什麼,如果我想要的網站管理員不得不發佈通知到所有租戶的能力嗎?

由於數據庫將強制Tenant關係,因此我無法將Tenant屬性設置爲虛假。

在EF之前,我會做這樣的事情,但現在我會失去很好的EF導航屬性。

class Announcement 
{ 
    public Announcement() 
    { 
     DateCreated = DateTime.Now; 
    } 

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

    public DateTime DateCreated { get; set; } 

    public string Title { get; set; } 

    public string Message { get; set; } 

    public int TenantID { get; set; } 

    public ApplicationUser Author { get; set; } 
} 

我會適當地使用TenantID所有基於租客通知,但將其設置爲0站點範圍內的通知。

那麼,有沒有更好的設計(除了兩個類/表),仍然可以利用EF導航屬性?

+1

你爲什麼選擇該模式同出一tenantid問題。你可以將它設置爲db中的一個可爲空的屬性,模型將具有'public int? tenantId {get; set;}' – Saravanan

+0

這是我的問題的基礎。我知道我可以使用一個int TenantID字段(可以爲空或不可),但是我失去了很好的EF導航屬性。 –

+0

我認爲在一個多租戶應用程序中,您應該爲獨立於租戶的內容分離結構。在這種情況下:單獨的「SiteAnnouncement」表。它看起來可能是多餘的,但是您應該將租戶數據視爲單獨的數據庫。例如,如果將來你想記錄哪個租戶已經閱讀了哪個公告?對於這兩種公告來說,情況會大不相同。 –

回答

0

什麼是

class Announcement 
{ 
    public Announcement() 
    { 
     DateCreated = DateTime.Now; 
    } 

[Key] 
public virtual int Id { get; set; } 

public virtual DateTime DateCreated { get; set; } 

public virtual string Title { get; set; } 

public virtual string Message { get; set; } 

public virtual int TenantID { get; set; } // during insert/Update set as required. the tenant should exist. 
              // A dummy SYSTEM wide tenant may be an option to consider 

// navigation props 
[ForeignKey("TenantID")] 
public virtual Tenant Tenant { get; set; } // <<<<< NAV as before 

public virtual ApplicationUser Author { get; set; } 
} 
+0

你將如何輸入系統範圍的公告?在你的班級,我認爲只有虛擬租戶才能工作。 –

+0

系統範圍條目將適用於所有租戶。當然,如果你需要租戶級別的消息控制。即Tenant X已經讀取/接收到該消息,因此不再顯示等,然後實際上可能需要爲每個租戶寫一條記錄。但你的設計不是我。我只是指出那裏有NAV。而且NAV這個類別也沒有像預期的那樣得到相當的宣佈。 –