Trying to upgrade, from EF4.1 to EF6.1, ObjectContext reference code? - entity-framework

I am trying to upgrade my MVC3/.NET4.5 application to use EF6.1, in the main part for its performance improvements.
One of my problem code lines is :
private AppEntities db = new AppEntities();
I have removed system.data.entity from references and web.config, but this line still seems to be looking for this dll. I did try and add:
using System.Data.Entity.Core.Objects.ObjectContext;
Did not seem to fix the issue with AppEntities line trying to find system.data.Entity v4.0.0.0
Many thanks
EDIT:
I managed to sort this by adding:
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
to AppEntities.cs file.
Also needed to add to some of the POCO files.
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
It all builds fine.
However my problem now is that it seems to take an age to show the page on first load, guess this is to do with native code creation. Also I have heard that much EF code that was native in .NET is now MSIL in Entity Framework.dll and thus need compiling to native code as well. Perhaps this is what is causing my pain, see: EF6 Warm up metrics

Related

add scaffolded item wizard cannot resolve data model class

everyone.
I'm trying to create new Scaffolded item for simple ASP.NET Wep API application which should be based on domain object class and DbContext derivative in separate assembly. The assembly is in the solution, target app has a reference to it (and manually created code which invokes the classes from my lib is build up without any errors) and, obviously, the classes I've mentioned have access modifier public.
The problem is wizard for creating new scaffolded item cannot see my model classes. (By the way, when the model classes were in another ASP.NET MVC5 app the wizard worked well.) I'm using Visual Studio 2013 Update5.
How to fix this? Any workaround would be helpful too!
Sorry for disturbing.
the origin of the problem seems to be that I've move my data model classes from ASP.NET MVC app into my class library in wrong way.
I've not just cut/paste them, but copy-paste, cut-paste (confirmed replacement).
this is the only thing which could cause the problem (although I still don't know the details).
Never the less I've started from the beginning again and cut/past the data model files initially - everything seems to work fine now.

BreezeJS: Metadata() Error after upgrading Breeze 1.2 to 1.4 and EF 5.0 to 6.1

I tried to upgrade my project in Angular using Breeze 1.2, EF 5.0 to use latest Breeze/EF. The project was working fine, but I ran into various issues after upgrade.
Actually, I only need some new features in EF, so I tried to upgrade EF from 5.0 to 6.1 first. However, I can't install EF 6.1 with Breeze 1.2. So, I had to upgrade to Breeze 1.4 first. After that, I had to make some changes due to new namespaces to get the project compile again.
First problem is that route has changed. In v1.2, I used to call
var manager = new breeze.EntityManager("api/breeze");
but in v1.4, I had to call
var manager = new breeze.EntityManager("breeze/breeze");
Then, I got this error on client:
GET http://localhost:1476/breeze/breeze/Metadata 500 (Internal Server Error)
On server, I got this error message on call to get Metadata():
The property 'Id' cannot be configured as a navigation property. The
property must be a valid entity type and the property should have a
non-abstract getter and setter. For collection properties the type
must implement ICollection where T is a valid entity type.
I'm getting lost here. I found this post and this post mentioning the namespace changes. However, I have no idea how to fix it in my case. For example, on server, my EntityState is coming from Breeze.ContextProvider, not from System.Data.
What is problem? Are my entities (I have a lot!) no more valid? Do I have to redefine all my entities?
Any hint is much appreciated!
Okay, after much struggle, I got it work now, but with the same code, so I'm not sure how it got fixed. This are the steps I tried. I commented out all the entities first, then I get the metadata without problem. Then, I added back the entities one by one and run get metadata, but no more errors.
One possible guess is that, after upgrading to EF 6.1, I didn't change the entities, so the my db schema didn't change and EF 6.1 is incompatible with the previous schema. Once I changed the entity, the auto migration kicks in. It's just my guess.
Hope it helps in case somebody runs into the same problem.

How to disable code-first feature in EF (MVC4 Visual Studio 2012)

How to disable code-first feature in EF (Visual Studio 2012)
I am using Visual Studio 2012, MVC4 (Internet application template).
I want to use EF, but not with its code-first feature. I would want the application to error out, rather than create or modify my database based on my code. (i just can not live with this feeling of my database being changed behind the scenes ... i want the application to use the exact db i have created ... and if there is any thing that has to be changed, i'll do it my self)
is this possible with the new Ef (VS2012)?
i have seen many people asking this, but so far i am unable to find the answer.
You can use Code First and ensure that your database never gets updated or overwritten when you change your model by setting the database initializer to null:
Database.SetInitializer<MyDbContext>(null);
It's a static method of the Database class and should be called at the beginning of your application, for example in global.asax or in a static constructor of your context class. Doing this you have to change model class and database schema manually so that they match.
You can also use the Reverse Engineer feature to create a Code First model from an existing database. It is explained here: http://msdn.microsoft.com/en-us/data/jj200620
Or if you don't want to use Code First at all and work with a model designer you can use the Database First approach, explained here: http://msdn.microsoft.com/en-us/data/jj206878
An overview about all the possible options is here: http://msdn.microsoft.com/en-us/data/ee712907.aspx

FacebookSDK - AllowPartiallyTrustedCallers Problem

I'm using the latest version of the FacebookSDK (4.0.2) (facebooksdk.codeplex.com)
And I noticed the Src code is made up of several projects namely:
1)
Samples -> Facebook.Samples.AspWebsite Project
Samples -> Facebook.Samples.MvcFacebookApp Project
Samples -> Facebook.Samples.MvcWebsite Project
2)
Tests -> Facebook.Tests
Tests -> Facebook.Web.Tests
3)
Facebook Project
Facebook.Web Project
Facebook.Web.Mvc Project
Okay, so I am building a Facebook Canvas .Net 4.0 MVC 2 application and am simply using the Facebook.Samples.MvcFacebookApp Project and replacing and adding classes and files as I see fit for my situation. However I have come to the point where my site will be running on a Microsoft Azure cloud and I want to abstract the data layer (database) into it's Project. So I went ahead and created a new Project in the solution called BusinessData and added an EntityDataModel to it, so now the solution looks like this:
1)
Samples -> Facebook.Samples ...
2)
Tests -> Facebook.Tests ...
3)
Facebook Project
Facebook.Web Project
Facebook.Web.Mvc Project
BusinessData Project
-> Business.edmx
Great so now I add a reference to the BusinessData Project to my Facebook.Samples.MvcFacebookApp Project and try and access my database. However I get this error in my TrackUserAttribute.cs class in the Facebook.Samples.MvcFacebookApp Project:
System.MethodAccessException occurred
Message=Attempt by security transparent method 'Facebook.Samples.MvcFacebookApp.Models.TrackUserAttribute..ctor()' to access security critical method 'BusinessData.BusinessEntities..ctor()' failed.
Assembly 'Facebook.Samples.MvcFacebookApp, Version=4.0.2.0, Culture=neutral, PublicKeyToken=null' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.
Source=Facebook.Samples.MvcFacebookApp
StackTrace:
at Facebook.Samples.MvcFacebookApp.Models.TrackUserAttribute..ctor() in C:\Users\rkara\Desktop\FacebookPazooza\ThuziFacebookSDK\Source\Src\Facebook.Samples.MvcFacebookApp\Models\TrackUserAttribute.cs:line 13
InnerException:
After some uneventful research and not being fully understood about CAS Policy and how that has evolved with ASP.Net 4.0 my understanding is that the GlobalAssemblyInfo.cs file contained within all of the projects of the FacebookSDK solution has something to do with it.
This line of code in the GlobalAssemblyInfo.cs file:
#if !(SILVERLIGHT || TESTS)
[assembly: AllowPartiallyTrustedCallers]
#endif
seems to be the culprit. According to this Microsoft expert:
http://www.simple-talk.com/dotnet/.net-framework/whats-new-in-code-access-security-in-.net-framework-4.0---part-i/
BusinessData has SecurityCritical level privileges. My class TrackUserAttribute.cs only has SecurityTransparent, so therefore cannot does not have privileges to invoke methods in the BusinessData assembly. Is this correct?
I've tried all sorts of things to get this thing to work even removing [assembly: AllowPartiallyTrustedCallers] from the GlobalAssemblyInfo.cs file, but I get another problem with the .edmx file itself saying it can't load the metadata, so I assume it still has something to do with full trust privileges etc.
I am sure there is a simple answer to this and am sure the guys who developed this SDK wrapper would have consider the fact that people will be extending their SDK to suit individual needs.
What is the best approach for me to ensure abstraction of my data and anything else I want to abstract without having to tie it into the projects provided?
For now and to continue with development and not be tied up in this for too long I've just stored my entity model (edmx) into the models folder of the Facebook.Samples.MvcFacebookApp. This works for now but I would much rather store the edmx in its own project.
Cheers
Rob
Ok so after I left that problem and moved onto installing log4net as a event logger for my application I came across this site:
http://it.toolbox.com/blogs/daniel-at-work/using-log4net-in-cnet-26794
Essentially I wanted to convert log4net to asp.net 4.0 rather than 1.0/2.0 that it was originally written in. Then after reading through this guys instructions on how he got the old log4net code to work in 4.0 the aha! moment came. Essentially all he needed to do was to put this attribute into his AssemblyInfo.cs file:
[assembly: AllowPartiallyTrustedCallers]
So I did the same for my BusinessData project and updated the AssemblyInfo.cs file there, and hey presto it worked!
After some deep contemplation I figured that what this did was allow my Facebook.Samples.MvcFacebookApp partial privileges to call a SecurityCritical assembly including those objects contained within it. Thus I could go ahead now and call my edmx (EntityDataModel) in this assembly from the Mvc application.
The second fix and is not fully known why this is necessary but it fixes the problem is to copy the connection string from the App.config file to the Web.config file in the Mvc application. For some reason the edmx seemed to try to read from the web.config instead of the App.config for the connection string. So now I have the same connection string in the App.config and Web.config file.
Meh! it works and I'm happy ... moving on!
:-D

ASP.NET MVC2 IoC: Looking for an example using CastleWindsor container that works with minimum config

I am looking for an example of how to configure an ASP.NET MVC2 project to use CastleWindsor container to do IoC.
I keep running into problems setting it up, and for every problem there seems to be a solution on-line, but in the end I make so many changes and end up with such a verbose setup to get IoC working using CastleWindsor, that I thought it best to ask this question.
I am looking for the minimum configuration required in the Global.asax page, the Web.config, and if required, what other changes and extension classes are required.
I am not looking to inject into actionfilters at this stage, so just the basics. Preferably not using XML files, but doing it in .NET programatically.
Thank you in advance...
This is as basic as it gets:
Start a MVC2 project from VS2010
Download MvcContrib for MVC2 (the one that says "extra binaries")
In your project, add a reference to (all these DLLs are included in MvcContrib):
Castle.Core.dll
Castle.DynamicProxy2.dll
Castle.MicroKernel.dll
Castle.Windsor.dll
MvcContrib.dll
MvcContrib.Castle.dll
In your Application_Start(), add these lines (and whatever namespaces are needed):
var container = new WindsorContainer();
container.RegisterControllers(typeof(HomeController).Assembly);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));