Entity Framework - Medium Trust - asp.net

I'm trying to get the entity framework working in medium trust. I've tried splitting the files and using a separate assembly but I seem to have one problem after another.
I moved the EDMX to a separate assembly, which causes a single .dll to be outpit to the sites /Bin directory. I'm referencing this as below from web.config.
<add name="ApplicationDB" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Application.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
<add name="ShopEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Application.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Whenever I try to access one of the entity classes, I get an ArgumentException: 'An item with the same key has already been added.' It's critical this works with medium trust, but I seem to be running out of options. Any advice greatly appreciated.

You can use Entity Framework 3.5 in medium trust. However, there is a problem with running this on Windows 7 and Windows Server 2008. Microsoft released a patch for this. Read here for more info.

Moving the EDMX and all partial classes to a separate assembly was the eventual solution.

Related

Hosting a ASP.NET MVC Application my server a lot of errors

I'm going to try to be so detailed as possible. Please if you need more info please ask. So 3 days ago i finished a website it was ready for a small alpha test. But when i uploaded it to my server with IIS 8.5.6900.16384. I'm quite new to MVC and ASP.NET. And have never hosted a website. So i'm trying to fugue out how it works.
So i when to sites and created a new website. :
Application Pool = DefuaultAppPool
Puted a physical path of : K:\www\Init
Site name InitWebsite.
The website Had 2 databases I only cared about 1 Its name is ASPNET-InitSquad-20170512014420.mdf.
When i stated the website i got a sql error 52. That it could not connect to the server. Why i still dont know but i got it working a few hours later. But i got a other message Error 50. I also dont have the specifics anymore. But all i can remeber is that i installed a lot of SQL software. I installed:
SQL Express 2012
SQL Express 2014
SQL Express 2016 (I first could not install this but got it working)
SQL LocalDB 2012 Version 11
SQL LocalDB 2014 Version 12
SQL LocalDB 2016 Version 13.0.4001
I have also installed
SSMS V17
After a full 12 hours of installing i finally finished. Than i got error 26. Again i dont have that info anymore. That was yesterday That is when i decided to call quites for the night. So after all that i have tryed all thease connection strings :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=KEVIN-SERVER\SQLEXPRESS01;AttachDbFilename=K:\www\InitSquad.com\App_Data\aspnet-InitSquad-20170512024420.mdf;Initial Catalog=aspnet-InitSquad-20170512024420;Integrated Security=True;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-InitSquad-20170512024420.mdf;Initial Catalog=aspnet-InitSquad-20170512024420;Integrated Security=True;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v13;AttachDbFilename=|DataDirectory|\aspnet-InitSquad-20170512024420.mdf;Initial Catalog=aspnet-InitSquad-20170512024420;Integrated Security=True;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=KEVIN-SERVER\SQLEXPRESS01;AttachDbFilename=|DataDirectory|\aspnet-InitSquad-20170512024420.mdf;Initial Catalog=aspnet-InitSquad-20170512024420;Integrated Security=True;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
After a lot of testing non of these worked. Again a dead end. Now a few hours ago i was reading more i got a error again. Error 50 With a code (0x80131904) I don't exactly remember the phrase but i can recreate it if its needed. Now its the same and its saying Database"DatabasePath" Already Exists. Choose a different database name. Cannot attach the file "DatabasePath" As Database "DatabaseName"
That is where i'm now i was searching i have unblocked some ports.
And it still doesn't work. Than i was reading that i soul get SQL Server Agent (MSSQLLOCALDB) To run but i tried it but i seem not to be able to get it working its saying it takes too long to start. And yes i have opened the ports. Just to get the start button to enable was already a pain. Anyone got suggestions. This is almost impossible to do at this point i'm going in to the 5th day now. Please just tell me if you need any more info. I am also using Visual Studio 2017. A other person is saying you need to pay for it. What sounds a bit weird that Microsoft is going to force people to pay for web applications with databases. So in basic terms everything on the internet
I got it It was the connection string i was awake for 4 days just because of the connection string. Tip for new users don't take LocalDB create a new DB sql will do this for you this was my end connection string
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=KEVIN-SERVER\SQLEXPRESS01;Initial Catalog=ASPNETAplicationDatabase;Trusted_Connection=yes;" providerName="System.Data.SqlClient" />
</connectionStrings>
This video is the best https://www.youtube.com/watch?v=BwckH9bVqPc

ASP .NET entity framework project giving UnintentionalCodeFirstException after being published to azure

I have a ASP .Net project that I want to publish to Azure. At first, I did Enable-Migration and published the website by checking "Execute Code First Migration" check box in Publish Web dialog box in Visual Studio.
But I realized I am not using Code first approach since I have my DB already designed. I am using EF to create models for the database tables.
I deleted the Migrations folder from my project and republished the project WITHOUT checking the "Execute Code First Migration" for the database. But after publishing I still get this error:
What am i missing?
EDIT:
From web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-IMS2-20161228113511.mdf;Initial Catalog=aspnet-IMS2-20161228113511;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="IMSEntities" connectionString="metadata=res://*/Models.IMSEntities.csdl|res://*/Models.IMSEntities.ssdl|res://*/Models.IMSEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\Projects;initial catalog=IMS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
From Azure portal. (Settings >> Application Settings >> Connection String). The name of the connection string in Azure is IMSEntitites.
Server=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;Persist Security Info=False;User ID=myUserName#xxxxxxxxx;Password=myPassword;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
What does your connection string look like? ( you can remove the sensitive bits )

