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
Related
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
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.
I want to map one table to two unrelated entities: EntityBasic and EntityAdvanced.
EntityAdvanced has extra business logic that I don't need for this one feature, and I would like to make a new Entity that only has fields from the table.
MyTable:
MyTableId : Guid
ParentId : Guid
Name : string
Description : string
Type : int
EntityBasic:
[Table("MyTable")]
public class EntityBasic
{
[Key]
public Guid MyTableId { get; set; }
public Guid ParentId { get; set }
public string Name { get; set; }
public string Description { get; set; }
public int Type { get; set; }
[ForeignKey("ParentId")]
public virtual List<EntityBasic> Entities{ get; set; }
}
EntityAdvanced:
[Table("MyTable")]
public class EntityAdvanced
{
private List<EntityAdvanced> _entities;
private List<Filter> _filters;
[Key]
public Guid MyTableId { get; set; }
public Guid ParentId { get; set }
public string Name { get; set; }
public string Description { get; set; }
public int Type { get; set; }
[ForeignKey("ParentId")]
public virtual List<EntityAdvanced> Entities
{
get { //Some complicated getter }
set { //Some complicated setter }
}
[NotMapped]
public string ImageUrl
{
get { //Some complicated getter }
set { //Some complicated setter }
}
public void SetFilters(//Some parameters)
{
//Some logic
}
}
When I do this i get this error:
The entity types 'EntityAdvanced' and 'EntityBasic' cannot share table 'MyTable' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.
Is there a way to do what I want?
As a base start, your EntityAdvanced should inherit EntityBasic since they share the same base set of properties. You don't need to rewrite them. Note the extends EntityBasic.
[Table("MyTable")]
public class EntityBasic
{
[Key]
public Guid MyTableId { get; set; }
public Guid ParentId { get; set }
public string Name { get; set; }
public string Description { get; set; }
public int Type { get; set; }
[ForeignKey("ParentId")]
public virtual List<EntityBasic> Entities{ get; set; }
}
[NotMapped]
public class EntityAdvanced : EntityBasic
{
//[NotMapped]
public string ImageUrl
{
get { //Some complicated getter }
set { //Some complicated setter }
}
public void SetFilters(//Some parameters)
{
//Some logic
}
}
Using inheritence, List<EntityBasic> Entities could reference EntityAdvanced objects so you don't need anymore to declare:
[ForeignKey("ParentId")]
public virtual List<EntityAdvanced> Entities
{
get { //Some complicated getter }
set { //Some complicated setter }
}
You can get usefull information about implementing inheritence with Entity Framework here.
Happy coding!
I think you can use the ability "Table Splitting" of Entity Framework 6,
Have a look at the example here: https://www.c-sharpcorner.com/UploadFile/ff2f08/table-splitting-in-entity-framework-6-code-first-approach/
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.
This has been puzzling me for quite some time but I keep getting an invalid identifier error when my entity framework tries to execute an oracle query. The classes in question are the following:
public class Projectship : ModelTrackingBase
{
[Column("PROJECTID")]
public long ProjectId { get; set; }
[Column("VISITID")]
public long VisitId { get; set; }
public virtual Bpid Bpid { get; set; } //new
public virtual Visit Visit { get; set; }
}
and
public class Bpid : EntityIdBase
{
[Column("BUDPRJID")]
[MaxLength(38)]
public string BPId { get; set; }
[Column("DESCRIPTION")]
[MaxLength(255)]
public string Description { get; set; }
[Column("CSTOBJ")]
[MaxLength(255)]
public string Custobj { get; set; }
[Column("PVID")]
[MaxLength(255)]
public string Pvid { get; set; }
public virtual ICollection<Projectship> Projectships { get; set; }
public IEnumerable<Visit> Visits
{
get { return Projectships.Select(p => p.Visit); }
}
[NotMapped]
public string DisplayName
{
get { return string.Format("{0}: {1}", BPId , Description); }
}
}
Now the EntityIdBase has the following:
public class EntityIdBase : EntityBase
{
[Column("ID")]
public long Id { get; set; }
}
It tries to keep on looking for a column Bpid_Id in the query. Does someone have any idea?
Bpid_id is created by EF because it can't automatically determine the relationship. Try adding the annotation:
[ForeignKey("ID")]
public virtual Bpid Bpid { get; set; } //new
You have specified a navigation property in the Projectship class
public virtual Bpid Bpid { get; set; }
You have not specified a foreign key to go with the navigation property so Entity Framework has chosen one, and it has chosen the name Bpid_Id. And it should be in the database. It should not be "non-existent".
You will probably find it easier to use Entity Framework if you add a foreign key like this:
public int BpidId { get; set; }
References:
Why does Entity Framework Reinsert Existing Objects into My Database?
Making Do with Absent Foreign Keys