Automapper, Entity Framework Core and multiple nested collections - c#

My database has two tables - RuleGroups and Rules. My Entity Framework classes are the following:
public class RuleGroup
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Rule> Rules { get; set; }
}
public class Rule
{
[Key]
public Guid Id { get; set; }
public Guid RuleGroupId { get; set; }
public string Name { get; set; }
public ICollection<Condition> Conditions { get; set; }
[ForeignKey("RuleGroupId")]
public virtual RuleGroup RuleGroup { get; set; }
}
[NotMapped]
public class Condition
{
public Guid Id { get; set; }
public string Name { get; set; }
}
Class Condition is not mapped because it is being serialized and stored as JSON in Rule Table (using this example)
My DTOS are the following:
public class UpdateRuleGroupDto
{
public string Name { get; set; }
public ICollection<UpdateRuleDto> Rules { get; set; }
}
public class UpdateRuleDto
{
public string Name { get; set; }
public ICollection<UpdateConditionDto> Conditions { get; set; }
}
public class UpdateConditionDto
{
public string Name { get; set; }
}
In my Startup.cs I initialize Automapper :
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<UpdateRuleGroupDto, RuleGroup>();
cfg.CreateMap<UpdateRuleDto, Rule>();
cfg.CreateMap<UpdateConditionDto, Condition>();
}
I have an API controller endpoint that accepts JSON PATCH document to make changes to data stored in database.
public IActionResult Patch(Guid ruleGroupId, [FromBody]JsonPatchDocument<UpdateRuleGroupDto> body)
{
RuleGroup ruleGroupFromRepo = _deviceRules.GetRuleGroup(ruleGroupId);
UpdateRuleGroupDto ruleGroupToPatch = Mapper.Map<UpdateRuleGroupDto>(ruleGroupFromRepo);
// Patching logic here
Mapper.Map(ruleGroupToPatch, ruleGroupFromRepo);
context.SaveChanges();
return NoContent();
}
The problem:
When changes are made/saved, Rules in Rule table change their/get new GUID.
Example, say we have this data in 2 Tables.
RuleGroup Table
[Id][Name]
[ddad5cac-e5a1-4db7-8167-66a6de3b8a0c][Test]
Rule Table
[Id][RuleGroupId][Name][Condition]
[17c38ee8-4158-4ecc-b893-97786fa76e13][ddad5cac-e5a1-4db7-8167-66a6de3b8a0c][Test][[{"Name":"Test"}]]
If I change field [Name] to a new value, Rules Table will look like this.
Rule Table
[Id][RuleGroupId][Name][Condition]
[ba106de8-bcbc-4170-ba56-80fe619cd757][ddad5cac-e5a1-4db7-8167-66a6de3b8a0c][Test2][[{"Name":"Test"}]]
Note that [Id] field has now a new GUID.
EDIT
#Gert Arnold made me realize that I'm not attaching entities.
I ran the following code:
foreach (var item in ruleGroupFromRepo.rules)
{
var x = _context.Entry(item).State;
}
and all the states were Added and not modified. Now I just have to figure out how to do it properly.

Related

using entities as json objects in EF core

I have below model structure
public class A621
{
public Guid? AirflowSource { get; set; }
public string AirflowSourceName { get; set; }
public string Category { get; set; }
}
public class A170
{
public Guid? AirflowSource { get; set; }
public string AirflowSourceName { get; set; }
}
I am using above models in entities like as below
public class MechanicalData
{
public List<A621> A621 { get; private set; }
public List<A170> A170 { get; private set; }
public List<Lab> Lab { get; private set; }
}
public class MechanicalTypeData
{
public MechanicalData MechanicalData { get; set; }
}
public class SpaceType : IAEIMaster, IRevisionData
{
[Column(TypeName = "jsonb")]
public MechanicalTypeData MechanicalTypeData { get; set; }
public string Note { get; set; }
public bool IsApproved { get; set; }
}
you can see that i am using this entity MechanicalTypeData in spacetype entity as json column and these entities A621 and A170 are child entities of MechanicalData ..
Now i would like to use A621 and A170 as a individual lookup tables and these are going to be really existing in DB. I am using EF core code first approach while creating tables.
my question is can i use A621 and A170 for both purposes, by both I mean as individual lookup tables and adding child objects to json that is going to be sit in column as json object.
Could any one throw some light on this or any other better approach as well.
many thanks in advance

Entity Framework Core default values for missing columns

