I have a problem in data transformation with AutoMapper that completely skip parent class after mapping to Dto when a member of parent is NULL.
Parent class:
public class Parent : BaseEntity
{
public string Something { get; set; }
public string SomthingElse { get; set; }
public Member Member { get; set; }
}
Parent Dto class:
public class ParentDto : BaseEntity
{
public string Something { get; set; }
public Member Member { get; set; }
}
Member class:
public class Member : BaseEntity
{
public string MemberName { get; set; }
public int? SomethingId { get; set; }
}
When we mapped parent class to Dto and if Member was not NULL all thing work like a charm but problem is when the Member is NULL parent model completely skipped in result. I have no exception or error, just parent class that have NULL Member was skipped in results.
Result generated by LINQ:
var results = await query
.Skip(pageNumber * pageSize)
.Take(pageSize)
.ProjectTo<ParentDto>()
.ToArrayAsync(ct);
Mapping created like this:
CreateMap<Parent, ParentDto>();
Packages version:
AutoMapper 6.1.1
Microsoft.AspNetCore 2.0.0
Microsoft.EntityFrameworkCore.InMemory 2.0.0
Did you have any idea that help me what is the problem and how can i solve it?
Related
I have two classes (Parent, Child) in ASP.Net Core and I'm using code first approach, my real project is more complex than that, so i have to use this method to migrate to database.
The issue here is when I'm defining the relations in Db Context class i face this error noting that I'm following this Microsoft document https://learn.microsoft.com/en-us/ef/core/modeling/relationships, and you can find the main class below:
Error: Cannot implicitly convert type 'System.Collections.Generic.List<logintest.Models.Chlid>' to 'System.Collections.Generic.IEnumerable<logintest.Models.Child>'. An explicit conversion exists (are you missing a cast?)
public class Parent
{
public int Id { get; set; }
public List<Chlid> Childs { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Childs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Child>()
.HasOne(p => p.Parent)
.WithMany(b => b.Childs)
.HasForeignKey(b => b.ParentId);
}
}
I would like to map the following hierarchy of entities to the TestViewModel class. I have TestViewModel class with the same member names and perhaps I will add more members to the view model. I am using AutoMapper.
public class TestProfile : Profile
{
public TestProfile ()
{
CreateMap ??????
}
}
public class Test
{
public List<Test1> Tests1 { get; set; }
public int TestId { get; set; }
}
public class Test1
{
public int Test1Id { get; set; }
public string Name { get; set; }
public List<Document> Documents { get; set; }
}
public class Document
{
public int DocumentId { get; set; }
public DateTimeOffset? ChangeDate { get; set; }
public List<Payload> Payloads { get; set; }
}
public class Payload
{
public string PayloadName { get; set; }
}
You didn't tell us what your TestViewModel class looks like, and whether you also have DocumentViewModel, PayloadViewModel, etc. Typically if you are mapping to a another set of classes that have the same naming and structure, like a set of ViewModels, you will want to have a configuration like this:
public class TestProfile : Profile
{
public TestProfile()
{
CreateMap<Test, TestViewModel>();
CreateMap<Test1, Test1ViewModel>();
CreateMap<Document, DocumentViewModel>();
CreateMap<Payload, PayloadViewModel>();
}
}
This will map all like-named properties between the two sets of classes. If your TestViewModel shares the same child entities, then you only need the first line.
I am trying to change the way asp.net generates its identity tables, trying to base the generation on an Id of int instead of a Guid(string), also adding a different schema(instead of dbo -> Security) and a QueryFilter for all my entities, in that case I created for each class a Mapping but will illustrate the idea with just one that is giving me the error.
public class AspNetRole : IdentityRole<int>, IEntityBase
{
public bool IsDeleted { get; set; }
public string CreatedBy { get; set; }
public string UpdatedBy { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
}
public interface IEntityBase
{
int Id { get; set; }
bool IsDeleted { get; set; }
string CreatedBy { get; set; }
string UpdatedBy { get; set; }
DateTime? CreatedOn { get; set; }
DateTime? UpdatedOn { get; set; }
}
The mapping class with the QueryFilter:
public class AspNetRoleMap : IEntityTypeConfiguration<AspNetRole>
{
public void Configure(EntityTypeBuilder<AspNetRole> builder)
{
builder.ToTable(name: "AspNetRole", schema: "Security");
builder.HasQueryFilter(app => !app.IsDeleted);
}
}
The DbContext:
public class AspNetSecurityDbContext : IdentityDbContext<AspNetUser, IdentityRole<int>, int>
{
public AspNetSecurityDbContext(DbContextOptions<AspNetSecurityDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new AspNetRoleMap());
}
}
Once I run the migration I am getting the following error:
The filter expression 'app => Not(app.IsDeleted)' cannot be specified
for entity type 'AspNetRole'. A filter may only be applied to the root
entity type in a hierarchy.
I tried this approach https://github.com/aspnet/EntityFrameworkCore/issues/10259 but still getting more errors
builder.HasQueryFilter(app => !((IEntityBase)app).IsDeleted);
The problem has nothing in common with EF Core query filter, but the incorrect base generic IdentityDbContext argument. Here
: IdentityDbContext<AspNetUser, IdentityRole<int>, int>
you are passing IdentityRole<int>, which in the base OnModelCreating will be configured as entity, hence EF Core will map your AspNetRole entity using TPH inheritance strategy, which along with the additional discriminator column introduces additional constraints, like the query filter exception you are getting.
To fix that, pass the correct generic type argument which in this case is the custom AspNetRole class:
: IdentityDbContext<AspNetUser, AspNetRole, int>
In case you create other custom entities inheriting the generic IndentityXyz<> classes, take a look at the other base IdentityDbContext classes having more generic type arguments, and select the one that allows you the pass all your custom identity derived types.
As entity framework states, "Code first", here we go with the code first...
public class BaseModel
{
[Key]
public Guid Id { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateChanged { get; set; }
public BaseModel()
{
this.Id = Guid.NewGuid();
this.DateCreated = DateTime.Now;
this.DateChanged = DateTime.Now;
}
}
public class Association: BaseModel
{
public string Name { get; set; }
public string Type { get; set; }
public virtual List<Rule> Rules { get; set; }
public Association()
: base()
{
}
}
public class Rule: BaseModel
{
[ForeignKey("Association")]
public Guid AssociationId { get; set; }
//[Required]
public virtual Association Association { get; set; }
//[Required]
public string Name { get; set; }
public string Expression { get; set; }
public virtual List<Action> Actions { get; set; }
public Rule()
: base()
{
}
}
public class Action: BaseModel
{
public string Name { get; set; }
public string ActionType { get; set; }
[ForeignKey("Rule")]
public Guid RuleId { get; set; }
public virtual Rule Rule { get; set; }
public int Order { get; set; }
public Action()
: base()
{
}
}
So these are my four model classes that are using entity framework code first.
Each inherit from the baseclass, so they all have an Id Guid as Primary Key.
An Association has a list of rules. (Rule has FK to Association)
A Rule as has a list of actions. (Action has FK to Rule)
What I would like to do is only change and save the most upwards class = Association.
For example when deleting a rule, I would like this code to work:
public ActionResult DeleteRule(Guid assId, Guid ruleId)
{
Association ass = this.DataContext.Associations.FirstOrDefault(a => a.Id == assId);
ass.Rules.RemoveAll(r => r.Id == ruleId);
this.DataContext.SaveChanges();
return RedirectToAction("Index");
}
On the context.savechanges this is giving me this error:
'The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.'
This error also occurs when deleting an action.
Is there a way to change the most upper (Association) object AND ONLY changing things to this Association.
I DO NOT want to say context.Rules.remove(...) or context.actions.remove(...)
here's the source: http://server.thomasgielissen.be/files/mvctesting.zip
you need VS2012, all nuget packages are included in zip and you should be able to build and run the project.
Thanks in advance for your feedback!
Greetz,
Thomas
I you want to fix this issue, you should store your relations through junction tables. I don't think that you can achieve what you need, with this model.
However if you put a junction table(or entity) between your entities, you can easily remove child objects and update parent object.
For example, put a junction entity between Association and Rule:
public class AssociationRule: BaseModel
{
public Guid AssociationId { get; set; }
public Guid RuleId { get; set; }
[ForeignKey("AssociationId")]
public virtual Association Association { get; set; }
[ForeignKey("RuleId")]
public virtual Rule Rule { get; set; }
public Association()
: base()
{
}
}
Now, you can easily remove any rule from any association:
public ActionResult DeleteRule(Guid assId, Guid ruleId)
{
AssociationRule assr = this.DataContext
.AssociationRuless
.FirstOrDefault(ar => ar.AssociationId == assId && ar.RuleId == ruleId);
this.DataContext.AssociationRules.Remove(assr);
this.DataContext.SaveChanges();
return RedirectToAction("Index");
}
I have the following situation with fluent nhibernate:
public class Stuff
{
public Stuff()
{
Entities = new List<Entity>();
}
public virtual int Id { get; set; }
public virtual IList<Entity> Entities { get; set; }
}
public abstract class Entity
{
public virtual int Id { get; set; }
public virtual string Type { get; set; }
public virtual Stuff Stuff { get; set; }
}
public class Person : Entity
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class Animal : Entity
{
public virtual string Species { get; set; }
}
And then, i have the following code to use automap and generate these mappings:
var sessionFactory =
Fluently.Configure().Database(persistenceConfigurer).Mappings(
m =>
m.AutoMappings.Add(
AutoMap.Source(new Types(typeof(Entity), typeof(Person), typeof(Animal), typeof(Stuff))))
.ExportTo(#"e:\")).ExposeConfiguration(BuildSchema).BuildSessionFactory();
however, what's happening is that i get the following exception:
---> NHibernate.MappingException: Association references unmapped class: ConsoleApplication1.Models.Entity
if i make the entity class non abstract this works, however, i'd like to avoid having that table in the database but still maintain the hierarchy concept with the re
You need to add your auto mappings like this
AutoMap.AssemblyOf<Entity>(yourConfiguration).IgnoreBase<Entity>();
Not only will this ignore your Entity base class but you don't need to add each entity manually so long as each model inherits from Entity.