我試圖實現這個scenario。我創建了Code First模型,然後從模型生成數據庫sql,並通過MigSharp創建了手動遷移。之後,我添加了代碼OnModelCreating
來更新映射。DbContext表映射
protected void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("dfg_Product");
modelBuilder.Entity<Customer>().ToTable("dfg_Customer");
}
問題是,仍然的DbContext試圖從默認映射「dbo.Product | dbo.Customer」的數據,我需要改變映射「dbo.dfg_Product | dbo.dfg_Customer」。 我試圖調試但OnModelCreating
沒有調用代碼。
請幫忙,我做錯了什麼?
編輯:添加連接字符串
<add name="DataModelContainer" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=TestDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
問題是通過改變連接字符串來解決:如果您之前執行的應用程序和模型保持不變
<add name="DataModelContainer" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
我看不到任何東西你的代碼立即出錯。如果OnModelCreating根本沒有被調用,那麼可能是因爲你沒有使用Code First,而是使用了項目中的EDMX。你的連接字符串是什麼樣的? – 2012-03-04 00:06:11
已添加到問題。所有表格都已在數據庫中創建,但帶有前綴。我需要的只是配置EF在表名之前添加此前綴。 – mironych 2012-03-04 00:20:29