I have a sqlite database which has some tables and columns like the following:
int Id
text Name
text Comment
...
And my object in my project looks like this:
Public Class Entry {
public int Id { get; set; }
public String Name { get; set; }
public String Comment { get; set; }
public String Additional { get; set; }
}
This can happen, because my programm need to handle different versions of the database.
EF Core now trys to access the Additional field of the database but returns an error that it cannot find the field. (Expected behaviour)
Now my question is, if there is a way to ignore this error and return a default value for the property?
I could bypass the error by making the properties nullable. But i don't want to check each property with .HasValue() before accessing it. Because the real database has 50+ columns in the table.
https://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx
Put NotMapped as an attribute on the Additional field:
using System.ComponentModel.DataAnnotations.Schema;
Public Class Entry {
public int Id { get; set; }
public String Name { get; set; }
public String Comment { get; set; }
[NotMapped]
public String Additional { get; set; }
}
This tells EF that the field is not a column in the database.
I would advise you to split your domain object from that persisted dto object. That way you can have different dtos with different mappings. Now you can instantiate your domain object with your dto and decide inside your domain object what values are the correct default values.
public class Entry
{
public int Id { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public string Additional { get; set; }
}
public class EntryDtoV1
{
public int Id { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
}
public class EntryDtoV2
{
public int Id { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public string Additional { get; set; }
}
Now you only need to create some kind of factory that creates the correct repository depending on what database version you query.

How to map recipes with ingredients using AutoMapper

I have following RecipeModel, IngredientModel and RecipePartModel classes which represent the DTO classes for the frontend user:
public class RecipeModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string Description { get; set; }
public IEnumerable<RecipePartModel> RecipeParts { get; set; }
}
public class IngredientModel
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class RecipePartModel
{
public Guid Id { get; set; }
public IngredientModel Ingredient { get; set; }
public string Unit { get; set; }
public decimal Quantity { get; set; }
}
Here are my entity classes:
public class Recipe : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string Description { get; set; }
public virtual IEnumerable<RecipePart> RecipeParts { get; set; }
}
public class Ingredient : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
public virtual IEnumerable<RecipePart> RecipeParts { get; set; }
}
public class RecipePart : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public Ingredient Ingredient { get; set; }
public Recipe Recipe { get; set; }
public string Unit { get; set; }
public decimal Quantity { get; set; }
}
My question is - how can I map the Recipe to RecipeModel using AutoMapper? I tried something like this but I assume it is bad, because it just join all the RecipeParts for the whole database, am I correct?
public class DomainProfile : Profile
{
public DomainProfile()
{
CreateMap<Ingredient, IngredientModel>().ReverseMap();
CreateMap<Recipe, RecipeModel>()
.ForMember(x => x.RecipeParts, opt => opt.MapFrom(src => src.RecipeParts));
}
}
To answer your question about how to use AutoMapper to map a type to another type, there are many ways of doing this. Documentation is here: http://docs.automapper.org/en/stable/Getting-started.html.
I wrote a console app and got it working in the quickest way I know possible using your code. When I debug this, and check inside recipeModel, it references a list of RecipePartModels with a single RecipePartModel. Inside that RecipePartModel, it references an IngredientModel.
static void Main(string[] args)
{
var profile = new DomainProfile();
Mapper.Initialize(cfg => cfg.AddProfile(profile));
var recipe = new Recipe
{
RecipeParts = new List<RecipePart>
{
new RecipePart()
{
Ingredient = new Ingredient()
}
}
};
var recipeModel = Mapper.Map<Recipe, RecipeModel>(recipe);
Console.ReadKey();
}
To answer your concern about getting all recipes from the database, if you're using Entity Framework, it depends on if you have lazy loading turned on. Lazy loading ensures that, when you get a recipe from the database, the recipe parts will not be loaded. They will only be loaded when you access the recipe part directly later on in the program flow. Lazy loading is turned on by default so this is the default behaviour. If you turn it off, you've enabled eager loading which loads all recipe parts and in turn their ingredient.
This might help: http://www.entityframeworktutorial.net/lazyloading-in-entity-framework.aspx.
There is nothing bad about this mapping. In fact you don't even need the ForMember call as this is the default convention. The mapping will simply convert each element in the entity child collection to a corresponding model object.
Of course, whether you load your entities in an efficient manner is another matter. If you load a large amount of Recipe entities, and lazy load the RecipeParts collections for each, you will have a major "SELECT N+1" problem. But this is not the fault of AutoMapper.

How to model with EntityFramework extensible fields for the entities

I have the following requirement, on my app the Entities will come with some fields, however the user needs to be able to add additional fields to the entity and then values for those fields.
I was thinking something like this but I am not sure if it would be a good approach or not.
The base class is an entity (Not sure which fields I need to add here)
public class Entidad
{
}
Then the Company Class will inherit from Entity
public class Empresa : Entidad
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
public string NIT { get; set; }
public string NombreRepresentanteLegal { get; set; }
public string TelefonoRepresentanteLegal { get; set; }
public string NombreContacto { get; set; }
public string TelefonoContacto { get; set; }
public ICollection<CampoAdicional> CamposAdicionales { get; set; }
}
As you can see there is an ICollection of additional fields. that class would have the fieldname, type and id
public class CampoAdicional
{
[Key]
public int Id { get; set; }
public string NombreCampo { get; set; }
public Tiposcampo TipoCampo { get; set; }
}
and then the field value would be something like this:
public class ValorCampo
{
public Entidad Entidad { get; set; }
public CampoAdicional Campo { get; set; }
public string ValorTexto { get;set ; }
public int ValorNumerico { get; set; }
}
However I am not sure if this is the correct model classes for my scenario and whether it would create the tables correctly.
EF works with lazy load so at least there are several "virtual" missings.
In all properties that does not use primitive types and in collections.
Can you extend more than one entity with additional fields? If so you need that ValorCampo contains the entity (Entidad) but the entity should have the Id so you need to move the Id from Empresa to Entidad. Otherwise you need ValorCampo should refer to Empresa not to Entidad

