Recently I was working on an asp.net mvc 4 application and when reviewing the application I noticed I had 2 connection strings that pointed to the same database.
ApplicationServices - For membership related stuff
MyDbEntities - For entitry framework related stuff
I know you can make the entity framework context point to the applicationservices connectionstring like below I guess
public MyDbContext() : base("name=NameOfYourConnectionString") // Name of your connection string
{ }
Is there any performance difference between having one connection string vs defininf multiple connection strings? Or is ASP.NET smart enough to know the database is the same and will share connection when need be?
Regards DotnetShadow
Is there any performance difference between having one connection
string vs defininf multiple connection strings?
The ADO.NET connection pool is per connection string. There will be a difference only if the connection strings have some differences. In this case you will have 2 different connection pools instead of reusing connections from the same pool which would have been better since you are hitting the same database. But if the 2 connection strings are strictly identical, there will be no difference.
Related
In Grails I could create Sql object in 2 ways:
def sql = new Sql(sessionFactory.currentSession.connection())
def sql = new Sql(dataSource)
I have read this thread here on Stackoverflow: Getting the SessionFactory for a particular Datasource in Grails
... and one of the answers was that dataSource "...gobbles up excessive connections better to use sessionFactory.currentSession.connection()"
Is this recommendation correct and what is the difference between those two?
When Inspecting created objects I could see that they are almost the same, just 2 properties were different:
dataSource and useConnection.
In case of dataSource it was dataSource=TransactionAwareDataSourceProxy and useConnection=null, while for sessionFactory it is dataSource=null and useConnection=$Proxy 36.
Why this makes difference with consequence of "gobblin up excessive connections"?
The comment about "gobbling up excessive connections" is based on a few assumptions, which may or may not be true in your case.
The assumption is that Hibernate is going to, or already has, or will, during the request create a connection to the database because of the session in view pattern used by Grails and GORM. In that case you would be using one connection for Hibernate and n-number for your other connections.
If you use a mix of GORM and SQL connections it's safer to get the connection from the sessionFactory.
I seem to recall older versions of Grails use to create the connection even if no GORM methods were executed during the request. I'm not entirely sure that's still the case with more recent versions (2.x+)
I had an application which used the Asp.net Membership provider. I converted this project to EF4 and trying to use the same authentication.
I am using the old DB for my connection as
<add name="Entities1"
connectionString="metadata=res://*/DbContext.csdl|res://*/DbContext.ssdl|res://*/DbContext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Master.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="PondsMasterEntities"
connectionString="metadata=res://*/DbContext.csdl|res://*/DbContext.ssdl|res://*/DbContext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\PondsMaster.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
And now I am ending with this error.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist
on
var result = await UserManager.CreateAsync(user, model.Password);
But when I use like this. I can add delete entries from the DB.
private Entities1 db = new Entities1();
db.IssueMasterLogs.Add(log);
Any thoughts?
Looks like you're using a database first approach and an existing database for a project that uses ASP.Net Identity. By default, Identity (V2) works with a code first approach, creating it's required tables in the database. Usually, people end up having a separate connection string for Identity because of this (even though both connection strings point to the same database).
In your case, simply switching the DefaultConnection Identity uses to your connection Entities1 will not help since it is not code first. You can however, keep the DefaultConnection and modify it to point to your db, and keep using it code first. If you do this, you will have two connection strings (Entities1 and DefaultConnection) that both point to the same database and are database first and code first, respectively.
For a db first a approach with Identity, you might want to take a look at this.
Is there a way to use Entity Framework 6 with DbUp without having 2 connection strings?
The problem is EF uses a conn string with a bunch of meta data for locating the model that DbUp doesn't like - it expects a simple conn string.
And EF doesn't run without all the meta data.
I could have 2 different connection strings but that seems just wrong.
You can use Code Based Modelling (Code "First") - that only uses the standard ADO.NET connection string
I have an Entity-class having a Property of type Int32: on generating DDL using DevArt for ORACLE a NUMBER(10) column is generated. Reading and writing instances works flawlessly.
However, on fetching instances of this Entity-class sending a custom query to ExecuteStoreQuery on the ObjectContext this Property seems to be returned as System.Double, as such constructing the instances fails.
Can I hint DevArt to construct System.Int32?
Thank you.
Bart
The reason is the fact that OracleDataReader, which is used in the ExecuteStoreQuery method, has type mapping different from the one used in the Entity Framework provider.
I recommend you to use NumberMappings, I suppose you will need to map Number(10) to Int32: Number Mappings=((NUMBER,10,10,System.Int32). These changes should be persisted to the model connection string (they are duplicating the default EF mapping rules, it is necessary for the OracleDataReader from ExecuteStoreQuery).
Please let us know if the problem persists.
I m beginner with EF,and my question is is there any way to use one connection string with multiple models.Because my application could have 50 models and it would be funny to change conn string in config 50 times.
Thank you...
No. An EntityConnection provides 2 sets of information: Provider connection string which is basically the database connection string and is equal across all your models (albeit if you are accessing the same database on all of them) and Metadata Information which points to the Conceptual Schema Definition Layer (CSDL), Store Schema Definition Layer (SSDL), and Mapping Schema Layer (MSL) files, and tells the context where to find these files which is NOT equal across your models:
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
provider connection string="Data Source=.;...."
The *only* way that you can have one connection string in your solution is to NOT have EDM files at all: **[Entity Framework Code First Development][1]**