MVC Migrate IdentityUser SQL Server 2012 Migration

Is it possible to change the IdentityUser to use a SQL Server 2012 database instead of the localdb that installs by default when creating a new ASP.NET Web Application?
I have an existing SQL Server 2012 database which I would like to use with the MVC 5 project, I can modify the connection string in the web config, however I get the error "The entity type ApplicationUser is not part of the model for the current context".
I've can't find any reference to AspNetUsers to that I can substitute for my User table.
Am I forced to modify the existing localdb and rewrite my previous database to work with the new AspNet table structure and if so can the database then be migrated and upscaled to SQL Server 2012?
Any assistance would be much appreciated :-)
Simply enable migrations for your project (assuming VS2013 - newer versions are different and don't require migrations to be enabled for vNext)
1. Enable-Migrations
2. Update-Database -Script -SourceMigration: $InitialDatabase
That will generate the scripts you need.
Solved this by closing Visual Studio 2013 and locating the path of the localdb which was WebApplication1\App_Data, there were two files aspnet-WebApplication1-20140430012043.mdf and aspnet-WebApplication1-20140430012043_log.ldf residing in that folder.
I modified the security permissions of WebApplication1\App_Data so that SQL Server 2012 would have no issues connecting to it.
I then opened Microsoft SQL Server Management Studio and proceeded to attach the database, once the database was attached to the instance, I backed the database up. Next I dropped the database and carried out a restore, this time renaming the database as per the project and relocating the mdf and ldf files.
I then opened my project in Visual Studio 2013 and modified the Web.config from:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20140430012043.mdf;Initial Catalog=aspnet-WebApplication1-20140430012043;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
to:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=MYSERVER;Initial Catalog=WebApplication1;Integrated Security=True" providerName="System.Data.SqlClient" />
Ran the project and all is well.

ASP.Net SQL ConnectionStrings config

I am trying to pick up ASP.Net - and following the mvc tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store-part-4 (Note: I am totally new to the .Net framework, both C# and ASP.Net)
At the beginning the tutorial suggested to use SQL Server Compact 4.0 - But, I got the SQL Server Express installed instead (since I will be using it after, not just for the tutorial)
SELECT ##VERSION AS [Version]
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) Apr 2 2010 15:53:02 Copyright (c) Microsoft Corporation Express Edition with Advanced Services on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
Now, I am having trouble when I got to the point to set up the db connection for the tutorial. The tutorial step to add this to the Web.config:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
But that doesn't work - and pretty sure it's because the different SQL Server that I am using. Suggestion on how to make this work?
I've tried changing the providerName to "System.Data.SqlClient" and it still doesnt work, and I've tried the following too:
<connectionStrings>
<add
name="MusicStoreEntities"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
And it still doesnt work with the following exception: The provider did not return a ProviderManifestToken string
Help with setting up this ASP.Net - SQL Server Express 2008 would be greatly appreciated!
EDIT
Seems like now I got it working, at least to connect to the db:
Now, obviously the database is empty - the tutorial didnt even say about instantiating the database, other than downloading some asset file for the db "SampeData.cs", and add it to the Global.asax.cs Application_Start method:
System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData());
And now, 2 things:
The line above in Application_Start dont seem to be hit when in debug mode (I did try rebuild solution)
Now I am getting the following error: Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
I think I am missing instantiating the data in the db - but not sure how / what - the tutorial doesnt say much
I assume that you have restored a copy of the database to SqlExpress. Once you do that, you should add a Database element to the string with the name of the database you restored it as. A helpful resource for connectionstrings is http://www.connectionstrings.com
<connectionStrings>
<add
name="MusicStoreEntities"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Database=MusicStoreEntities"
providerName="System.Data.SqlClient" />
</connectionStrings>
Note that what you have is a link to an .sdf file, which is a SqlCE database. The |DataDirectory| portion is replaced with the location of your App_Data folder in your application.
This is what I use:
<add name="<CONNECTION NAME>"
connectionString="Data Source=<SERVER NAME>;Initial Catalog=<DATABASE>;Persist Security Info=True;User ID=<USER ID>;Password=<PASSWORD>;"
providerName="System.Data.SqlClient" />
Replace <...> values with your corresponding values.
I can never remember connection strings, I use this site to figure out what they need to be.

Database in App_Data folder -> Unable to open the physical file "xyz.mdf"

I am creating a package of an asp.net mvc3 application to distribute it. Means I want to zip it and send it to someone for review. I want that you just start the solution in VS and it runs. So no need for changes in web.config.
I was using sql server with hard-coded DataSource. (COMPUTERNAME\SQLEXPRESS)
I changed it to .\SQLEXPRESS to make it relativ. Better.
Then i detached the db with SSMS, copied the DB to the App_Data folder, added AttachDBFilename=|DataDirectory|MyDatabase.mdf;
And (as found on msdn) Initial Catalog=; "use it but don't set it"->great feature ;)
This is the result:
<connectionStrings>
<add name="DBService" connectionString="Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|MyDataBaseFile.mdf;Initial Catalog=;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
When I start the asp.net Development Server (hit "play") in VS Web Developer 2010 there is a Database error:
Unable to open the physical file ".........mdf". Operating system error 5: "5(Zugriff verweigert)".
Any suggestions how to attach the file to the solution? What about switching to Compact Edition?
Have you tried adding
User Instance=True
?
Data Source=.\SQLEXPRESS;AttachDbFilename="|DataDirectory|MyDataBaseFile.mdf";Integrated Security=True;User Instance=True

Resources