Entity Framework 6 - Database First - String Length Attribute missing - c#

I am using EF6 database first to create my EF 6 DB Context. The POCOs that are created are missing the property attributes such as "StringLength" or "Required". I have been on projects before where these were created for me and thought it was base functionality. Where can I find documentation on how to configure this behavior?
From a new Winform application I added a new ADO.Net Entity Data Model to my project and followed the wizard for "EF Design from database".
Environment
New .Net Framework 4.72 Winform app
EF 6
C#

I found out what happened. By default Scaffold-DBContext's parameter DataAnnotations defaults to use Fluent API. Fluent API configures the properties using function chaining in the OnModelCreating method of the context. The DataAnnotations parameter is a switch, so just specifying the parameter will change the engine to use Data Annotations.
This is important to be because I use the EF Models outside of EF as well. There is a big benefit using them with ASP.net because it will use the annotations during request to validate the models.

Related

ASP.NET Core API model concept

I'm working on a sample app that exposes a restful API. I'm also using EF core to map entities mkd to a SQLite db.
Each entity has an ID property that is auto-generated and is unique.
The api expose a method for the creation of an entity by providing a model object. At the moment I'm using the same model that is mapped to EF Core (with the ID field included).
I honestly don't think that exposing the ID field during the creation of an entity is a good practice, so I thought to create another model without the ID field that will be passed to the create method and then map it the model that EF is using.
Is it a good practice or is it too overkill?
Am I missing something?
What you are essentially doing is the use of a DTO/ViewModel instead of the model object directly, and that is considered a good practice. It saves you from the problem of over-posting as well. Alternatively, you can choose to Bind specific properties as well - refer to https://www.hanselman.com/blog/ASPNETOverpostingMassAssignmentModelBindingSecurity.aspx

Why can't i use DataAnnotations KeyAttribute for composite keys?

EntityFramework Core restricts using KeyAttribute for defining composite keys.
But what is the reason for this limitation? I have found a solution for this without using Fluent api.
According to this GitHub Issue it is in the backlog to be implemented in the upcoming version of EF Core. And from this GitHub Issue, here is the explanation that you were looking for:
The reason is that it used a combination of Key and Column for ordering, but column ordering is not the same thing as key ordering, and so this caused confusion and issues in EF6. Changing existing data annotations is also costly because of their relationship to .NET Core and .NET Framework.
However, we see the value in being able to this and other common configuration using attributes, since the jump to the fluent API can be a big one. Therefore, we will investigate creating a new library/package with additional new EF configuration attributes that can be used alongside data annotations. This would be one of the things that would be implemented there.
However, According to this announcement, from ASP.NET Core 3.0 you can use all the features of EF 6.x with ASP.NET Core as there will be EF 6.3 on .NET Core >= 3.0

Where is EnglishPluralizationService in EF Core 2.0?

EF Core 2.0 still has its conventions and it can change plural to singular and singular back to plural. Thus it definitely has the pluralization service built into it.
Yet I can't find this service to use it for other purposes.
In EF 6, I would write:
using System.Data.Entity.Infrastructure.Pluralization;
// Then somewhere in code
var englishPluralizationService = new EnglishPluralizationService();
var singularCat = englishPluralizationService.Singularize("Cats");
EF Core 2.0 still has its conventions and it can change plural to singular and singular back to plural.
Thus it definitely has the pluralization service built into it.
No, it can't do that and has no integrated pluralization service.
As explained in Table Mapping section of the documentation, it uses a simple convention:
By convention, each entity will be setup to map to a table with the same name as the DbSet<TEntity> property that exposes the entity on the derived context. If no DbSet<TEntity> is included for the given entity, the class name is used.
EF Core 2.0 introduced IPluralizer service that is used to singularize entity type names and pluralize DbSet names during scaffolding, but as stated in the link
this is just a hook where folks can easily plug in their own pluralizer
Shorty, there is no such service. For "other needs" you have to use your own or 3rd party pluralizer.
There is no built in pluralization in EF Core, but you can hook in for example the Inflector package: https://www.nuget.org/packages/Inflector/
I do that in "EF Core Power Tools" - see https://github.com/aspnet/EntityFrameworkCore/commit/dda3f43c046c2464f4813fdbb4261a6146aa4432 for more info
#bricelam, a member of the EF Core team, has published this blogpost, where he suggests using his Bricelam.EntityFrameworkCore.Pluralizer package, that is based on the EF Core pluralization service.

Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs

I will be using Solr search server with my ASP.NET 4.5 application. I've already installed SOLR on my Windows 8 laptop computer. According to SolrNet this documentation, I need to use specific attributes on my POCOs.
The thing is that I am using Entity Framework and my classes are auto generated. Is there an option to assign those type of Solr attributed and also make sure that they are presistant and won't be erased if your suggested solution is based on editing the template (.tt) file.
I want to use Entity Framework, but if it is not possible, I will just copy the pocos and create the classes myself with those Attributes. But I prefer searching for a solution that will allow me to use solrnet with Entity Framework. Thanks.
I would suggest that you create separate classes that map to your Solr Index schema, as typically the structure of your EF classes and your index schema will not be identical. This way you have a clean separation between your persistence classes (those auto-generated by EF) and your index mapping classes and can control how the mapping between the two occurs. I recommend the use of AutoMapper to assist with translating your objects from EF to Solr and back again as needed.

Force Entity Framework 5 to use datetime2 data type

Is it possible to globally set Entity Framework DbContext to use datetime2 for all properties that are System.DateTime when using Code-First model?
I can do this for each column by using HasColumnType() method, but for an existing codebase I would like a global solution.
Since EF6 has been out for quite a while now and this question still shows up in searches, here is the way to use custom conventions to set the SQL type. Within OnModelCreating method in your DbContext class do:
modelBuilder.Properties<DateTime>()
.Configure(c => c.HasColumnType("datetime2"));
Not in EF5 but EF6 (currently in alpha version) allow this with custom conventions. For EF5 you would need some custom convention based framework based on reflection which would add HasColumnType calls to model builder through reflection - check for example EF Code First Extras (it claims to have support for pluggable conventions).

Categories