ConnectionString problem Entity-Framework - entity-framework

I have a Project with Entity Framework. On till yesterday everthing was working as expected. After an Update from Visual Studio 2019 to Version 16.6.1 i got the following error:
SqlException: CREATE FILE encountered operating system error 5(Zugriff verweigert) while attempting to open or create the physical file 'C:\Users\benjaPRWSolution2020.mdf'.
The funny thing is, in my opinion C:\Users\benjaPRWSolution2020.mdf should be C:\Users\benja\PRWSolution2020.mdf
also as referance my connnectionString what i didnt has changed since long time
<connectionStrings>
<add name="con" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=PRWSolution2020;Integrated Security=True" providerName="System.Data.SqlClient"/>
Is there any possible workaround?
Update:
When i input
attachdbfilename="C:\Users\benja\PRWSolution2020.mdf"
to my connectionString its working. But this can not be the solution.

Related

Reverse Poco only Generating initial comment block

I'm trying to generate models from an existing database using the Reverse POCO generator. I've not made any changes to the default template file and I've called my connection string "MyDbContext".
<connectionStrings>
<add name="MyDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=xx.xxx.xx.xxx\SQLEXPRESS;Initial Catalog=ZooQ;Persist Security Info=True;User ID=ZooQDev;Password=******;"/>
</connectionStrings>
I've checked the credentials are ok by testing the connection in visual studio, so I know they are right. Only difference to the usual way I do this is that now I'm accessing a VM server on Azure, but I wouldn't think that would be the problem.
I've tried uninstalling and reinstalling the generator, uninstalling and reinstalling Entity Framework, deleting and re-adding the template file and closing and re-opening Visual Studio.

EF works but I can't find the database (but only in the test environment)

My three environments are all acting differently.
EF came with my chosen MVC website project template and I haven't messed with it. I do have a mdf file in my App_Data folder. The location and filename were determine by the nuget installer I assume. Identity framework uses it. To give you some context.
My web.config ef connection string is:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-AACOMvc-20160730112925.mdf;Initial Catalog=aspnet-AACOMvc-20160730112925;Integrated Security=True" providerName="System.Data.SqlClient" />
This is the connection string provided by the template / installer whatever.
Local Machine
It works. If I mess with the connection string or the mdf filename, it breaks. So I know it's hitting that file.
Test Environment
It works. Funny because I don't publich the App_Data folder! I have searched all over that box looking for the mdf (it created?) but there is not one. If I comment out the connection string in the web.config, it still works! It's being "smart" and "sneaky" and I don't like it! :)
Production Environment
It doesn't work. Even if I copy over the App_Data folder, it still will fail with "file not found" error.
This question is focusing on the test environment. If I can find out how it's working, I think I will be closer to getting it to working in production.
I recall I fixed this by changing the EF connection string to a sql server instance instead of a sql server file. EF didn't even create any tables in the database b/c I don't have any entities defined.

Entity Framework Code First Data Migrations not working with VS2012 Web Deploy

I have created an MVC 3.0 application using Visual Studio 2012, .NET 4.5 and Entity Framework 5.0.
Using Code First Data Migrations, I am able to correctly propagate model changes to my local test database, but I can't figure out how to get this to work when deploying to my staging and production servers using Web Deploy.
I have read the following article ...
http://msdn.microsoft.com/en-us/library/dd394698(v=vs.110)#dbdacfx
... which explains what's supposed to happen, but it's not working for me, as Web Deploy seems unable to detect that I am using Entity Framework. The tutorial shows a checkbox to enable execution of Code First Migrations ...
... but my dialog shows the only Update Database checkbox for each database.
I have read that, in order for Visual Studio to detect the use of an Entity Framework context, the Web.config must include an element that defines it. Here's mine:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<contexts>
<context type="MyContext, MyAssembly">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyContext, MyAssembly], [MyConfig, MyAssembly]], EntityFramework">
<parameters>
<parameter value="MyConnectionStringName"/>
</parameters>
</databaseInitializer>
</context>
</contexts>
</entityFramework>
Any suggestions would be greatly appreciated.
Thanks,
Tim
I have discovered the solution to this problem through experimentation.
In fact, my MVC application was created by VS2012 by converting from a VS2010 solution. Since the conversion process did not report any issues, I assumed that it had correctly converted everything, including the publishing profiles.
However, I discovered that the problem was in the conversion of these profiles and, unless I manually edit their XML files, there is apparently no way to get old imported profiles to participate in Code First Migrations.
Simply creating new publishing profiles in the converted solution results in the expected behavior.
Try this:
Enable-Migrations -Force
Ensure the below is set to true
AutomaticMigrationsEnabled = true;
Republish - worked for me

Publish project with embedded DB connect with entity framework

I have a SQL Server CE 3.5 DB which is set to "Embedded" vis a vis Properties. The database has an associated .edmx file and entity-framework code works when I run it from VS 2010.
The app.config string is the following:
<add name="MapProjectDB2Entities1" connectionString="metadata=res://*/MapProjectDB.csdl|res://*/MapProjectDB.ssdl|res://*/MapProjectDB.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=MapProjectDB2.sdf"" providerName="System.Data.EntityClient" />
But when I Publish my project and run it from the one-click deploy folder, the exception is "The Underlying Data Provider Failed to Open".
So I need to somehow connect to my embedded database. Thanks in advance the google hits are a bit spotty for this one.
I should mention that the DB is member of a library (DLL) project, which is loaded from another project.
I needed to add the DB to my Exe project. From there I needed to change the config string to be the following:
The difference is only the |DataDirectory|\ part
<add name="MapProjectDB2Entities1" connectionString="metadata=res://*/MapProjectDB.csdl|res://*/MapProjectDB.ssdl|res://*/MapProjectDB.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=|DataDirectory|\MapProjectDB2.sdf"" providerName="System.Data.EntityClient" />

How to have multiple projects accessing same Entity Framework project?

Ok, this question has been asked many times. Just googling with the error message:
The specified named connection is either not found in the configuration,
not intended to be used with the EntityClient provider, or not valid.
I have tried all suggestions on forums, help sites et blogs... nothing works!
Situation recap
I'm splitting solution into three different projects: one for my model with EF4, one of WinForm and one ASP.NET MVC web application. I have a problem with connections string and configuration.
Is there a updated solution using VS2010/EF4/.NET Framework 4.0?
Not sure, but you may have to move the Entity Framework configuration sections in your EF4 project's app.config file to both your WinForm app.config and ASP.NET MVC web.config files.
Really, the answer is copying the <connectionStrings> section in the other project's config files like mentionned above.
<connectionStrings>
<add name="DataEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=xxx;Initial Catalog=xxx;User Id=xxx;Password=xxx;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
So, it's the same answer as I saw before on many sites. In fact, it was working from the beginning, but I had another error ("Configuration system failed to initialize"), so I thought it was a wrong value of the connection string in the app.config. In fact, the app.config was valid, but just moving the section on bottom solved this... Bizarre?