我正在使用ODAC将带有SQL Server的proyect迁移到Oracle。 NET 4.0和实体框架6。
我可以访问这个基础并创建表但是当EF尝试读取这个表时我有这个例外:错误:ORA-00942。我在DB中检查这些表,它存在,但是如果我试着用这个查找内容(select * from table
)我有一个错误“表不存在”但如果我尝试使用双引号我可以({{1} })。
我在实体中放了一些代码来选择我的模式,因为没有这个我有其他例外:
select * from "table"
这是我的DbMigration代码:
public class ChatEntities : DbContext
{
public ChatEntities(): base ("ChatContext")
{
}
public DbSet<ChatUser> ChatUser { get; set; }
public DbSet<Chat> Chat { get; set; }
public DbSet<ChatPeople> ChatPeople { get; set; }
public DbSet<ChatHistory> ChatHistory { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("PROYECT");
modelBuilder.Entity<ChatUser>().ToTable("ChatUser", schemaName: "PROYECT");
modelBuilder.Entity<Chat>().ToTable("Chat", schemaName: "PROYECT");
modelBuilder.Entity<ChatPeople>().ToTable("ChatPeople", schemaName: "PROYECT");
modelBuilder.Entity<ChatHistory>().ToTable("ChatHistory", schemaName: "PROYECT");
}
}
.........
在每个班级中我都放了这个数据注释
string schema = "PROYECT.";
public override void Up()
{
CreateTable(
schema + "Chats",
c => new
{
Id = c.Decimal(nullable: false, precision: 10, scale: 0, identity: true),
AdminId = c.Decimal(nullable: false, precision: 10, scale: 0),
})
.PrimaryKey(t => t.Id);
CreateTable(
schema + "ChatHistories",
c => new
{
Id = c.Decimal(nullable: false, precision: 10, scale: 0, identity: true),
ChatId = c.Decimal(nullable: false, precision: 10, scale: 0),
UserId = c.Decimal(nullable: false, precision: 10, scale: 0),
Message = c.String(),
Date = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey(schema + "Chats", t => t.ChatId, cascadeDelete: true)
.Index(t => t.ChatId);
我在其他帖子中读到我需要将Schema放在UpperCase中并且我有这个但是EF用双引号创建所有表并且在找不到这个表之后。
任何想法或解决方案,谢谢。
答案 0 :(得分:2)
默认情况下,Oracle以大写形式存储表和其他对象名称。可以通过将对象名称括在双引号"
中来覆盖它。由于您可以通过用双引号括起它来访问表,这意味着表是使用混合大小写名称创建的。
您需要用双引号括住任何对象名称,以便在除了全部大写以外的任何情况下存储它们时访问它们。
如果您更改迁移代码以将表格名称存储为大写,那么您将不再需要双引号来访问您的表格。
答案 1 :(得分:0)
ORACLE中的案例和使用引号引起的混乱很容易使您失望。说到ORACLE,只需使用所有大写字母。此示例是使用Oracle.ManagedDataAccess.EntityFramework NuGet包与Oracle的.NET框架身份集成。所有CAPS up脚本:
------------------------------------------------------------- */
-- Add sequences */
-- ---------------------------------------------------------------------- */
CREATE SEQUENCE SQ_ASPNETUSERCLAIMS
START WITH 1
INCREMENT BY 1
MINVALUE 1
NOMAXVALUE
nocycle
noorder
/
-- ---------------------------------------------------------------------- */
-- Add tables */
-- ---------------------------------------------------------------------- */
-- ---------------------------------------------------------------------- */
-- Add table "ASPNETUSERS" */
-- ---------------------------------------------------------------------- */
CREATE TABLE ASPNETUSERS (
ID NVARCHAR2(128) CONSTRAINT NN_ANU_ID NOT NULL,
EMAIL NVARCHAR2(256),
EMAILCONFIRMED NUMBER(1) CONSTRAINT NN_ANU_EMAILCONFIRMED NOT NULL,
PASSWORDHASH NCLOB,
SECURITYSTAMP NCLOB,
PHONENUMBER NCLOB,
PHONENUMBERCONFIRMED NUMBER(1) CONSTRAINT NN_ANU_PHONENUMBERCONFIRMED NOT NULL,
TWOFACTORENABLED NUMBER(1) CONSTRAINT NN_ANU_TWOFACTORENABLED NOT NULL,
LOCKOUTENDDATEUTC DATE,
LOCKOUTENABLED NUMBER(1) CONSTRAINT NN_ANU_LOCKOUTENABLED NOT NULL,
ACCESSFAILEDCOUNT NUMBER(10) CONSTRAINT NN_ANU_ACCESSFAILEDCOUNT NOT NULL,
USERNAME NVARCHAR2(256) CONSTRAINT NN_ANU_USERNAME NOT NULL,
CONSTRAINT PK_ASPNETUSERS PRIMARY KEY (ID)
)
/
-- ---------------------------------------------------------------------- */
-- Add table "ASPNETROLES" */
-- ---------------------------------------------------------------------- */
CREATE TABLE ASPNETROLES (
ID NVARCHAR2(128) CONSTRAINT NN_ANR_ID NOT NULL,
NAME NVARCHAR2(256) CONSTRAINT NN_ANR_NAME NOT NULL,
CONSTRAINT PK_ASPNETROLES PRIMARY KEY (ID)
)
/
-- ---------------------------------------------------------------------- */
-- Add table "ASPNETUSERROLES" */
-- ---------------------------------------------------------------------- */
CREATE TABLE ASPNETUSERROLES (
USERID NVARCHAR2(128) CONSTRAINT NN_ANUR_USERID NOT NULL,
ROLEID NVARCHAR2(128) CONSTRAINT NN_ANUR_ROLEID NOT NULL,
CONSTRAINT PK_ASPNETUSERROLES PRIMARY KEY (USERID, ROLEID)
)
/
-- ---------------------------------------------------------------------- */
-- Add table "ASPNETUSERLOGINS" */
-- ---------------------------------------------------------------------- */
CREATE TABLE ASPNETUSERLOGINS (
LOGINPROVIDER NVARCHAR2(128) CONSTRAINT NN_ANUL_LOGINPROVIDER NOT NULL,
PROVIDERKEY NVARCHAR2(128) CONSTRAINT NN_ANUL_PROVIDERKEY NOT NULL,
USERID NVARCHAR2(128) CONSTRAINT NN_ANUL_USERID NOT NULL,
CONSTRAINT PK_ASPNETUSERLOGINS PRIMARY KEY (LOGINPROVIDER, PROVIDERKEY, USERID)
)
/
-- ---------------------------------------------------------------------- */
-- Add table "ASPNETUSERCLAIMS" */
-- ---------------------------------------------------------------------- */
CREATE TABLE ASPNETUSERCLAIMS (
ID NUMBER(10) CONSTRAINT NN_ANUC_ID NOT NULL,
USERID NVARCHAR2(128) CONSTRAINT NN_ANUC_USERID NOT NULL,
CLAIMTYPE NCLOB,
CLAIMVALUE NCLOB,
CONSTRAINT PK_ASPNETUSERCLAIMS PRIMARY KEY (ID)
)
/
-- ---------------------------------------------------------------------- */
-- Add foreign key constraints */
-- ---------------------------------------------------------------------- */
ALTER TABLE ASPNETUSERLOGINS ADD CONSTRAINT FK_ASPNETUSERLOGINS_USERID
FOREIGN KEY (USERID) REFERENCES ASPNETUSERS (ID) ON DELETE CASCADE
/
ALTER TABLE ASPNETUSERCLAIMS ADD CONSTRAINT FK_ASPNETUSERCLAIMS_USERID
FOREIGN KEY (USERID) REFERENCES ASPNETUSERS (ID) ON DELETE CASCADE
/
ALTER TABLE ASPNETUSERROLES ADD CONSTRAINT FK_ASPNETUSERROLES_USERID
FOREIGN KEY (USERID) REFERENCES ASPNETUSERS (ID) ON DELETE CASCADE
/
ALTER TABLE ASPNETUSERROLES ADD CONSTRAINT FK_ASPNETUSERROLES_ROLEID
FOREIGN KEY (ROLEID) REFERENCES ASPNETROLES (ID) ON DELETE CASCADE
/
-- ---------------------------------------------------------------------- */
-- Add triggers */
-- ---------------------------------------------------------------------- */
CREATE OR REPLACE TRIGGER TR_ASPNETUSERCLAIMS
BEFORE INSERT ON ASPNETUSERCLAIMS
FOR EACH ROW
BEGIN
SELECT SQ_ASPNETUSERCLAIMS.nextval INTO :new.ID FROM dual;
END;
/
IdentityModel.cs中的代码以映射到所有大写字母
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// user id of owner
var ownerSchema = System.Configuration.ConfigurationManager.AppSettings["OwnerSchema"];
modelBuilder.HasDefaultSchema(ownerSchema);
// the identity framework is looking for table and column names that are camel cased
// however, our scripts don't force camel casing (we don't put quotes around our table and column names in the deltas)
// therefore, we need to tell the identity framework to look for the all CAPS versions of the tables and columns
modelBuilder.Entity<IdentityRole>().ToTable("ASPNETROLES", adminSchema);
modelBuilder.Entity<IdentityRole>().Property(p => p.Id).HasColumnName("ID");
modelBuilder.Entity<IdentityRole>().Property(p => p.Name).HasColumnName("NAME");
modelBuilder.Entity<IdentityUserClaim>().ToTable("ASPNETUSERCLAIMS");
modelBuilder.Entity<IdentityUserClaim>().Property(p => p.Id).HasColumnName("ID");
modelBuilder.Entity<IdentityUserClaim>().Property(p => p.UserId).HasColumnName("USERID");
modelBuilder.Entity<IdentityUserClaim>().Property(p => p.ClaimType).HasColumnName("CLAIMTYPE");
modelBuilder.Entity<IdentityUserClaim>().Property(p => p.ClaimValue).HasColumnName("CLAIMVALUE");
modelBuilder.Entity<IdentityUserLogin>().ToTable("ASPNETUSERLOGINS");
modelBuilder.Entity<IdentityUserLogin>().Property(p => p.LoginProvider).HasColumnName("LOGINPROVIDER");
modelBuilder.Entity<IdentityUserLogin>().Property(p => p.ProviderKey).HasColumnName("PROVIDERKEY");
modelBuilder.Entity<IdentityUserLogin>().Property(p => p.UserId).HasColumnName("USERID");
modelBuilder.Entity<IdentityUserRole>().ToTable("ASPNETUSERROLES");
modelBuilder.Entity<IdentityUserRole>().Property(p => p.UserId).HasColumnName("USERID");
modelBuilder.Entity<IdentityUserRole>().Property(p => p.RoleId).HasColumnName("ROLEID");
modelBuilder.Entity<ApplicationUser>().ToTable("ASPNETUSERS");
modelBuilder.Entity<ApplicationUser>().Property(p => p.UserName).HasColumnName("USERNAME");
modelBuilder.Entity<ApplicationUser>().Property(p => p.TwoFactorEnabled).HasColumnName("TWOFACTORENABLED");
modelBuilder.Entity<ApplicationUser>().Property(p => p.SecurityStamp).HasColumnName("SECURITYSTAMP");
modelBuilder.Entity<ApplicationUser>().Property(p => p.PhoneNumberConfirmed).HasColumnName("PHONENUMBERCONFIRMED");
modelBuilder.Entity<ApplicationUser>().Property(p => p.PhoneNumber).HasColumnName("PHONENUMBER");
modelBuilder.Entity<ApplicationUser>().Property(P => P.PasswordHash).HasColumnName("PASSWORDHASH");
modelBuilder.Entity<ApplicationUser>().Property(p => p.LockoutEndDateUtc).HasColumnName("LOCKOUTENDDATEUTC");
modelBuilder.Entity<ApplicationUser>().Property(p => p.LockoutEnabled).HasColumnName("LOCKOUTENABLED");
modelBuilder.Entity<ApplicationUser>().Property(p => p.Id).HasColumnName("ID");
modelBuilder.Entity<ApplicationUser>().Property(p => p.EmailConfirmed).HasColumnName("EMAILCONFIRMED");
modelBuilder.Entity<ApplicationUser>().Property(p => p.Email).HasColumnName("EMAIL");
modelBuilder.Entity<ApplicationUser>().Property(p => p.AccessFailedCount).HasColumnName("ACCESSFAILEDCOUNT");
}
如果您有兴趣,请删除脚本以回滚:
-- ---------------------------------------------------------------------- */
-- Drop triggers */
-- ---------------------------------------------------------------------- */
DROP TRIGGER TR_ASPNETUSERCLAIMS;
-- ---------------------------------------------------------------------- */
-- Drop foreign key constraints */
-- ---------------------------------------------------------------------- */
ALTER TABLE ASPNETUSERLOGINS DROP CONSTRAINT FK_ASPNETUSERLOGINS_USERID
/
ALTER TABLE ASPNETUSERCLAIMS DROP CONSTRAINT FK_ASPNETUSERCLAIMS_USERID
/
ALTER TABLE ASPNETUSERROLES DROP CONSTRAINT FK_ASPNETUSERROLES_USERID
/
ALTER TABLE ASPNETUSERROLES DROP CONSTRAINT FK_ASPNETUSERROLES_ROLEID
/
-- ---------------------------------------------------------------------- */
-- Drop table "ASPNETUSERCLAIMS" */
-- ---------------------------------------------------------------------- */
-- Drop constraints */
ALTER TABLE ASPNETUSERCLAIMS DROP CONSTRAINT NN_ANUC_ID
/
ALTER TABLE ASPNETUSERCLAIMS DROP CONSTRAINT NN_ANUC_USERID
/
ALTER TABLE ASPNETUSERCLAIMS DROP CONSTRAINT PK_ASPNETUSERCLAIMS
/
DROP TABLE ASPNETUSERCLAIMS
/
-- ---------------------------------------------------------------------- */
-- Drop table "ASPNETUSERLOGINS" */
-- ---------------------------------------------------------------------- */
-- Drop constraints */
ALTER TABLE ASPNETUSERLOGINS DROP CONSTRAINT NN_ANUL_LOGINPROVIDER
/
ALTER TABLE ASPNETUSERLOGINS DROP CONSTRAINT NN_ANUL_PROVIDERKEY
/
ALTER TABLE ASPNETUSERLOGINS DROP CONSTRAINT NN_ANUL_USERID
/
ALTER TABLE ASPNETUSERLOGINS DROP CONSTRAINT PK_ASPNETUSERLOGINS
/
DROP TABLE ASPNETUSERLOGINS
/
-- ---------------------------------------------------------------------- */
-- Drop table "ASPNETUSERROLES" */
-- ---------------------------------------------------------------------- */
-- Drop constraints */
ALTER TABLE ASPNETUSERROLES DROP CONSTRAINT NN_ANUR_USERID
/
ALTER TABLE ASPNETUSERROLES DROP CONSTRAINT NN_ANUR_ROLEID
/
ALTER TABLE ASPNETUSERROLES DROP CONSTRAINT PK_ASPNETUSERROLES
/
DROP TABLE ASPNETUSERROLES
/
-- ---------------------------------------------------------------------- */
-- Drop table "ASPNETROLES" */
-- ---------------------------------------------------------------------- */
-- Drop constraints */
ALTER TABLE ASPNETROLES DROP CONSTRAINT NN_ANR_ID
/
ALTER TABLE ASPNETROLES DROP CONSTRAINT NN_ANR_NAME
/
ALTER TABLE ASPNETROLES DROP CONSTRAINT PK_ASPNETROLES
/
DROP TABLE ASPNETROLES
/
-- ---------------------------------------------------------------------- */
-- Drop table "ASPNETUSERS" */
-- ---------------------------------------------------------------------- */
-- Drop constraints */
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_ID
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_EMAILCONFIRMED
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_PHONENUMBERCONFIRMED
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_TWOFACTORENABLED
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_LOCKOUTENABLED
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_ACCESSFAILEDCOUNT
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT NN_ANU_USERNAME
/
ALTER TABLE ASPNETUSERS DROP CONSTRAINT PK_ASPNETUSERS
/
DROP TABLE ASPNETUSERS
/
-- ---------------------------------------------------------------------- */
-- Drop sequences */
-- ---------------------------------------------------------------------- */
DROP SEQUENCE SQ_ASPNETUSERCLAIMS
/