C# 在“中映射POCO”;schema.tablename";首先在EF 4.1代码中格式化,使用dotconnect for Oracle
我有这个实体:C# 在“中映射POCO”;schema.tablename";首先在EF 4.1代码中格式化,使用dotconnect for Oracle,c#,.net,oracle,ef-code-first,code-first,C#,.net,Oracle,Ef Code First,Code First,我有这个实体: public class MyEntity { [Key] public int Id { get; set; } public string Name { get; set; } } 我希望将此实体映射到Oracle的Oracle 11g数据库MySchema.MyEntity中 protected override void OnModelCreating(DbModelBuilder modelBuilder) {
public class MyEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
我希望将此实体映射到Oracle的Oracle 11g数据库MySchema.MyEntity中
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().ToTable("MyEntity", "MySchema");
base.OnModelCreating(modelBuilder);
}
模式名称始终是我设置为UserId的名称。只有当我明确写下以下内容时,它才会改变:
con.ChangeDatabase("MySchema"); //this will only work if the database connection is open...
但我现在确实想把这写下来
如何做到这一点
编辑:
哦,老兄。。。解决方案:
第一:大写模式名称
第二:在Official dotconnect示例中,有一行:
config.Workarounds.IgnoreSchemaName = true;
删除它。。。(仅当您为所有实体设置模式名称时,这才有效,否则将使用oracle中不存在的“dbo”模式…kori0129,您的解决方案是正确的。相应的博客文章如下:
如果您在使用dotConnect for Oracle功能时遇到任何困难,请通过与我们联系。我使用ConnectionString获取架构 以下是我的解决方案:
public class MyContext : DbContext
{
private string oracleSchema;
public MyContext()
: base("OracleConnectionString")
{
Database.SetInitializer<MyContext>(null);
oracleSchema = new System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString).UserID.ToUpper();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().ToTable(string.Format("{0}.{1}", oracleSchema, "CUSTOMER"));
modelBuilder.Entity<Invoice>().ToTable(string.Format("{0}.{1}", oracleSchema, "INVOICE"));
modelBuilder.Entity<Product>().ToTable(string.Format("{0}.{1}", oracleSchema, "PRODUCT"));
modelBuilder.Entity<Category>().ToTable(string.Format("{0}.{1}", oracleSchema, "CATEGORY"));
modelBuilder.Entity<Item>().ToTable(string.Format("{0}.{1}", oracleSchema, "ITEM"));
modelBuilder.Entity<Invoice>().HasRequired(p => p.Customer);
modelBuilder.Entity<Item>().HasRequired(p => p.Invoice);
modelBuilder.Entity<Item>().HasRequired(p => p.Product);
modelBuilder.Entity<Product>()
.HasMany(x => x.Categories)
.WithMany(x => x.Products)
.Map(x =>
{
x.ToTable("ASS_CATEGORY_PRODUCT", oracleSchema);
x.MapLeftKey("ID_CATEGORY");
x.MapRightKey("ID_PRODUCT");
});
modelBuilder.Entity<Category>()
.HasMany(x => x.Products)
.WithMany(x => x.Categories)
.Map(x =>
{
x.ToTable("ASS_CATEGORY_PRODUCT", oracleSchema);
x.MapLeftKey("ID_PRODUCT");
x.MapRightKey("ID_CATEGORY");
});
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<Item> Items { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
公共类MyContext:DbContext
{
私有字符串oracleSchema;
公共MyContext()
:base(“OracleConnectionString”)
{
Database.SetInitializer(null);
oracleSchema=new System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionString[“OracleConnectionString”].ConnectionString.).UserID.ToUpper();
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().ToTable(string.Format(“{0}.{1}”,oracleSchema,“CUSTOMER”);
modelBuilder.Entity().ToTable(string.Format(“{0}.{1}”,oracleSchema,“发票”);
modelBuilder.Entity().ToTable(string.Format(“{0}.{1}”,oracleSchema,“PRODUCT”);
modelBuilder.Entity().ToTable(string.Format(“{0}.{1}”,oracleSchema,“CATEGORY”);
modelBuilder.Entity().ToTable(string.Format(“{0}.{1}”,oracleSchema,“ITEM”);
modelBuilder.Entity().HasRequired(p=>p.Customer);
modelBuilder.Entity().HasRequired(p=>p.Invoice);
modelBuilder.Entity().HasRequired(p=>p.Product);
modelBuilder.Entity()
.HasMany(x=>x.Categories)
.有许多(x=>x.Products)
.Map(x=>
{
x、 ToTable(“ASS\u CATEGORY\u PRODUCT”,oracleSchema);
x、 MapLeftKey(“ID_类别”);
x、 MapRightKey(“ID_产品”);
});
modelBuilder.Entity()
.HasMany(x=>x.Products)
.WithMany(x=>x.Categories)
.Map(x=>
{
x、 ToTable(“ASS\u CATEGORY\u PRODUCT”,oracleSchema);
x、 MapLeftKey(“ID_产品”);
x、 MapRightKey(“ID_类别”);
});
}
公共数据库集客户{get;set;}
公共数据库集{get;set;}
公共数据库集项{get;set;}
公共数据库集产品{get;set;}
公共数据库集类别{get;set;}
}
[表(“MyEntity”,Schema=“MySchema”)]也被忽略
public class MyContext : DbContext
{
private string oracleSchema;
public MyContext()
: base("OracleConnectionString")
{
Database.SetInitializer<MyContext>(null);
oracleSchema = new System.Data.SqlClient.SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString).UserID.ToUpper();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().ToTable(string.Format("{0}.{1}", oracleSchema, "CUSTOMER"));
modelBuilder.Entity<Invoice>().ToTable(string.Format("{0}.{1}", oracleSchema, "INVOICE"));
modelBuilder.Entity<Product>().ToTable(string.Format("{0}.{1}", oracleSchema, "PRODUCT"));
modelBuilder.Entity<Category>().ToTable(string.Format("{0}.{1}", oracleSchema, "CATEGORY"));
modelBuilder.Entity<Item>().ToTable(string.Format("{0}.{1}", oracleSchema, "ITEM"));
modelBuilder.Entity<Invoice>().HasRequired(p => p.Customer);
modelBuilder.Entity<Item>().HasRequired(p => p.Invoice);
modelBuilder.Entity<Item>().HasRequired(p => p.Product);
modelBuilder.Entity<Product>()
.HasMany(x => x.Categories)
.WithMany(x => x.Products)
.Map(x =>
{
x.ToTable("ASS_CATEGORY_PRODUCT", oracleSchema);
x.MapLeftKey("ID_CATEGORY");
x.MapRightKey("ID_PRODUCT");
});
modelBuilder.Entity<Category>()
.HasMany(x => x.Products)
.WithMany(x => x.Categories)
.Map(x =>
{
x.ToTable("ASS_CATEGORY_PRODUCT", oracleSchema);
x.MapLeftKey("ID_PRODUCT");
x.MapRightKey("ID_CATEGORY");
});
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<Item> Items { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}