EntityFramework Single connection string - entity-framework-4

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]**

Related

How to get Processed string from string in Core Data

In my application I have used core data for data persistent.
I have one entity "Networks"
Networks entity have one attribute ip address.
Many times ip address is not in well format . Can I transform one raw ip string to other well form string i.e can I process core data entity attribute value using heavy core data migration like custom Entity Migration Policies .
When to go for Custom Entity Migration Policies
When is a migration necessary? The easiest answer to this common question is “when you need to make changes to the data model.”
You need to opt for Custom Entity Migration only when you have changed the entity type of your attribute.
You can get more information about this topic in here : https://www.raywenderlich.com/174025/lightweight-migrations-in-core-data-tutorial

Multiple connection strings to the same database performance?

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.

Problems with DbContext in Class Project

Background:
I have a Web API project and a Class Library project in the same solution that share the same Model classes. Both projects share the same database and both use DbContext to read/write data.
The Web API project is set up in the typical UnitOfWork pattern and works just fine.
The class project is a bit different. I specify the connection string in the constructor instead of the Web.config file:
class MyContext : DbContext
{
public MyContext(string connectionString)
: base()
{
// Do this to force me to use eager loading
this.Configuration.LazyLoadingEnabled = false;
// Init connection string
this.Database.Connection.ConnectionString = connectionString;
Database.SetInitializer <CFCalcContext> (null);
}
... DbSets ...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
... modelBuilder details ...
}
}
I use the Web API to call a function defined in the class library. When I attempt to query the database using the class library I get an error.
The Problem:
In the class library project I get a EntityCommandCompilationException with the inner exception as:
InnerException: System.Data.MappingException
HResult=-2146232032
Message= (82,10) : error 3034: Problem in mapping fragments starting at lines 82, 90:Two entities with different keys are mapped
to the same row. Ensure these two mapping fragments do not map two
groups of entities with different keys to the same group of rows.
(82,10) : error 3034: Problem in mapping fragments starting at lines
82, 98:Two entities with different keys are mapped to the same row.
Ensure these two mapping fragments do not map two groups of entities
with different keys to the same group of rows.
(82,10) : error 3034: Problem in mapping fragments starting at lines
82, 106:Two rows with different primary keys are mapped to the same
entity. Ensure these two mapping fragments do not map two groups of
entities with identical keys to two overlapping groups of rows.
(82,10) : error 3034: Problem in mapping fragments starting at lines
82, 112:Two entities with different keys are mapped to the same row.
Ensure these two mapping fragments do not map two groups of entities
with different keys to two overlapping groups of rows.
(82,10) : error 3034: Problem in mapping fragments starting at lines
82, 118:Two entities with different keys are mapped to the same row.
Ensure these two mapping fragments do not map two groups of entities
with different keys to two overlapping groups of rows.
(90,10) : error 3034: Problem in mapping fragments starting at lines
90, 98:Two entities with different keys are mapped to the same row.
Ensure these two mapping fragments do not map two groups of entities
with different keys to the same group of rows.
Also, when I right click the derived DbContext class (in the class library) and use Entity Framework Power Tools to "View Entity Data Model" I get the error: "A constructible type deriving from DbContext could not be found in the selected file." This works just fine in the Web API project.
Why would two DbContexts that connect to the same database and are configured basically the same perform differently? I must be missing a configuration step, but I don't know which.
Thanks for the help!
A lot going on here. Firstly, if you're manually setting the connection string you should be setting it in the call to the base constructor:
public MyContext(string connectionString)
: base(connectionString)
Secondly though, you should really be putting that in Web.Config - if the name is the issue, you can give it a different name like:
public MyContext()
: base("Whatever name I used in Web.Config")
Third this is probably just wrong:
Database.SetInitializer <CFCalcContext> (null);
The documentation for Database.SetInitializer states:
Gets or sets the database initialization strategy. The database initialization strategy is called when DbContext instance is initialized from a DbCompiledModel.
In other words setting it to null is likely to make your DbContext explode as soon as it tries to do anything - I suspect that's what's happening to you here.

Specify Connection string for Entity Framework used from multiple projects

I am currently using EntityFramework 4 with POCO objects. The POCO objects are located in their own .net project (project.Models). The Context is located in the DAL project (project.DAL). I have multiple other projects that I wish to use the context/models, for Example:
project.Website
project.Webservice
project.ConsoleApplication
Question:
How do I set the Context's connection string myself?
I have noticed that the Context object automatically finds the connection string in the web.config of the website if I add it there. Do I need to do something similiar for all the other projects? This seems inelegant and i think i'd rather have a way to manually configure it from my own config file or something.
Thanks!
AFrieze
You can pass a connection string as the first argument to the ObjectContext constructor. Read it from wherever you like and pass it explicitly, if that's what you want.

Entity Framework, Oracle, DevArt, Context#ExecuteStoreQuery: System.Int32 constructed as System.Double?

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.

Resources