2014-09-24 14 views
0

我創建了一個使用實體框架創建數據庫的MVC項目。我有兩個模型,每個模型都有5個字段。但是,我希望這些字段中的一些具有我自己創建的唯一返回類型,但我不希望將唯一數據類型製作成表格。如何防止我的實體模型中的類型和屬性被保存到數據庫中?

編輯

每評論:我只想澄清,我仍然喜歡的人與職業表有相應的部分

例如歲以下的兒童和地址數據:

型號:

class Person { 
[Key] 
public int PersonID{get;set} 
public string name {get;set;} 
public int age {get;set;} 
//note below: return type is ICollection of type Kids 
//or it can just be a return type of type Kids like so 
//public Kids kids {get;set;} 
public ICollection<Kids> kids {get;set;} 
public int OccupationID{get;set;} 
public virtual Occupation Occupation {get;set} 
} 

class Occupation { 
[Key] 
public int OccupationID{get;set;} 
public string OccupationName{get;set} 
//note the line below has a return type of Address 
public Address Location{get;set;} 
public int PersonID{get;set;} 
public virtual Person Person{get;set;} 

} 

ot使用上述

class Kids { 
public int numberOfKids{get;set;} 
public List<string> namesOfKids{get;set;} 
} 

class Address { 
public string street{get;set;} 
public string city{get;set;} 
public int apartmentNumber{get;set;} 
} 

她的數據類型,如果我不希望做Kids並要做出的Address數據類型到數據庫表中,其中將這些被放置在項目?我問這個是因爲我試圖創建一個名爲「src」的新文件夾並將它們放在該文件夾中,但是當我嘗試爲Person模型創建一個新的腳手架項目時,出現錯誤,指出這些數據類型沒有密鑰。

另一個問題:我知道我在問如何不把這些數據類型放入表中,但這是一種不好的做法嗎?我是否也應該將它們放入桌子?

另外:我可以有多個虛擬字段嗎?或者是否會破壞某些東西?

我真的很感激幫助

+1

所以你不希望這些字段存儲在數據庫中?如果是這樣,你可以使用'[NotMapped]'註釋這個 – Thewads 2014-09-24 14:49:16

+0

@Thewads謝謝你,試試吧!註釋去哪裏?在類聲明之前還是在裏面? – 2014-09-24 14:50:44

+0

它仍然要求一個關鍵字,我把註釋放在類聲明的上方 – 2014-09-24 14:55:16

回答

2

如果你是想實現的是沒有得到在數據庫中創建某一類的字段,那麼你可以在創建時利用[NotMapped]數據註解

class Occupation { 
    [Key] 
    public int OccupationID{get;set;} 
    public string OccupationName{get;set} 

    [NotMapped] 
    public Address Location{get;set;} 
    public int PersonID{get;set;} 
    public virtual Person Person{get;set;} 
} 

的在上述情況下,您的數據庫結構,以及查詢你的數據庫,[NotMapped]屬性下的字段將被忽略。

可以閱讀更多關於類在這裏:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute(v=vs.110).aspx

除了這一點,不包括在你的DbContext任何類你不想爲

創建表此外,至於你can I have multiple virtual fields? or does that break something?去:

virtual實體框架中的屬性主要用於允許延遲加載,如果你有它在你的系統配置。

但它也對變化跟蹤有影響。如果您使用純POCO或使用代理,但不要使屬性變爲虛擬,則更改涉及更改檢測。您可以在這裏詳細閱讀有關:http://blog.oneunicorn.com/2012/03/10/secrets-of-detectchanges-part-1-what-does-detectchanges-do/

1

如果你不想一類映射,只是不包括它在DbContext類作爲DbSet<T>

如果一個類裏面,你不想被映射的屬性,你可以在屬性中使用NotMapped屬性,或者,如果你正在使用流利的API,你可以做這樣的事情:

modelBuilder.Entity<Person>().Ignore(p => p.LastName); 
0

一種方式來做到這一點是使用一個接口來塑造類,你想要的方式和組成的干將的複雜對象:

public interface IPerson 
{ 
    string name {get;set;} 
    int age {get;set;} 
    Kids kids {get;} 
    Occupation Occupation {get;} 
    void AddKid(string name); 
} 

public class Person : IPerson 
{ 
    [Key] 
    public int PersonID{get;set} 
    public string name {get;set;} 
    public int age {get;set;} 
    [NotMapped] 
    public Kids kids { get 
     { 
      Kids k = new Kids 
      { 
       namesOfKids = (new [] 
       { 
        Name1, 
        Name2, 
        Name3, 
        // ... 
       }).Where(n => n != null).ToList(); 
      }; 
      k.numberOfKids = k.namesOfKids.Count; 
      return k; 
     } 
    } 

    public int numberOfKids{get; set;} 
    public List<string> namesOfKids{get; set;} 

    public int OccupationID{get;set;} 
    [NotMapped] 
    public Occupation Occupation {get { // TODO }} 
    public string Name1 { get; set; } 
    public string Name2 { get; set; } 
    // ... Add more name fields up to the max 

    public void AddKid(string name) 
    { 
     switch (numberOfKids) 
     { 
      case 0: Name1 = name; 
        break; 
      case 1: Name2 = name; 
        break; 
      // ... 

     } 
     numberOfKids ++; 
    } 
} 

class Kids 
{ 
    public int numberOfKids{get;set;} 
    public List<string> namesOfKids{get;set;} 
} 

需要注意的是,您只能在代碼中使用IPerson對象。你可以直接將任何Person對象投射到IPerson

這不是編譯或測試,但它應該給如何實現你的目標的想法。

相關問題