Mapping POCOs in "schema.tablename" format in EF 4.1 code first, using dotconnect for Oracle
I have this entity:
public class MyEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
I want this entity to mapped into Oracle an oracle 11g database as MySchema.MyEntity
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().ToTable("MyEntity", "MySchema");
base.OnModelCreating(modelBuilder);
}
The problem is that when I try to add an entity ,and do SaveChanges, it completely ignores the schema part of the ToTable(), even if I add a [Table("MySchema.MyEntity") ] attribute to the class it is also ignored. Schema will always be the login name of the connnection string regardless of what I do.
DbConnection con = new Devart.Data.Oracle.OracleConnection(
"User Id=system;Password=admin;Server=XE;Persist Security Info=true;");
The schema name is always what I set as UserId. It only changes if I write down explicitly that:
con.ChangeDatabase("MySchema"); //this will only work if the database connection is open...
But I do now want to write this down...
How to make this work?
EDIT:
Oh man... Solution :
First : UPPERCASESCHEMANAMES!!!
Second: In the offical dotconnect example there is a line:
config.Workarounds.IgnoreSchemaName = true;
Delete it... (this will only 开发者_运维问答work if you set the schema name for ALL your entities, otherwise a "dbo" schema will be used that does not exist in oracle... )
kori0129, your solution is correct. The corresponding blog article is here: http://www.devart.com/blogs/dotconnect/index.php/entity-framework-code-first-support-for-oracle-mysql-postgresql-and-sqlite.html .
If you encounter any difficulties with the dotConnect for Oracle functionality, please contact us via http://www.devart.com/company/contact.html .
I used the ConnectionString to get the SCHEMA
Here's my solution:
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; }
}
精彩评论