Entity Framework and Membership Provider not working - asp.net-mvc

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.

Related

Why doesn't my application see Entity Framework model updates

I have an MVC application that uses Entity Framework. I needed to create a new stored procedure for the application to use. I created the stored procedure in the database first so I know it exists. Then in my project, I updated the model from the database and pulled in the new stored procedure. Then I created a function import with a complex type and saved the model. I have a service that is using the model and I can reference the new complex type without a problem.
The issue I run into is when running the application and hitting this stored procedure, I get an error that says "Could not find stored procedure."
The method throwing the error is here:
public virtual ObjectResult<E3_Assessment_GetPDFImages_Result> E3_Assessment_GetPDFImages(Nullable<int> assessmentID)
{
var assessmentIDParameter = assessmentID.HasValue ?
new ObjectParameter("assessmentID", assessmentID) :
new ObjectParameter("assessmentID", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<E3_Assessment_GetPDFImages_Result>("E3_Assessment_GetPDFImages", assessmentIDParameter);
}
Are there any additional steps I need to take to make sure the everything is updated in the Entity Framework? I have verified the stored procedure exists in the database. The only other thing I can think of is that the model is not seeing the new stored procedure.
UPDATE Connection String
<add name="AssessmentEntities" connectionString="metadata=res://*/AssessmentModel.csdl|res://*/AssessmentModel.ssdl|res://*/AssessmentModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer;initial catalog=MyDatabase;persist security info=True;user id=MyUser;password=MyPassowrd;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
UPDATE
I ran sql server profiler and found why I'm getting the error. When the stored procedure runs it is running in the master database instead of the actual database it should run in. Other stored procs are running in the correct database.
Why is it running in the master database instead of the database I want?
I had to reinitialize my context before calling the stored procedure. That is why it was using the master database.

Using Specific table/s of a different Database in Entity Framework MVC

I have been searching on different threads but was unable to find an answer which can give me clear picture.
Here is the scenario:
I am creating an application using MVC & Entity framework DB first . I have two databases, DB1 is dedicated DB for my application and DB2 is a huge common Database getting used by many other applications. I will only have to use specific tables/views from DB2.
Now following are the approches I can go with:
Use normal SQL commands for DB2 and populate viewmodels with them. (I only have read-only access on DB2)
Create SQL views in the DB1 and generate EDMX.
Add DB2 in the context of my application.
I dont want to go with Approach#3 as I mentioned DB2 is huge and I only need to use couple of tables from DB2.
Also, Just in case I want to go with code first approach for DB1 what is the best solution in that case.
I often have projects with three of more models because we have many databases.
I would create a two EDMX set-up with two distinct contexts. Create your application DB1 Context as normal then create an additional context and only pull in those tables that you are interested in from DB2.
To make your life easier in the long run and easier to maintain generally just create a DLL for each model so that it has its own namespace and that way you can distinguish between users in DB1 and users in DB2 for instance and add or remove entities from one without affecting the other.
Each DLL would have an app.config connection string that gets yo to your data, such as
<add name="DB1Entities" connectionString="metadata=res://*/DB1Model.csdl|res://*/DB1Model.ssdl|res://*/DB1Model.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=ClientDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="DB2Entities" connectionString="metadata=res://*/DB2Model.csdl|res://*/DB2Model.ssdl|res://*/DB2Model.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=ClientMaster;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
<add name="DefaultConnection" connectionString="Data Source=(local);Initial Catalog=Reports;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>
Just remember to copy each of the connection string from the Dll's App.config into your applications app.config or you web.config file for a site.
In your project reference the DLL's and then load your contexts.
DB1Entities DB1Context = new DB1Entities()
DB2Entities DB2Context = new DB2Entities()
You can now happily distinguish between DB1 and DB2 entities and use content from one in the other like this.
var address1 = DB1Context.Addresses.Single(a => a.AddressId == 1);
var address2 = DB2Context.Addresses.Single(a => a.Id == address1.GlobalAddressId);

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.

Entity Framework 4 and DB2: database generation error

We are using Entity Framework 4 RC on Visual Studio 2010 with DB2 version 9.7.3.4. I also have the VS Add-ins and can see the DB2 database in Server Explorer. I have created a very simple VS console project and it works fine against SQL Server, so I know it is valid. I have references to "IBM.Data.DB2.9.7.3" and "IBM.Data.DB2.Entity" in my project.
In app.config my connnection string is:
<add name="ProductContext"
providerName="IBM.Data.DB2"
connectionString="Database=DB2TEST;User ID=XXXX;PWD=XXXX;Server=XXXX;Persist Security Info=True;"/>
The first statement in my code is a database initializer:
DbDatabase.SetInitializer<ProductContext>(new DropCreateDatabaseIfModelChanges<ProductContext>());
During run-time when I reach a line that causes a change to the data context I get the error:
Model compatibility cannot be checked
because the database does not contain
model metadata.
Since I requested that the database be dropped, this does not seem to be a logical error. Does anyone know what the cause could be?
I would try to inherit from CreateDatabaseIfNotExists first, which will add the EdmMetadata table to the schema. I believe the error is that EF is saying that it cannot find that table.
So
DbDatabase.SetInitializer<ProductContext>(new CreateDatabaseIfNotExists<ProductContext>());
Run it once, then change to DropCreateDatabaseIfModelChanges once the EdmMetadata table exists.
Try removing the IncludeMetadataConvention like this:modelBuilder.Conventions.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
To avoid the "dbo" issue, just map all your entities using either DataAnnotation attributes or Fluent mapping:
[Table("Product", SchemaName = "MySchema")]
public class Category { //DataAnnotoaion approach
modelBuilder.Entity<Category>().ToTable("Categories", "MySchema"); //Fluent approach

EntityFramework Single connection string

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

Resources