Why would the RIAServices.EntityFramework NuGet Package break context class code generation? - entity-framework-5

I have an existing project using RIAServices with Entity Framework. The project builds correctly and generates the AmsiWeb.g.cs file with all the context classes for my services.
I am converting my designer based entities and ObjectContext with Code First entities and DbContext. I installed the RIAServices.EntityFramework NuGet package to the web application that contains my services. However, now when I build the AmsiWeb.g.cs file only contains the WebContext class. It doesn't contain any generated services.
I have only at this point converted a single EDMX model to Code First and DbContext and made the requisite changes to the services that use that model to inherit from DbDomainService.
I am using EF 5.0... not sure if that matters cause I'm not sure how adding a DLL to the AmsiWeb application project would break code generation.
What would cause this to no longer work and how can I fix it?

Maybe it's a problem within the msbuild task that generates the proxy code (I mean the *.g.cs file). Probably it's looking for the wrong version of a entity framework. Have a look at this blog post http://mcasamento.blogspot.it/2012/10/entity-framework-5-code-first-and-wcf.html in the final part I wrote an assembly redirect statement that did the trick

It turns out that their needs to be a redirect for Entity Framework 5.0 (4.4.0.0 since I was using .Net 4.0) in the web.config. But, since my RIA Services were in a web application project that was not my root project the code wasn't generating.
Once I added the redirect to the web.config of the web application with the RIA services in it, the context code was correctly generated.

Related

How to do the Authorization in Asp.net by using Identity Framework?

For doing authorization in asp.net mvc 5 I have searched lot on internet but not getting correct answer. Actually I am using Visual studio community edition with .net framework 4.5.2 and mvc5. I want to do the Authorization in my project. My project is created with no authentication mode while creating a project.
Firstly I have tried these things:
I typed a command "Enable Migrations" on Package manager console.
Then I got one error like: Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
I dont know how to resolve this error. please help me to get out of it.
Which approach did you chooses to build our database model? Code First, Model First or Database First?
I'm asking because you can't use migrations with Model First or Database First approach. EF Migrations is a product targeted for Code First approach. CodeFirst assumes that you will never make any changes manually to the database. All the changes to the database will go through the code first migrations.
But there is solution for that - you can use Code First approach with an existing database. Take a look here: Using EF “Code First” with an Existing Database.

Entity Framework 6 Code First Migrations using Identity user models in a separate project

Is it possible using EF6 Code First and MVC5 to put all the models, views, and controllers that involve ASP.Identity into its own class library project. So that over multiple web applications you could use that same DLL and already have all the views / controllers / models and be using the same security database for multiple applications?
We have several web applications with separate databases and one security database that handles all of them, and we weren't sure how to keep this model now that we're moving to EF6 Code First and MVC5.
If it is possible could someone point me to a tutorial of something similar or give me a basic outline of steps to go through?
Or if there is a better way to achieve my goal, of having one set of code to handle ASP.NET-Identity security that I can plug that dll into multiple web applications and get the same logic and databases?
Or is this not a good idea in general?
Very open to suggestion and advice. I appreciate it.
Yes it is. We do this with every project that we have. The structure is very simple. Just create a class library project to your solution, add EF to the project, then reference the class library from your main project.
If using Code First Migrations be sure to select the class library project as the default project in the Package Manager console when running migrations or adding migrations.
Here is a pseudo solution structure for your solution
MySolution
- MyWebApp
reference: MyDAL
-MyDAL
reference: EF6
The advantage that I find to this is that you can then reference the "DAL" class library from say a companion console application or windows form application, or a companion website, even in a different solution, and they will use the same code base.
For example:
MySolution
- MyWebApp
reference: MyDAL
- MyDAL
reference: EF6
- MyOtherWebApp
reference: MyDAL
NOTE: Your data context will look for its connection string in the Web.config or App.config in the startup project. NOT the class library. This can be confusing at first... But once you think about how .NET compiles the application together into the final package, it makes sense.
If you're talking about creating one class library for an entire data layer shared between multiple projects, then that's easy enough. You can move all your models, your context, etc. into a class library and run migrations using the class library project. The other projects will just reference that class library and not have migrations of their own.
However, if you're talking about multiple databases and associated data layers, where project Foo has its own models, context and migrations and project Bar has its own models, context and migrations, while the class library has just the IdentityUser and IdentityDbContext, things get a little more complicated. You won't be able to combine any of these contexts. So in your Foo project you'd have to instantiate your context for Foo and your Identity context if you need to work with both. It's not a problem, per se, but it's something to be aware of.

