我正在學習針對northwind數據庫的EF4.1。我創建了一個POCO類像這樣故意用非相同的列名,並沒有在該方案中存在的屬性,所以我可以學習,準備映射來更新我的遺留應用程序:在EF4.1中使用POCO映射FluentAPI
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public Decimal? UnitPrice { get; set; }
public bool Discontinued { get; set; }
public Int16 QuantityInStock { get; set; }
public int CategoryID { get; set; }
public Category Category { get; set; }
}
我要地圖我架構這個實體是這樣的:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Map(config =>
{
config.Properties(p => new
{
p.ProductName,
p.ProductID,
p.CategoryID,
p.Discontinued,
p.UnitPrice
});
});
modelBuilder.Entity<Product>().ToTable("Products");
base.OnModelCreating(modelBuilder);
}
奇怪的是,當我試試這個:
Product tmp = db.Products.Find(4);
我得到這個例外,我不知道爲什麼,因爲我所說的,我甚至映射它ToTable( 「產品」): {"Invalid object name 'dbo.Product1'."}
這究竟是爲什麼?
感謝。我發現這個Ignore選項,但每次我添加一個屬性到我的類時,我必須更新地圖忽略它,這是一個痛苦。更糟的是,我有現有的商業類,我想要使用有很多屬性,並會很痛苦,忽略每一個。有什麼辦法可以指定應該設置哪些屬性而不是忽略哪些屬性? – powlette 2012-03-18 21:26:28
@powlette:不,我不知道。 – Slauma 2012-03-18 21:30:27
這是之前問過的:http://stackoverflow.com/questions/9585263/use-entity-frameworks-structuraltypeconfiguration-ignore-to-ignore-all-proper – 2012-03-18 21:38:12