Why does an Entity Framework Connection require a metadata property? - database

I switched my DAL from using LINQ over to Entity Framework. Because my application connects to different databases depending on the current user, I need to dynamically create the DataContext at run time and pass in the appropriate connection string. However, when I tried to programatically create an Entity Framework connection using my old connection string, the connection failed. It complained that it didn't recognize the key in the connection string, "server" to be exact.
I found out that I needed to do this in order to get the Entity Framework connection to work:
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = clientConnectionString;
entityBuilder.Metadata = "res://*/xxxxxxxxxx.csdl...";
Entities entities = new Entities(entityBuilder.ToString());
Why is this?
What is the Metadata property for?
Is it going to be a problem that its always the same for multiple different connections?
What should it be?
Is there any way around this?
Thanks in advance!
Update 1:
Thanks for the update Randolpho, but...
The whole reason I'm having this issue, is that I can't store the connection strings in a configuration file. The connection string is dynamically determined at runtime by which user is connecting.
Here is my exact scenario:
If user A is connecting, the app pulls data from database A. If user B is connecting, the app pulls data from database B.
The connection strings are stored in a main database, and the number is potentially limitless. Every time I add a user, I don't want to have to go into the web.config, not to mention the fact that it would eventually get HUGE!

Expanding on Randolpho's answer:
The metadata property specifically points to the location of the .SSDL (Storage Model,) .CSDL (Conceptual Model,) and .MSL (Mapping Model) files. These three files essentially are the Entity Data Model. The "res://" URI-style qualifier indicates that the files are embedded as resources in the compiled EDM assembly.

You will find these links very informative:
http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.aspx
http://weblogs.asp.net/pgielens/archive/2006/08/21/ADO.NET-Entity-Framework-Metadata.aspx
Bottom line? Entity Framework needs the metadata to build your entity mappings.
Additionally, you should consider moving your connection information out to your configuration file rather than build it in code. The first link will show you how to do that.

Related

EF Core 5.0 DB first approach to access DB for read purpose