model backing the context has changed since the database was created. Consider using Code First Migrations to update the database

I am using MVC5 and EF6 CodeFirst migrations. My model is in a separate project from my MVC web app and I keep getting this error when trying to access the model classes. I have automatic migrations already enabled. I can drop the entire database and then using update-database to regenerate everything I still get this error. The error is wrong because the context has not changed since I created the database. Also, through a Unit Test project, using the same calling code as I have in my MVC app, I can reference the same Model project, access the model classes and data. I have the Model separate from the MVC project because I need to be able to reuse the Model outside of the web.
The model backing the "xx" context has changed since the database was created. Consider using Code First Migrations to update the database
Database.SetInitializer<DbContext>(null);
http://patrickdesjardins.com/blog/the-model-backing-the-context-has-changed-since-the-database-was-created-ef4-3
I got a similar problem :
The model backing the 'ApplicationDbContext' context has changed since
the database was created. Consider using Code First Migrations to
update the database
I have one project for MVC and another project for the model, context and repositories. I've been working on it for weeks but today it said stop.
I have tried to delete database, enable-migration, add-migration and update-database so many times that I've lost count. I've added initializers to MigrateDatabaseToLatestVersion as well as DropCreateDatabaseIfModelChanges.
What finally made it work was to move model, context and repositories into the MVC project (not something I was keen on)...then it worked right out of the box without any code changes at all (besides namespaces)! Very strange...
I've read so many blog posts during the day trying to solve this problem. One of them (I don't know which one) mentioned a bug in Visual Studio 2013 where reference to DLL files weren't always updated as they should, suggesting that my MVC project missed out something when I was running add-migration and update-database in my separate project. But it's just a guess.
I'm using EF 6.1 and .Net 4.5.1 in my solution.
Just to elaborate on RouR's answer:
I had an MVC web project that had a model attached.
I then created a console app that consumes this model.
My console app, only is aware of the connection string and has EF reference, just doesn't know how to let each one communicate.
Hence, the model backing database changed error appears
The engine is just confused how to proceed it sees and expects an entity, as was reference, just losses it when it fails to find the proper context, and assumes context wan't migrated properly.
So here we are,
Database.SetInitializer<MyProject.Models.MyModel>(null);
We are having the same issue. What solved it for me was not necessarily to move the Model, Context into the MVC project, but move my seperate project to the same solution as the MVC project and reference it by project folder.
This worked, but referencing a DLL instead of a project did not work.
Funnily enough, when creating a new Console Application and referencing the DLL assembly, we did not get this issue... only from an MVC project
We use EF code migrations to keep databases schemas up to date. The other day I had the same problem, in my case I was working with 2 database instances (QA and DEV databases of the same client with exact same data however QA environment was throwing this error. I've fixed the problem by deleting the _MigrationHistory folder and problem solved.

Is it possible to make a self-hosted project of a ServiceStack+MVC3 like SocialBootstrapApi?

I am learning how ServiceStack works with the help of self-hosting projects.
Self-hosting projects are much easier to debug and understand, since no magic happens inside IIS.
I followed these steps, read http://www.servicestack.net/mythz_blog/?p=785 and applied them backwards.
It worked fine for Docs, but not for the SocialBootstrapApi. The code in Application_Start() from Global.asax is the problem I think
Steps for ServiceStack Docs
Download the ServiceStack Docs sample project
Create a new console project (.Net 3.5 since Docs is still 3.5)
Copy all files from old web project except web.config. (I first used drag and drop, but that only moved the file, it didn't update the .csproj file, so I had to use an editor to copy some ItemGroup sections.)
Move AppHost into a separate class file, changed to inherit from AppHostHttpListenerBase
Copie the Programs.cs file from the RazorRockstars project
Change the baseUrl: var baseUrl = "http://localhost:2001/"; // ConfigUtils.GetAppSetting("WebHostUrl");
Copy the app.config file from RazorRockstars project (but this wasn't needed)
Change output path for console project to .\ because otherwise PageManager.Init got the wrong path and most files were not found
When you done this, download the complete ServiceStack source code and recompile in Debug mode, replace all ServiceStack references in the Docs project by the newly compiled things. Now, you can easily step through all ServiceStack-code, set breakpoints etc, and understand what is happening and when. Console projects also have some other advantages, they start faster, Edit-and-Continue works more often.
For SocialBootstrapApi I did the same except/and
Renamed web.config to app.config
Commented away // [assembly: WebActivator.PreApplicationStartMethod(typeof(SocialBootstrapApi.AppHost), "Start")]
Now, the normal ServiceStack part of the project works, although the url is without the initial /api/
Tried moving Application_Start() from Global.asax to AppHost.Configure ==>> Refuses to start
ASP.NET MVC can't be hosted in a self-hosted HttpListener (since it's coupled to ASP.NET), but ServiceStack does support hosting it's Razor and Markdown view engines inside a self-hosted HttpListener app.
The Razor Rockstars website is a show case website of ServiceStack's Razor and Markdown view engine support. razor-console.servicestack.net is the same website hosted in a HttpListener self-host. The source code of RazorRockstars is on GitHub with the RazorRockstars.SelfHost example contains the source code for razor-console. The one thing you have to remember with self-hosted websites is that the Razor view pages Build Action in VS.NET needs to be set to Copy if Newer so the views are copied in the /bin directory so they can be found by ServiceStack at runtime.

deploying global.asax to sharepoint 2010 in an enterprise environment to enable unity DI container

Working on a SharePoint project I'm trying to use Unity as a dependency injection container.
My first idea to get this container running is using the global.asax as described in the best practices by P&P:
http://webclientguidance.codeplex.com/releases/view/17134#DownloadId=43305
In these best practices they tell you to manually edit the global.asax file to make it inherit SPUnityHttpApplication.
<%# Application Language="C#" Inherits="Unity.SharePoint.SPUnityHttpApplication" %>
Manually editing this file is not an option in enterprise environments since we have multiple environments (DTAP) and all of them have multiple frontend servers that would need manual steps.
I can't find any way to deploy a global.asax file by using a feature or wsp or anything because the global.asax is located in the web application root and sharepoint deploys other files to the /14 hive folder so you can't acces the web application root directory.
Alternatives i've looked into is the SharePointServiceLocator. this build in functionality does almost what i want. but it can only resolve classes that have a default constructor. this way i can't chain resolve all my implementations by using constructor injection. I found a post how to change the service locator to make use of unity but this doesn't seem to work properly if you read the comments.
My problem can be fixed by fixing 1 of these 2 main problems:
Don't arrange unity in the global.asax, but then where and how?
Deploy the global.asax in sharepoint? possible?
The global.asax doesn't seem to be the best solution to do this because of the deployment issues described in the question.
A viable solution is implementing this in a httpmodule
The init method can be used to wire everything up since this is called when the sharepoint application starts.
the httpmodule can be added in the web.config by a feature receiver
This way there is no need to do tricks with the global.asax that is located in a directory you can't deploy to with a feature and you have all the functionality and correct time to instantiate the DI container.
It may not be ideal, but you could look at using a feature receiver and write code to edit the existing files directly.

Resources