I would like to ask you how can I implement DB First model in ASP.NET Identity Scheme. When I create a WebAPI project with individual user authentication I can see code-first model with some implementation in it. I am trying to solve this and I was searching for some solution but i didnt found it. Do you know about any good tutorials or just the solution? Thanks
Models need not be present always inside Model folder of web-api project.
They can be from other dlls as well.
I would suggest you to create a new project for your Data Access Layer, and add the EF databasefirst dbml to that project. Later refer it in your web-api project to use it.
Related
I am new to MVC and EF.
I use to follow 3 tier architecture before with asp.net with BO,BLL,DAL,UI(asp.net webform).
I want to follow same with MVC4 in UI layer, in replace of webform.
I have generated my entity from database in DAL but now I am confused what is the use of BO layer now ?
Because all my entity are in DAL layer itself.
Also I will create Viewmodel classes in model folder so don't know how the flow will go now.
I am confused about the architecture now, please suggest if I am doing it correct or not, or is there any different approach I have to take for best practice,
PS: I don't wanna use single layer in my project. I think EF save our time by creating BO classes and enable sp used as functions and we don't have to use Ado.net repetitive code again and again.
As finally no help I got from 29 views counts, So I opted to create BO again from my project, DAL consist of E.F and UI have Viewmodels and ViewmodalList.
In your case, DAL should .edmx files. And if you want to use repository patterns you can add there.
In Business Logic layer, you will add your services. Where you are actually performing database operations.
You can declare another layer for interfaces as well. Or you can use Business logic layer for it.
Bit of a noob question I'm afraid. I have an ASP.NET Core Web Application, which uses Individual User Account for authentication so it's brought in the Identity bits and created me migrations and an "ApplicationDbContext" for all of the user/roles stuff. I moved the Data related classes, migrations etc into a separate class project to separate it from the MVC project, but other than that it's out-of-the-box. All good. My DB has been created by migrations and I can register and login.
Now, I'm coming to build my app, and I'm not sure where is best to add my entities. The question is should I
1) Add my entities to the "ApplicationDbContext" which was created for me by Visual Studio?
2) Create a second DbContext instance in my Data project?
3) Something else?
I understand that if my app was to use separate databases for authentication and domain data then it would be a no brainer and option 2 would be the answer. But this app is very simple with one database, so I'm wondering if I may be able to get away with option 1.
The ApplicationDbContext that was created for me inherits from IdentityDbContext. I don't know if that has other implications if I were to go with option 1 and add my entities to ApplicationDbContext.
I did start down the road of option 2, but quickly found that things like Add-Migration wouldn't work when the project had 2 contexts. I found that the Add-Migration command now has a -context switch which can be used to tell the command which context to use, but I'm a bit concerned that I will run into other issues. Particularly I am going to be using VS Team Services to build and deploy the app to Azure using continuous deployment, and I don't know if those build and release features will cope with the multiple DB contexts.
Any advice from somebody in the know? This has got to be a pretty standard requirement, right?
Hence your app is very simple one,you can use ApplicationDbContext for your domain models as well.That means, I would like to recommend you to use single context class for your app.If you do so,you can easily manage your business requirements (i.e. any relations between your classes) with the users and Roles on the IdentityDbContext context.
I've started an ASP.NET Web App project using the template for an Azure Mobile Service and tried to create my model the Model-First approach.
After generating my database from my finished model I proceeded to add a TableController class for one of my entities to test my project.
But when I tried to make a POST request I got this message:
Model compatibility cannot be checked because the DbContext instance
was not created using Code First patterns. DbContext instances created
from an ObjectContext or using an EDMX file cannot be checked for
compatibility.","exceptionType":"System.NotSupportedException
Is it not possible to create an Azure Mobile Service with Model-First at all? What are my options if I want to use the Model-First approach?
If it's not an existing model, I would switch to Code First. It is possible to use Model First, but it requires more manual configuration.
If you have an existing model, see this tutorial on how to add the right system properties and map to data transfer objects: http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/
This question already has answers here:
Ioc/DI - Why do I have to reference all layers/assemblies in application's entry point?
(4 answers)
Closed 9 years ago.
I have a Visual Studio solution that has the following:
MyProject.Domain - POCOs.
MyProject.Data - ORM (Entity Framework DbContext).
MyProject.Services - Wrapper around the DbContext. Contains classes that perform business logic.
MyProject.Web - MVC app that uses the service layer.
For my MVC app, I am using Ninject to inject services into my MVC controllers. The binding in my MVC project looks like this:
kernel.Bind<ISomeService>().To<SomeService>();
This part is working fine. However, I would further like to inject an IDbContext into my services, and the examples I see online are a little confusing. The examples I see online look make the above code look like this:
kernel.Bind<ISomeService>().To<SomeService>();
kernel.Bind<ISomeDbContext>().To<SomeDbContext>();
Now, since SomeDbContext is located in MyProject.Data, this means that I have to add a reference to the ORM project in my MVC project. This works, but this seems to defeat the purpose of IoC. The MVC project shouldn't "know" about the ORM, right? Why isn't this considered poor design? What is the proper way to manage this dependency?
If you configure all your dependencies in the MVC project, it is needed to have reference to project that holds implementations.
I am OK with this solution and if it is really needed to avoid this kind of reference you can try the following approach.
I am not familiar with Ninject, but in CastleWindsor it is possible to write installers anywhere (within any assembly) and then invoke them during type registration process from your MVC project. There is a SO question that might help you: Convert this Castle Windsor Installer to Ninject to register all repositories.
There won't be dirrect reference to ORM project from MVC project if you add a project with "installers" that knows about ORM project and its interfeces. In this case your MVC project should know about DataAccess layer interfaces and the project with installers only.
I currently generate XML from my single source of truth and save it as an Entity Framework EDMX file and then use the EntityClassGenerator object to create the classes from the diagram. Is there a way to generate the classes without having to create the XML file first?
I haven't heard back from Ladislav Mrnka, so I'll put his comment here as an answer. Using the Entity Framework's new Code-First, I can have a code-centric development workflow where my generator will create POCOs and a custom DbContext, then my database will be generated from the POCOs using convention instead of configuration. No need for an EDMX at all!
Here's a good explanation of it: http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx
EF needs the metadata from the EDMX at runtime. Even if you could use CodeModel or something to generate the entity classes, they would be useless to the EF runtime without the metadata describing the storage model, mapping etc.
Fabio Scopel has a webcast on youTube where he shows this Beta Tool (back then) called Entity Framework Reverse Engineer.
Check the link Entity Framework 5.0 - Code First Reverse Engineering existing DataBase