を使用してクラスごとにエンティティのスキーマ名を定義できることはわかっていますが、特定のToTable("TableName", "SchemaName")
タイプの関係を使用しているときに奇妙な結果が得られるため、構成内のすべてのテーブルのスキーマ名を設定できるように設定する方法はありますか?dbo.TableName
内部SQLクエリでデフォルトに戻るマッピングと分割エンティティマッピング
SQL出力の例については、この以前の投稿を参照してください
を使用してクラスごとにエンティティのスキーマ名を定義できることはわかっていますが、特定のToTable("TableName", "SchemaName")
タイプの関係を使用しているときに奇妙な結果が得られるため、構成内のすべてのテーブルのスキーマ名を設定できるように設定する方法はありますか?dbo.TableName
内部SQLクエリでデフォルトに戻るマッピングと分割エンティティマッピング
SQL出力の例については、この以前の投稿を参照してください
Oracleデータベースを持っています-最初にEF4.1で、すべてのマッピングはデータ注釈で行われます。テスト環境と本番環境での異なるスキーマ名。私の解決策は、流暢なAPI、リフレクション、遅延バインディングを利用して、OnModelCreating中にスキーマを動的にマッピングすることです。ジェネリック型のすべてのContextクラスのプロパティを繰り返し処理し、ダーティな作業を行います。これまでのところ私のために働きます。
public class Context : DbContext
{
public Context()
: base(new OracleConnection(ConfigurationManager.ConnectionStrings["OraAspNetConString"].ConnectionString), true)
{
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (var p in typeof(Context).GetProperties().Where(foo=>foo.PropertyType.IsGenericType))
{
// this is what we are trying to accomplish here --
//modelBuilder.Entity<User>().ToTable("TBL_USERS", "TestSchema");
Type tParam = p.PropertyType.GetGenericArguments()[0]; // typeof User
MethodInfo generic = typeof(DbModelBuilder).GetMethod("Entity").MakeGenericMethod(new[] { tParam });
object entityTypeConfig = generic.Invoke(modelBuilder, null);
MethodInfo methodToTable = typeof(EntityTypeConfiguration<>).MakeGenericType(new[] { tParam }).GetMethod("ToTable", new Type[] { typeof(string), typeof(string) });
methodToTable.Invoke(entityTypeConfig, new[] { GetMappedTableName(tParam), currentOraSchemaName });
}
base.OnModelCreating(modelBuilder);
}
private string currentOraSchemaName = ConfigurationManager.AppSettings.Get("OraSchemaName");
private string GetMappedTableName(Type tParam)
{
TableAttribute tableAttribute = (TableAttribute)tParam.GetCustomAttributes(typeof(TableAttribute), false).FirstOrDefault();
return tableAttribute.Name;
}
}
ここでは、ハードコードされたスキーママッピングのないユーザークラス-
[Table("TBL_USERS")]
public class User
{
[Column("USER_ID")]
public string UserId { get; set; }
[Column("USER_NAME")]
public string Name { get; set; }}
最終的なEFv4.1バージョンにはカスタム規則用のパブリックAPIがないため、APIからスキーマをグローバルに変更することはできません。