I am developing an application with Asp.Net core 5 and the application accesses and displays information from two different databases(both Sql). One database is application’s own database where all the information will be stored/added. But there is another database already exists(on-prem server) and being used by another application. I want to read some master data from this database to use in my application. So I want to connect to the second database to just read master data and display it in application pages. For the first database connectivity I have created a separate entity project using Entity Framework Core 5.0 Code first approach.
How do I access second database just for read data purpose, Which would be the feasible approach for this. I was thinking to create another entity project with EF Core 5 DB first approach, but with this approach it creates DBContext class and all DbSets objects for each table. Which is not required I feel because I just want to read 8-10 tables from the entire database
Can anyone please suggest which would be a better approach for this? DB first approach or Ado.Net Vanilla method?
Finally I am going to reference these entity projects into my Web API application for all DB operations.
Thanks!
Within the same project, next to your existing dbContext, register a new extension of db context that will encapsulate the second database that you want to read from.
Do not copy the entire project from the start, rather think that the only reason to have a second dbContext to start with, is when dealing with multiple databases.
The rest of your code can remain re-usable ( probably ) and you dont need to cope with an other project as well
EDIT:
For example you might register your current context like below:
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
You can create a new class, lets say
public class OldDataDbContext : DbContext
{
public OldDataDbContext(DbContextOptions<OldDataDbContext> options)
: base(options)....
....
....
on that new dbContext class, register all the dbSets that you are going to read from that master db.
Then register that "new implementation of db context" with the connection string for the other database.
services.AddDbContext<OldDataDbContext>(
options => options.UseSqlServer("name=ConnectionStrings:OldDatabaseConnection"));
Now next to your DefaultConnectionString appSetting ( or however else you might have called it, add an other line with the other connectionString, with keyName "OldDatabaseConnection" and you can use that new class just as you use the old one.

export image from lotus domino database

I'm a beginner with Lotus so I really need help about it.
my company gave me a database in lotus. This database include around 18,000 images which I must import in a new DB developed in MySQL.
When I tried to open it with notes designer/notes the fallowing message is showed:
You have insufficient access to perform this operation, I think there is a password that I
do not know.
for privacy reasons I can not post nothing about the db, sorry.
So, how can I export all images as easy way as possible?
First, you need to ensure you have sufficient access to the database. This means following:
If database resides on a server, ensure you are allowed to access the server and have Reader access to the database (you may need higher rights, see below for details);
If database resides on your workstation, you must ensure it's not locally encrypted by someone else or doesn't have Enforce ACL enabled;
With above in mind, exporting images from DB may require different approach depending on their location:
Are they stored as design elements (Image Resource), then all you have to do is to open the database in Domino Designer, go to Image Resources, select desired image resource and perform Resource -> Export;
Are they stored in documents, then this becomes a more complex task. You may use LotusScript or Java to iterate and process all documents containing images. For this, you would NotesRichTextItem class to retrieve NotesEmbedded object from each document. Alternative way would be to convert all rich-text field in document to MIME format using Call notesDocument.ConvertToMIME( conversionType, options) and process it after with NotesMIMEEntity class;
To access design of a database located on a Domino server you will need Designer rights.
Hopefully this will help.

How to read data from SQLite database on Eclipse

I'm using a static database that I created with SQLite Database Browser. I put it in my assets folder and built a code to copy the database to a database variable (Does that make sense?) so I could read information from it. Problem is I don't know how - mostly the SQL queries involved - and what are your suggested methods do to that? In other words, what methods should I add to my Database Handler class (Or data adapter?) in order to present the data in a list view, for example.
Thank you for all your help.
Read the Android database documentation.
Copying your database from the assets folder is typically done in the onCreate and/or onUpgrade functions of your SQLiteOpenHelper-derived class.
This tutorial covers the basics:
Using the SQLite Database with ListView
As for naming things: use whatever names make sense in your application.

Can I use the connection from Entity Framework or should I create a new one?

I have a SQLite Database that I access using System.Data.SQLite. The database is read into my Entity Framework Objectcontext. However, I also need direct access to the database in certain cases.
Can I / Should I use the same connection that my Entity Framework object context is using? I.e. ObjectContext.Connection?
If yes, how can I access it? Casting Entities.Connection to SQLiteConnection results in "Unable to cast object of type 'System.Data.EntityClient.EntityConnection' to type 'System.Data.SQLite.SQLiteConnection'."
If not, should I create a seperate connection, open it and keep it open for the application duration, or should I open and close the connection every time I want to access the database with it?
ObjectContext.Connection is an EntityConnection. You can get the store connection via:
var sc = ((EntityConnection)ObjectContext.Connection).StoreConnection;
You can cast that to the SQLiteConnection.
It's OK to use this connection if you need it.
But, as #Chris mentioned, you can just use ObjectContext.ExecuteStoreQuery, etc., if that will do for you.
If you're using the DBContext API you can actually execute a direct query by using this: aDBContext.Database.SqlQuery
I wouldn't be surprised if something similar exists for the ObjectContext API, but I barely used that one and thus can't tell you what it is.
Now if for some reason you can't (or don't want) to do that, I'd create a new connection for your custom queries. Put them in a Using and close it as soon as you're done with it.
Entity Framework has built in methods for executing raw SQL queries if that's what you need to do.

Site update, testing was fine, after deployment, again fine, once user load increases, FAIL?

We are using ASP.NET MVC with LINQ to SQL. We added some features and tested them all to perfection on our QA box. We are using Windows Server 2003 and SQL Server 2005. So when we pushed out changes to the Live web server we also used Red Gate SQL Compare to push new database changes to the LIVE database. We tested again between the few of us, no problems. Time for bed.
The morning comes and users are starting to hit the app, and BOOM. We have no idea why this would happen as we have not been doing any new types of code things that we were not doing before. However we did notice that during the SQL Compare sync the names of all the foreign keys were different between the two databases, not the IDs in the tables, FK_AssetAsset_A0EB67 to FK_AssetAsset_B67EF8 (for example, don't remember the exact number of trailing mixed characters during the SQL Compare), we are not sure why but that is another variable in this problem.
Strangely once this was all pushed out we could then replicate the errors on QA, but not before everything was pushed to LIVE.
QA and LIVE databases are on the same SQL Server, but the apps are on different instances of Windows Server 2003.
Errors generated:
Index was outside the bounds of the array.
Invalid attempt to call FieldCount when reader is closed.
Server failed to resume the transaction.
There is already an open DataReader associated with this Command which must be closed first.
A transport-level error has occurred when sending the request to the server.
A transport-level error has occurred when receiving results from the server.
Invalid attempt to call Read when reader is closed.
Invalid attempt to call MetaData when reader is closed.
Count must be positive and count must refer to a location within the string/array/collection. Parameter name: count
ExecuteReader requires an open and available Connection. The connection's current state is connecting.
Any one have any idea what the heck could have happened?
EDIT: Since we were able to replicate the errors all of a sudden on QA, it might not be a user load issue... Needless to say we all feel really screwed here.
Concurrency always brings bugs out of the woodwork. I'd recommend you check for objects that could be shared among requests (such as static members and singletons) and refactor your code so that as little as possible is shared.
As far as specifics go, for the error "There is already an open DataReader associated with this Command which must be closed first," you may want to try adding MultipleActiveResultSets=True to your connection strings.
It sounds like you're crossing the streams a bit and trying to share DataContexts across requests. My suggestion would be to wire in a dependancy injection framework that creates a new instance of the dependancy for each request.
I use Castle's IoC and wire it into the controller factory so that when it sees a dependancy on a repository it creates a new instance of that repository for each request. If you go this route let me know and I can shoot you a few more resources.

Resources