Entity framework, issue saving data in many-to-many relationship

I have issue saving data in many-to-may relationship between two tables that breaks by introducing another table in between, containing primary keys of both. I have code first existing database approach along with repository pattern and unit of work in MVC application
and here is my model classes
Navigation_Functions
public class Navigation_Functions
{
public Navigation_Functions()
{
}
[Key]
public int Function_ID { get; set; }
[StringLength(250)]
[Required(ErrorMessage = "Required Title")]
[Display(Name = "Function Title")]
public string FunctionName { get; set; }
[Required(ErrorMessage = "Required Hierarchy Level")]
[Display(Name = "Hierarchy Level")]
public int Hierarchy_Level { get; set; }
public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
}
}
Navigation_FunctionController Model
public class Navigation_FunctionController
{
public Navigation_FunctionController()
{
}
[Key]
public int ControllerID { get; set; }
[StringLength(250)]
[Required]
public string ControllerName { get; set; }
public ICollection <Navigation_Functions> Navigation_Functions { get; set; }
}
Junction Model
[Table("Navigation_FunctionInController")]
public class Navigation_FunctionInController
{
public Navigation_FunctionInController()
{
}
[Key]
public int FunctionInController_ID { get; set; }
[Key]
[ForeignKey("Navigation_Functions")]
public int Function_ID { get; set; }
[Key]
[ForeignKey("Navigation_FunctionController")]
public int ControllerID { get; set; }
public Navigation_FunctionController Navigation_FunctionController { get; set; }
public Navigation_Functions Navigation_Functions { get; set; }
}
I have generic repository for CRUD operation
public void InsertEntity(TEntity obj)
{
_DbSet.Add(obj);
}
My ViewModel
public class FunctionsNavigation_ViewModel
{
public Navigation_Functions _Navigation_Functions { get; set; }
public Navigation_FunctionController _Navigation_FunctionController { get; set; }
}
public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
{
using (var _uow = new FunctionsNavigation_UnitOfWork())
{
try
{
var _navigationFunction = _obj._Navigation_Functions;
_navigationFunction.Navigation_FunctionController = new List<Navigation_FunctionController>();
_navigationFunction.Navigation_FunctionController.Add(_obj._Navigation_FunctionController);
_uow.Navigation_Functions_Repository.InsertEntity(_navigationFunction);
_uow.Save();
}
catch
{
}
}
}
if I remove following line from above code then it save new Navigation_Functions
_navigationFunction.Navigation_FunctionController.Add(_obj._Navigation_FunctionController);
following is screen shot from debug code.
I am wondering if my ViewModel are correct? secondly How Entity Framework knows that it need to put primary keys of two tables in Navigation_FunctionInController?
When your model looks like this...
public class Navigation_Functions
{
...
public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
}
public class Navigation_FunctionController
{
...
public ICollection <Navigation_Functions> Navigation_Functions { get; set; }
}
...so without a Navigation_FunctionInController class, the junction table in the database (Navigation_FunctionInController) is not represented in the class model. If you have this model code first, EF will create a junction table itself. If you work database first, EF won't create a junction class and in the diagram you will see a pure many to many association, like this: *--*. But this only happens if the junction table only contains the two foreign keys, both of which comprise a compound primary key.
In your model you have an explicit junction class (probably because the table has a primary key field besides the two foreign keys). This means that the many to many association turns into a 1:n:1 association. The class model should essentially look like this...
public class NavigationFunction
{
...
public ICollection<NavigationFunctionInController> NavigationFunctionInControllers { get; set; }
}
public class NavigationFunctionController
{
...
public ICollection <NavigationFunctionInController> NavigationFunctionInControllers { get; set; }
}
public class NavigationFunctionInController
{
public int FunctionInControllerID { get; set; }
[ForeignKey("NavigationFunction")]
public int FunctionID { get; set; }
[ForeignKey("NavigationFunctionController")]
public int ControllerID { get; set; }
public NavigationFunctionController NavigationFunctionController { get; set; }
public NavigationFunction NavigationFunction { get; set; }
}
Note that the foreign key fields don't have to be part of the primary key (also not that I prefer the singular name Navigation_Function and plural names for collections, and no underscores in names).
So what happens in your code is that you have a 1:n:1 association, but you try to manage it like a m:n association by adding items directly to _navigationFunction.Navigation_FunctionController(s) . But EF doesn't track that collection, and the items are not saved.
Instead you have to create a junction class instance...
var nfic = new NavigationFunctionInController
{
NavigationFunction = obj.NavigationFunction,
NavigationFunctionController = obj.Navigation_FunctionController
};
...and save it through your repositories and UoW.

Categories

Resources