2011-10-07 61 views
2

我有这样的实体:映射波苏斯在EF“schema.tablename”格式4.1代码首先,使用dotconnect甲骨文

public class MyEntity 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 


} 

我想这个实体映射到甲骨文Oracle 11g数据库作为MySchema.MyEntity

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<MyEntity>().ToTable("MyEntity", "MySchema"); 
     base.OnModelCreating(modelBuilder); 
    } 

的问题是,当我尝试添加一个实体,做的SaveChanges,它完全忽略了ToTable()的模式部件,即使我添加了一个表(“MySchema.MyEntity”)]属性的类也会被忽略。无论我做什么,架构将永远是连接字符串的登录名。

 DbConnection con = new Devart.Data.Oracle.OracleConnection(
     "User Id=system;Password=admin;Server=XE;Persist Security Info=true;"); 

架构名称总是我设置为UserId。它只有当我写下明确的改变:

con.ChangeDatabase("MySchema"); //this will only work if the database connection is open... 

但现在我想将它记下来......

如何使这项工作?

编辑:

噢,伙计......解决方法:

第一:UPPERCASESCHEMANAMES!

二:在官方dotconnect例子有一行:

config.Workarounds.IgnoreSchemaName = true; 

删除它...(这个,如果你设置你所有的实体架构名称只会工作,否则“DBO”模式将被使用,在oracle中不存在...)

+0

表( “myEntity所” 模式= “MYSCHEMA”) 也将被忽略 –

+0

问题得到解决.. –

回答

1

我用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; } 
    }