我創建了一個使用實體框架創建數據庫的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模型創建一個新的腳手架項目時,出現錯誤,指出這些數據類型沒有密鑰。
另一個問題:我知道我在問如何不把這些數據類型放入表中,但這是一種不好的做法嗎?我是否也應該將它們放入桌子?
另外:我可以有多個虛擬字段嗎?或者是否會破壞某些東西?
我真的很感激幫助
所以你不希望這些字段存儲在數據庫中?如果是這樣,你可以使用'[NotMapped]'註釋這個 – Thewads 2014-09-24 14:49:16
@Thewads謝謝你,試試吧!註釋去哪裏?在類聲明之前還是在裏面? – 2014-09-24 14:50:44
它仍然要求一個關鍵字,我把註釋放在類聲明的上方 – 2014-09-24 14:55:16