I'm using the Official Oracle SQL and Entity Framework driver to read the database. But when reading the database it prefixes the table name with "dbo".:
SELECT
*
FROM "dbo"."Woningen"
Without the "dbo". prefix the code works fine, with, it causes the error "table or view does not exist". This is probably because the user isn't 'dbo', so it does not have access to that schema. This is the Entity Framework code that I'm using:
[Table("Woningen")]
public class Woningen
I've tried updating the Oracle nuget package but then it comes up with the error "Connection string is not well-formed". So it probably has the same error as before, it just failed sooner. This is the connectionString format I used:
<add name="DefaultConnection"
providerName="Oracle.ManagedDataAccess.Client"
connectionString="USER ID=testUser;PASSWORD=password;
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=serverUrl)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=Database)));"/>
I can see three possible solutions to this problem, but have no idea how to implement them:
Manipulate Entity Framework to exclude schema names from queries
Give my user access to the schema
Update the driver and fix the connectionstring format, if anyone knows how the format changed..
Note that current code already works in production so the current version should be fine. The database and it's user are new, so the problem could be with how they are created.
You can specify the schema that Entity Framework uses. See below
If you are using Entity Framework 6+ you can use the following
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set a default schema for ALL tables
modelBuilder.HasDefaultSchema("YourSchemaName");
}
}
If you wish to set a schema on a specific table...
[Table("Woningen"), Schema = "YourSchemaName")]
public class Woningen { }
If you are using EF5
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Unfortunately you have to specify each table you want to set a schema for...
modelBuilder.Entity<Woningen>().ToTable("Woningen", "YourSchemaName");
}
}
Related
The following used to work. I am unsure what has changed.
var db = new MyDbContext()
var compatible = db.Database.CompatibleWithModel(true)
gives the following error
Model compatibility cannot be checked because the database does not
contain model metadata. Model compatibility can only be checked for
databases created using Code First or Code First Migrations.
I have been able to create and run the migrations using Package Manager and I can see the code in the Migrations folder.
I can see the migrations in the __MigrationHistory table.
I resolved the issue in a different database when the user did not have access to the table. However in this case the user does have access.
The table shows the product version is 6.4.4
I tried creating a new migration to see if there were any differences, but it was empty.
I looked at the configuration.cs in the migrations folder
internal sealed class Configuration : DbMigrationsConfiguration<MigrationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
}
Then I corrected the type
var db = new MigrationDbContext()
Then I changed the
So I have set up a SQL Server and EF in my project.
MPSDBEntities mpsEntities = new MPSDBEntities();
And it works fine when I do a save changes. e.g
mpsEntities.SaveChanges();
it updates the database (does it mean my connection string is correct?)
However, whenever I tried to perform any kind of load data/SQL from the EF, such as
var temp = mpsEntities.CARD_BY_CHECKTYPE.Where(x => (x.CHECK_TYPE == "AA2")).ToList();
It would throw an exception of
Unable to Load the specified metadata resource.
Here is my connection string, which I doubt is where the problem is at:
<add name="MPSDBEntities"
connectionString="metadata=res://*/MPSDBModel.csdl|res://*/MPSDBModel.ssdl|res://*/MPSDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=PAE0DT-DDWB282\MPS2;initial catalog=MPS;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
I went through the following posts. They didn't solve my problem.
System.Data.MetadataException: Unable to load the specified metadata resource
MetadataException: Unable to load the specified metadata resource
Entity Framework: Unable to load the specified metadata resource
Unable to load the specified metadata resource
Anyone know what the problem is?
Thank you.
#petryuno1
Here is my DbContext, if this is what you are asking..
public partial class MPSDBEntities : DbContext
{
public MPSDBEntities() : base("name=MPSDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<CARD_BY_CHECKTYPE> CARD_BY_CHECKTYPE { get; set; }
........
}
This worked for me.
Changing from this:
connectionString="metadata=res://*/Model.Project.csdl|res://*/Model.Project.ssdl|res://*/Model.Project.msl;
change to:
connectionString="metadata=res://*/;
And add the rest of your connection string after. Hope it helps.
I already had a database first ASP MVC project. Now I remove .edmx file and and all every thing related to database first and make it code first. When I want to Enable-Migrations I get this error in powershell. :
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
Here is my Connection string in webconfig:
<connectionStrings>
<add name="Context" connectionString="Data Source=.\;AttachDbFilename=|DataDirectory|\myDbName.mdf;Initial Catalog=myDbName;Integrated Security=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
And Here is my Context :
public class Context : DbContext
{
public Context()
: base("name=Context")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}
How can I resolve this problem?
This question already has answers here:
MetadataException: Unable to load the specified metadata resource
(47 answers)
Closed 6 years ago.
Yes I did read and try entity framework Unable to load the specified metadata resource
I typically use code first and have had no issues. However I needed to troubleshoot a project with EDMX
Context:
public partial class x500Entities : DbContext
{
public x500Entities()
: base("name=x500Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<WorkerPublicExtended> WorkerPublicExtendeds { get; set; }
}
connection string :
<add name="x500Entities"
connectionString="metadata=res://*/CDISWorkerPublicExtended.csdl|res://*/CDISWorkerPublicExtended.ssdl|res://*/CDISWorkerPublicExtended.msl;provider=System.Data.SqlClient;provider connection string="data source=xserver;initial catalog=x500;persist security info=True;user id=xuser;password=xpassword;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
ERROR :
An exception of type 'System.Data.Entity.Core.MetadataException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Unable to load the specified metadata resource.
Upon hitting this line:
return context.WorkerPublicExtendeds.FirstOrDefault(x => x.upperIDSID == idsid.ToUpper().Trim());
Complete Method :
public WorkerPublicExtended GetEmployee(string idsid)
{
using (x500Entities context = new x500Entities())
{
return context.WorkerPublicExtendeds.FirstOrDefault(x => x.upperIDSID == idsid.ToUpper().Trim());
}
}
Why is this happening?
I connected to sql server ssms and I don't see the table WorkerPublicExtended that I have seen in diagram edmx and the model, I don't see where that name is translated to a real table name. How is this?
Probable causes:
1) your resource file is nowhere to be found in you project (this resource file is configured in you app.config in connection string) confirm oyu have it.
2) when you say you connect to the database and you don't see the Table then you're in the wrong database? that should never happen, you have an EDMX generated for another database, or someone droped the table and didn't regenerate the edmx ? :X
If you have 200% sure that the database you are connecting it's the latest in schema, procedures, views, etc, or it's the production database, why not to delete the entire edmx and recreate, test and redeploy?
The three parts of the EMDX look correct in the connection string. After double-checking this part to be correct (refer to this guideline to troubleshoot your issue):
You might have changed the MetadataArtifactProcessing property of the model to Copy to Output Directory, or
You changed other things (like the name of an assembly), or
You might be using a post-compile task to embed EMDX which is no longer working.
Source from here.
Also check the entire string for correct syntax.
I'm using EF6.0 and implementing my db with SQLServerDatabaseProject.
I want to use the EF Migration tools for Database migration. but since I have my database on DbProject I want all my migration files to be SQLFiles (not c#)
So I would like to know if EF supports this feature and if not, is it possible to write a new Migration class which keeps the EF features but works this way?
Please also consider that I don't want EF to generate my migrations but I would like to be able to use other migration commands such as update-database and ...
==MORE DETAILS ABOUT THE QUESTION==
I don't want to have c# classes load my sql files. The sql files must be saved for up and down migrations directly and be treated exactly as if they are the DbMigration classes.
A simple example of Migrations dir would be something like this:
Migrations
-> up
-> 201510060807125_alter-course-change-family.sql
-> 201510060813136_alter-course-add-mark-column.sql
-> down
-> 201510060807125_alter-course-change-family.sql
-> 201510060813136_alter-course-add-mark-column.sql
Simply in the migration class use SqlFile extension method:
public partial class MyFancyMigration : DbMigration
{
public override void Up()
{
SqlFile("myUpSQLFile.sql");
}
public override void Down()
{
SqlFile("myDownSQLFile.sql");
}
}
Although you can use SqlFile method, I suggest you to write this on Seed method of your Configuration.cs file in your migration folder.
internal sealed class Configuration : DbMigrationConfiguration<YourDbContext>
{
protected override void Seed(YourDbContext context)
{
base.Seed(context);
context.Database.ExecuteSqlCommand(FileReadAllText("migration.sql"));
}
}
If you write migrations in Up method. It will be executed per each migration when you update the database, which I think you don't expect.
What you want is that your scripts run per each Update-Database. (Not per each migration in it)