首先使用dotconnect for Oracle在EF 4.1代码中以“schema.tablename”格式映射POCO

时间:2011-10-07 09:17:57

标签: 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)
    {
        modelBuilder.Entity<MyEntity>().ToTable("MyEntity", "MySchema");
        base.OnModelCreating(modelBuilder);
    }

问题在于,当我尝试添加实体并执行SaveChanges时,它完全忽略了ToTable()的架构部分,即使我在类中添加了[Table(“MySchema.MyEntity”)]属性它也被忽略了。无论我做什么,Schema都将始终是connnection字符串的登录名。

        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;

删除它...(这只有在为所有实体设置模式名称时才有效,否则将使用oracle中不存在的“dbo”模式...)

2 个答案:

答案 0 :(得分:1)

kori0129,您的解决方案是正确的。相应的博客文章位于:http://www.devart.com/blogs/dotconnect/index.php/entity-framework-code-first-support-for-oracle-mysql-postgresql-and-sqlite.html

如果您在使用dotConnect for Oracle功能时遇到任何困难,请通过http://www.devart.com/company/contact.html与我们联系。

答案 1 :(得分:1)

我使用ConnectionString来获取SCHEMA

这是我的解决方案:

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