I have the following connection string:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="MyContext" connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string='data source=SQLSERVERDB;initial catalog=TestDB_CodeFirst;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
When I try to enable migrations I first get a warning:
Cannot determine a valid start-up project. Using project 'MyApp.Model' instead.
Your configuration file and working directory may not be set as expected.
Use the -StartUpProjectName parameter to set one explicitly.
Then I get this exception:
Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied.
Is the connection string wrong and why should I need ssdl if I'm using Code First?
NOTE
My context is in MyApp.Model project where my Migrations folder should be located.
I don't have connection strings in my main startup project because connection strings are retrieved from a second database and the user can select one of them when logging in to the application.
I have just one connection string shown above in my MyApp.Model project which points to my development database.
Also, my second question is:
If I use CF migrations, will all databases be migrated each time a user selects a different database for the first time?
EDIT
I changed the connection as mentioned below, and I get the following exception:
The item with identity 'table1' already exists in the metadata collection.
Parameter name: item
It must be noted that I reverse-engineered an existing database. So I don't know what possibly went wrong!
I've also deleted the Migrations folder and checked the database but there is no migration_history table created.
You are trying to use a connectionString designed to work with Database First / Model First. You can tell because your providerName is System.Data.EntityClient instead of System.Data.SqlClient.
Your connection string should look like this:
<connectionStrings>
<add name="MyContext"
connectionString="Data Source=SQLSERVERDB; Initial Catalog=TestDB_CodeFirst;user id=***;password=***;"
providerName="System.Data.SqlClient" />
</connectionStrings
Although I would suggest using Integrated Security instead of a user/password. Just personal preference, though.
Related
I've a Web API project which uses EF 6.0 for database operations. I have 3 different Azure SQL databases (Dev, Test, Prod).
I have been able to create an Entity Data Model with data first approach.
I've used configuration manager of VS2017 to create a web.test config file, but transformation of connection strings isn't working.
Currently my web.config file has the connection string that points to Dev environment as follows:
<connectionStrings>
<add name="ProjectDbEntity"
connectionString="metadata=res://*/Data.ProjectEntityModel.csdl|res://*/Data.ProjectEntityModel.ssdl|res://*/Data.ProjectEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=company-ai-Projectserver.database.windows.net;initial catalog=company.DB_Dev;persist security info=True;user id=Project;password=Password1234$$;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
And, web.test.config is as follows:
<connectionStrings>
<add name="ProjectDbEntity"
connectionString="metadata=res://*/Data.ProjectEntityModel.csdl|res://*/Data.ProjectEntityModel.ssdl|res://*/Data.ProjectEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=company-ai-Projectserver.database.windows.net;initial catalog=company.DB_Test;persist security info=True;user id=Project;password=Project1234$$;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"
xdt:Transform="SetAttributes"
xdt:Locator="Match(name)"/>
</connectionStrings>
I'm not able to figure out how do I make EF change connection string, when I'm using it to build a EF model using one instance of database, but during deployment I want to use other database instances with the same schema.
I've been reading up on the following post, but somehow not able to make it work.
EDIT: I found the solution. All I had to do is select Publish option, then Settings and again select Settings on the popup, and finally select the configuration from the drop down option to the desired configuration. It seems selecting the configuration on the main menu does not publishes the same environment unless explicitly selected.
I found the solution. All I had to do is select Publish option, then Settings and again select Settings on the popup, and finally select the configuration from the drop down option to the desired configuration. It seems selecting the configuration on the main menu does not publishes the same environment unless explicitly selected
I am trying to learn mvc.net. I created a small project in which I used Model first approach. The problem is I wanted my database to be shown in App_data folder for that I followed this article:
http://www.dotnetfunda.com/articles/show/2182/how-to-embed-sql-database-in-appdata-folder
In short I detached the database and then I include the database in app_data folder. Now I am facing problem in changing the connection string, Previously I generated connection string automatically using entity framework, It was like:
<add name="KeepitrememberEntities"
connectionString="metadata=res://*/EDM.csdl|res://*/EDM.ssdl|res://*/EDM.msl;
provider=System.Data.SqlClient;provider connection string="
data source=mypcname\SQLEXPRESS;initial catalog=Keepitremember;
integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
and now I changed it to:
<add name="KeepitrememberEntities"
connectionString="Data Source=mypcname\SQLEXPRESS;
AttachDbFilename=|DataDirectory|Keepitremember.mdf;Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />
When I am executing the code and trying to save the value using a form, it's showing me the error in EDM.Context.cs and error is:
An exception of type
'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException'
occurred in Emptymvc.dll but was not handled in user code
Additional information: Code generated using the T4 templates for Database First
and Model First development may not work correctly if used in Code
First mode. To continue using Database First or Model First ensure
that the Entity Framework connection string is specified in the config
file of executing application. To use these classes, that were
generated from Database First or Model First, with Code First add any
additional configuration using attributes or the DbModelBuilder API
and then remove the code that throws this exception.
What I need to do now, any solution!!
Thanks for your time.
As far as I see, the first connection string is Entity Framework connection string, and the second one is SQL Express connection string. In an EF project, both of them should be exist in web.config file for DbContext and code generation template usage - hence they're not interchangeable (i.e. you should not change EF connection string into SQL Express one and vice versa).
Since you have changed EF connection string contained CSDL, SSDL & MSL information required by EF into SQL Express one, EF assumes the existing database metadata isn't exist anymore and trying to create a new database like Code First has, hence it triggers UnintentionalCodeFirstException after executing OnModelCreating method.
Instead of changing EF connection string in web.config, just add SQL Express connection string on the same connectionStrings element as this (you may need to define initial catalog name when required):
<connectionStrings>
<!-- SQL Express connection string -->
<add name="KeepitrememberConnection"
connectionString="Data Source=mypcname\SQLEXPRESS;Initial Catalog=Keepitremember;
AttachDbFilename=|DataDirectory|Keepitremember.mdf;Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />
<!-- EF connection string -->
<add name="KeepitrememberEntities"
connectionString="metadata=res://*/EDM.csdl|res://*/EDM.ssdl|res://*/EDM.msl;
provider=System.Data.SqlClient;provider connection string="
data source=mypcname\SQLEXPRESS;initial catalog=Keepitremember;
integrated security=True;MultipleActiveResultSets=True App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
NB: The connection string names should be same with predefined names in EF model generation schema.
Related issues:
Model First with DbContext, Fails to initialize new DataBase
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode
Entity Framework cant use the DbContext, model being created
I'm using Entity Framework 6 and "model first" in my solution, I separated my "Data Model" classes into another project, so that I can add reference to the "Data Model" classes without exposing my "Data Model Contexts" and connections.
I don't want to expose my Entity Data Model project (especially the DB Context etc) to my UI Layer. I have this:
I have now successfully separated my auto generated entity classes from my data model, I tried it this works by adding an entity or a property to an entity is updated in the project Mapeo.BusinessEntity.
This is my connection string from DatabaseLayer (Mapeo.DatabaseModel)
<connectionStrings>
<add name="MapeoModelContainer" connectionString="metadata=res://*/MapeoModel.csdl|res://*/MapeoModel.ssdl|res://*/MapeoModel.msl;provider=System.Data.SqlClient;provider connection string="data source=raranibar\ral;initial catalog=Mapeo;user id=sa;password=*****;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
In my service layer I it copied this connection string to the App.config, my problem is this when I want to add a data I have this message: Unable to load the specified metadata resource How I can resolve this problem?
Updated
I found the solution, I changed the connectionstring in App.Config the pretentation layer I replaced in the connection strign "*" for the directory of DataModel in my case (Mapeo.DatabaseModel) this is now my connection string in layer presentation:
<connectionStrings>
<add name="MapeoModelContainer" connectionString="metadata=res://Mapeo.DatabaseModel/MapeoModel.csdl|res://Mapeo.DatabaseModel/MapeoModel.ssdl|res://Mapeo.DatabaseModel/MapeoModel.msl;provider=System.Data.SqlClient;provider connection string="data source=raranibar\ral;initial catalog=Mapeo;user id=sa;password=scugua;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Update your connectionString with correct path if you've such like issue to load metadata files. May it help someone else...
connectionString="metadata=res://DatabaseModel/MyModel...
When you copy an EDMX from one project to another, you must be careful with the name of the folder where you originally created the EDMX, because it's reflected in the app.config (or web.config), in the connection string related to the metadata resource. Like this:
//Initial model created in Folder1 in the original project
<add name="DataModelContainer" connectionString="metadata=res://*/Folder1.ServerModel.csdl|res://*/Folder1.ServerModel.ssdl|res://*/Folder1.ServerModel.msl;provider=System.Data.SqlClient;provider connection string= .../>
//Then manually copied to Folder2 in other project
<add name="DataModelContainer" connectionString="metadata=res://*/Folder2.ServerModel.csdl|res://*/Folder2.ServerModel.ssdl|res://*/Folder2.ServerModel.msl;provider=System.Data.SqlClient;provider connection string= .../>
After creating the entity if the database edit, entity does not work
As long as the entity update
In this way:
If this method did not work
It is better to do:
Remove this tag: <add name="MapeoModelContainer" ...
Remove MapeoModel.edmx
Add ADO.NET Entity Data Model
Then use the wizard to create a connection and entity
If this method did not work either send Inner exception
I am trying to set up a simple ASP.NET MVC 4 webapp using DB first migrations from a SQL Server (2005). I have created the tables in the database and used Entity Framework to create the objects in the code. I can access the data using these objects.
The problems come when I try to initialize the WebSecurity using WebSecurity.InitializeDatabaseConnection("FLMREntities", "UserProfile", "UserId", "UserName", true); in the Global.asax.cs file. I have tried using the InitializeSimpleMembershipAttribute filter that came with the template and got the same issue. I get the error message:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
Here is the relevant connection string:
<add name="FLMREntities"
connectionString="metadata=res://*/Models.FLMR.csdl|res://*/Models.FLMR.ssdl|res://*/Models.FLMR.msl;
provider=System.Data.SqlClient;
provider connection string="data source=notes.marietta.edu;
initial catalog=muskwater;
user id=muskwater;password=********;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
Also, I have created the membership tables in the database to match what the template creates. If I change the final parameter in the Initialize call to false (so that it does not try to create the tables automatically) it returns that it cannot find the UserProfile table. I have also tried variations on the names, such as [dbo].[UserProfile].
All I need is to have a simple account model to allow users to log in and allow certain users to see more content.
I had a similar problem, what I've done:
I modified the default connection. I already had a connection string for the edmx model, which looked like this:
<add name="ChemicalReporterEntities" connectionString="metadata=res://*/ChemicalDB.csdl|res://*/ChemicalDB.ssdl|res://*/ChemicalDB.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLExpress;initial catalog=ChemicalReporter;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
But I cannot used it with the SimpleMemebrship provider. So in Visual Studio I opened the Server Explorer > Data Connections, I selected my connection, right click, properties and I copied the connection string from there and pasted it to defaultConnection:
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=ChemicalReporter;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
After that I changed WebSecurity.InitializeDatabaseConnection to use the DefaultConnection.
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);
This could be a duplicate of this.
From what I remember about this problem, you need to make sure that the sqldata provider is registered. Sometimes, in your machine.config there is a duplicate entry that you need to delete and if I remember correctly, there could be an erroneous entry that you need to remove. Have a look at this msdn post, the info for this is about halfway down the page.
The link to the other SO has some links that could be helpful. If this ends up not being marked as a duplicate question, I can add them here as well or whatever is the proper thing to do.
The section you are looking for will look similar to this I think:
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider"
invariant="System.Data.SqlClient"
description=".Net Framework Data Provider for SqlServer"
type="System.Data.SqlClient.SqlClientFactory, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</DbProviderFactories>
</system.data>
From the information you have provided in comments, it seems that this is missing in your machine.config. Unless I am mistaken (possible) that means that the SQL Provider isn't registered on your machine. You'll need to add the above section to your web.config. You'll also need to make sure that System.Data.SqlClient is referenced in your project.
If this isn't installed on your target machine, you'll have more work to do, getting it installed. I wish I could help you with that more, but when I did this, it was for SqlServerCE, so the files I used are much different.
Good luck!
EDIT:
A critical piece of information, is the version needs to match the file that you are using. in type="blabh..blah..blah..Version=2.0.0.0, blah blah" that needs to be the version of the file you are using so right click your file and get the file version. If it fails, try variations of the version. I believe my file was 2.0.0.1 but Version=2.0 is what worked when I did it. Just try a few different versions based on your version number (2.0, 2.0.0, 2.0.0.1 for example).
Just wanted to chime in here, that I had this issue, by looking at my machine.config for the targeted .NET version that I was running off of... within the structure:
ensure that your targeted data providers are correctly inputted within there, only one per provider (I have seen duplicates cause problems), as well as, ensure that it is valid XML, and you only have one DBProviderFactories entry. In my case, I had a entry after the initial entry (essentially two entries, one with my providers, one blank), and the blank was was causing all the issues.
Hope this helps someone with the same issue :)
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient Provider, not valid."
I have a working console project. However when I tried to copy the exe and the app.config (same folder) to a live server it didn't work and got the following error. Could it be a domain issue, or some setting that's baked in? I'm pretty sure it has access to databases since I have used other projects except this time is different because I chose edmx.
<connectionStrings>
<add name="AdvWorksEntities" connectionString="metadata=res://*/GroupsModel.csdl|res: //*/GroupsModel.ssdl|res://*/GroupsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=abcd;Initial Catalog=AdvWorks;Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Solution
The app.config is a file name used in a project. However, when it is compiled, the file name becomes application's exe name + .config.
For example, if the application name is "sample.exe", then the configuration name should be "sample.exe.config".