EF Core entity classes and Clean architecture - c#

I am building a new ASP.NET Core 5 MVC app. I want to use clean architecture as outlined in Microsoft's web app architecture ebook.
I am also studying eShopOnWeb sample application available here :
https://github.com/dotnet-architecture/eShopOnWeb
What I understand from the ebook and sample app is - EF Core entity classes (say Customer, Product, Order) will go inside ApplicationCore project's Entities folder. The DbContext will be in Infrastructure project.
My confusion is: is it alright to add data annotation schema attributes such as [Table], [DatabaseGenerated], and [Key] on these entity classes inside ApplicationCore project? If not, where should I add these data annotations?
Any advice in this regards is highly appreciated.
Thank you.

In the example of eShopOnWeb they separate Entities and configuration models. So, that means to have clean architecture you don't need annotations directly in these Entities.
You can use FluentAPI, as they used as well in the Infrastructure/Data/Config directory.
So, if you have a separate project for DataContext that is the best location for describing your entities with FluentAPI in this case.
More information about FluentAPI:
https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/types-and-properties

Related

Extending Entity Framework Models for SAAS

I am building an asp.net mvc application that is using Entity Framework 6. We have the challenge of building several implementations of this same application. So we have created a core library called MyApp.Core which contains the following:
DbContext
Models
Customer
Product
(other models)
Repositories
We have the need to extend models for different implementations of the application. For example we might want to put SomeProperty on the customer table for 1 customer and SomeOtherProperty for another customer.
How can we improve the structure so it doesn't break the EF code first migrations? Or cause any other issues?
Should we just have a unique ASP.net project for each customer that references the MyApp.Core? And should we reference those via a nuget package? Or something else like a git sub module?
Any suggestions on the organization of the custom implementations of this type of structure?
Your solution may be an IsA (Is-a, is a) database structure that can be created from your code-first model using the TPC method mentioned here:
http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines
You might create an abstraction such as ICustomer, or CustomerBase which contains a reference to a table containing the implementations of your application (is that Product?).
You might also transform your Customer class into a base class, and other classes with additional fields would inherit from it.
In either event, the article deals with bringing code first into a database model that can handle this kind of thing. You may also want to take a look at multi-tenant architecture, just to say you've done your homework. That's here: https://msdn.microsoft.com/en-us/library/aa479086.aspx

.Net api for generating entity framework models from database

Does the entity framework provides an API for .net (c#) to generate models from an existing database? I don't want to use EdmGen.exe, because a direct integration would be nice.
Maybe even the code which is used by the EdmGen.exe provides some information, because they also have to use some API functionality.
This is needed, because i want to compile the models dynamicaly with roslyn at runtime, which is currently working pretty well with some demo models.
Thank you.

Entity Framework & Code First With Partial Mapping of Tables

I am new to Code First. I am interested in using Code First going forward on my projects. I am using EF 6.xx. I will be creating several projects using an existing database but will be adding additional tables/views/stored procedures where necessary. Perhaps a silly question... Can I develop a library of POCOs that are tagged with the appropriate Fluent API tags and then pick and choose what Fluent API tagged POCO library classes I want to include in the OnModelCreating method for the particular project. I'm interested in re-using the same POCOs from project to project. Is this what others are doing or are they re-creating the POCOs in every project?
Thanks in advance,
Terry
You can certainly re-use the POCO classes between applications. If they are not referenced directly by your DbSet subclass or indirectly by another class that is already referenced then they won't be used by EF.
You can use attributes (what I think you mean by tags) on the various POCO classes as long as those attributes are the same between all projects that will use them - e.g. column name etc.
For stuff that changes between projects you'll definitely want to use Code First's fluent interface to configure them in the OnModelCreating.

Work flow using Code-First -vs- DB-First in starting a new project? EF6

When creating a new solution (typically ASP.Net MVC or API) I usually create a data access project and my web project. In the data access project I typically use EF6 and have been trying to use Code-First so that I can take advantage of the migrations feature.
In the data access project I create a model folder and then in that folder add my classes and my DBContext class. Then I generate my migrations and seed data. This all works ok.
What I was wondering are there more efficient ways of creating this
type of project?
Am I hampering my productivity by going code first?
I know I am not using code generation that the database first process
would use. Can I gain efficiencies by switching to DB first?
What are typical work flows that others use?
OR is there a hybrid approach that people find successful?
I understand that this has been addressed in other posts but my questions is more in context of EF6 and those all seem to be based on EF4.
TIA

Strategy for Silverlight and Entity Framework solution

I am new to Silverlight and am developing an ASP.NET web application that requires a Silverlight project to record webcam audio/video streams.
The solution consists of a library project containing business entities such as [User], [BillingInfo], etc., and an ASP .NET Web Application.
Since Silverlight does not support EF, how can I use the strongly-typed entity objects from within Silverlight? I do not need access to the Context object but will require access to Entity classes.
I use Code First so have to mark Entities with Annotations which Silverlight will not recognize since the DataAnnotations assembly is not referencable. Switching to model-first is also an option (albeit less preferable) if required.
Has anyone dealt with a similar scenario? What is the best way to get strongly-typed entities in Silverlight. Any articles or references would help as well. Thank you.
Technologies (upgrade is an option if required):
ASP .NET Web Application (.NET 4)
Entity Framework 5
Silverlight 5
I would recomend giving WCF RIA Services a try. This will simplify the data access for your application and provide you with strongly typed entities on the Silverlight client side.
Silverlight works very well with entity framework!
You could directly consume your entity through webservices as describe in this blog: http://geekswithblogs.net/berthin/archive/2011/05/29/ado_net_entityframework_from_silverlight.aspx
Or as Dave suggest, you could combine RIA services and Entity Framework, so you could use annotation http://blogs.msdn.com/b/brada/archive/2010/03/15/silverlight-4-ria-services-ready-for-business-exposing-data-from-entity-framework.aspx
The combination of Silverlight and Entity Framework create a really powerfull ready to use business logic.

Categories