Mapping ICollection to IEnumerable using automap issue - entity-framework

Stuck on a strange issue, that works using my get method but fails when returning iqueryable. Some limitation when using projection or iqueryable?
My code looks like below, simplified:
public class SimpleEntity
{
public long Id { get; set; }
public string Name { get; set; }
public virtual SimpleEntity Parent { get; set; }
public virtual ICollection<SimpleEntity> Children { get; set; }
public SimpleEntity()
{
Children = new List<SimpleEntity>();
}
}
public class SimpleEntityResponseDTO
{
public long Id { get; set; }
public string Name { get; set; }
public NameValueItem ParentReferral { get; set; }
public IEnumerable<NameValueItem> ChildReferrals { get; set; }
public NavigationFolderResponseDTO()
{
ChildReferrals = new List<NameValueItem>();
}
}
public class NameValueItem
{
public long Value { get; set; }
public string Name { get; set; }
}
The web api actions:
[HttpGet, Queryable]
public IQueryable<SimpleEntityResponseDTO> List()
{
//Generic crudservice returning an iqueryable based on Set<SimpleEntity>
return _crudService.QueryableList().Project().To<SimpleEntityResponseDTO>();
}
[HttpGet]
public HttpResponseMessage Get(long id)
{
SimpleEntity result = _crudSrv.Get(id);
if (result != null)
return Request.CreateResponse<SimpleEntityResponseDTO>(HttpStatusCode.OK, Mapper.Map<SimpleEntity , SimpleEntityResponseDTO>(result));
else
return Request.CreateResponse(HttpStatusCode.NotFound);
}
And now the mapping:
Mapper.CreateMap<SimpleEntity, SimpleEntityResponseDTO>()
.ForMember(to => to.ParentReferral, opt => opt.MapFrom(from => new NameValueItem { Name = from.Parent.Name, Value = from.Parent.Id }))
.ForMember(to => to.ChildReferrals, opt => opt.MapFrom(from => from.Children.Select(o => new NameValueItem {Name = o.Name, Value = o.Id}).ToList() ));
The parent mapping works no matter what. But the Children mapping is causing below issue.
When retrieving an object through the get method everything works, no matter wich entity i retrieve. When using List i get "Object reference not set to an instance of an object", "d__b.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n". Tried for example adding $filter=Id eq 5 (or whatever id) but results in same issue. Perhaps someone can hint me to what goes wrong here?

Related

Error when creating mappings with AutoMapper

I'm trying to map objects with AutoMapper. I've created the HTTP POST controller method, which should create the new Part object to database. It should add data to both entities, Part and PartAvailabilites. Database is already existing and is scaffolded by EF Core. The error I'm receiving is:
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration Part -> PartDto (Destination member list)PartManagement.Entities.Part -> PartManagement.Dto.PartDto (Unmapped properties:Balance)"
Does anyone know what could be the problem with this mapping? I tried to do the mapping in several ways but none of them is working.
Here is my mapping:
CreateMap<PartDto, PartEntity>()
.ForMember(dest => dest.FkPartAvailability,
opts => opts.MapFrom(src => new PartAvailabilities
{
Balance = src.Balance
}));
Example JSON request:
{
"name": "testPart",
"catalogNumber": 12345,
"balance": 10
}
Here are my entity classes:
public class Part
{
public long Id { get; set; }
public int PartNumber { get; set; }
public string Name { get; set; }
public PartAvailabilities FkPartAvailability { get; set; }
}
public class PartAvailabilities
{
public PartAvailabilities()
{
Parts = new HashSet<Part>();
}
public long Id { get; set; }
public decimal Balance { get; set; }
public ICollection<Part> Parts { get; set; }
}
public class PartDto
{
public string Name { get; set; }
public int PartNumber { get; set; }
public decimal Balance { get; set; }
}
This is Create method in the ManagementService class:
public async Task<PartDto> Create(PartDto request)
{
var part = _mapper.Map<PartDto, PartEntity>(request);
var createdPart = partRepository.Add(part);
await partRepository.UnitOfWork.SaveChangesAsync();
return _mapper.Map<PartDto>(createdPart);
}
And here is HttpPost method from controller:
[HttpPost]
public async Task<IActionResult> Part_Create([FromBody] PartDto request)
{
PartDto createdPart;
try
{
if (request != null)
{
createdPart = await _partManagementService.Create(request);
return Ok(request);
}
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
return Ok(new string[] { "Part created" });
}
The message is trying to tell you that there is no map between Part and PartDto. AM will create a map for you, but that map is not valid, because PartDto.Balance cannot be mapped. So you have to create the map and tell AM how to map Balance. Things might be easier to understand if you set CreateMissingTypeMaps to false.

Entity Framework circular dependency for last entity

Please consider the following entities
public class What {
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Track> Tracks { get; set; }
public int? LastTrackId { get; set; }]
public Track LastTrack { get; set; }
}
public class Track {
public Track(string what, DateTime dt, TrackThatGeoposition pos) {
What = new What { Name = what, LastTrack = this };
}
public int Id { get; set; }
public int WhatId { get; set; }
public What What { get; set; }
}
I use the following to configure the entities:
builder.HasKey(x => x.Id);
builder.HasMany(x => x.Tracks).
WithOne(y => y.What).HasForeignKey(y => y.WhatId);
builder.Property(x => x.Name).HasMaxLength(100);
builder.HasOne(x => x.LastTrack).
WithMany().HasForeignKey(x => x.LastTrackId);
Has you can see there is a wanted circular reference:
What.LastTrack <-> Track.What
when I try to add a Track to the context (on SaveChanges in fact):
Track t = new Track("truc", Datetime.Now, pos);
ctx.Tracks.Add(t);
ctx.SaveChanges();
I get the following error:
Unable to save changes because a circular dependency was detected in the data to be saved: ''What' {'LastTrackId'} -> 'Track' {'Id'}, 'Track' {'WhatId'} -> 'What' {'Id'}'.
I would like to say... yes, I know but...
Is such a configuration doable with EF Core ?
This is what I like to call the favored child problem: a parent has multiple children, but one of them is extra special. This causes problems in real life... and in data processing.
In your class model, What (is that a sensible name, by the way?) has Tracks as children, but one of these, LastTrack is the special child to which What keeps a reference.
When both What and Tracks are created in one transaction, EF will try to use the generated What.Id to insert the new Tracks with WhatId. But before it can save What it needs the generated Id of the last Track. Since SQL databases can't insert records simultaneously, this circular reference can't be established in one isolated transaction.
You need one transaction to save What and its Tracks and a subsequent transaction to set What.LastTrackId.
To do this in one database transaction you can wrap the code in a TransactionScope:
using(var ts = new TransactionScope())
{
// do the stuff
ts.Complete();
}
If an exception occurs, ts.Complete(); won't happen and a rollback will occur when the TransactionScope is disposed.
I encountered the same problem, but i solved it differently.
In my case, it was about a list of status and a reference to the last status. So with the following case :
public class What {
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Status> StatusList { get; set; }
public int? LastStatusId { get; set; }
public Status LastStatus { get; set; }
public void AddStatus(Status s)
{
StatusList.Add(s);
LastStatus = s;
}
}
public class Status{
public int Id { get; set; }
public int WhatId { get; set; }
public What What { get; set; }
}
In my program, i changed my code to use StatusList as an history that doesn't include the lastStatus, so :
public class What {
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Status> StatusHistory { get; set; }
public int? LastStatusId { get; set; }
public Status LastStatus { get; set; }
public void AddStatus(Status s)
{
if(LastStatus) StatusList.Add(LastStatus);
LastStatus = s;
}
public List<Status> GetStatusList(Status s) // If needed, a method, not a property because i got an error with lazyLoading
{
return new List<Status>(StatusHistory) { LastStatus}; // List of all status (history + last)
}
}
public class Status{
public int Id { get; set; }
public int? WhatId { get; set; }
public What What { get; set; }
}
and don't forget to put in your context IsRequired(false) on the foreignKey :
builder.HasMany(x => x.Status).
WithOne(y => y.What).HasForeignKey(y => y.WhatId).IsRequired(false);
Like this, no more circular reference.

Entity framework replaces delete+insert with an update. How to turn it off

I want to remove a row in database and insert it again with the same Id, It sounds ridiculous, but here is the scenario:
The domain classes are as follows:
public class SomeClass
{
public int SomeClassId { get; set; }
public string Name { get; set; }
public virtual Behavior Behavior { get; set; }
}
public abstract class Behavior
{
public int BehaviorId { get; set; }
}
public class BehaviorA : Behavior
{
public string BehaviorASpecific { get; set; }
}
public class BehaviorB : Behavior
{
public string BehaviorBSpecific { get; set; }
}
The entity context is
public class TestContext : DbContext
{
public DbSet<SomeClass> SomeClasses { get; set; }
public DbSet<Behavior> Behaviors { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<SomeClass>()
.HasOptional(s => s.Behavior)
.WithRequired()
.WillCascadeOnDelete(true);
}
}
Now this code can be executed to demonstrate the point
(described with comments in the code below)
using(TestContext db = new TestContext())
{
var someClass = new SomeClass() { Name = "A" };
someClass.Behavior = new BehaviorA() { BehaviorASpecific = "Behavior A" };
db.SomeClasses.Add(someClass);
// Here I have two classes with the state of added which make sense
var modifiedEntities = db.ChangeTracker.Entries()
.Where(entity => entity.State != System.Data.Entity.EntityState.Unchanged).ToList();
// They save with no problem
db.SaveChanges();
// Now I want to change the behavior and it causes entity to try to remove the behavior and add it again
someClass.Behavior = new BehaviorB() { BehaviorBSpecific = "Behavior B" };
// Here it can be seen that we have a behavior A with the state of deleted and
// behavior B with the state of added
modifiedEntities = db.ChangeTracker.Entries()
.Where(entity => entity.State != System.Data.Entity.EntityState.Unchanged).ToList();
// But in reality when entity sends the query to the database it replaces the
// remove and insert with an update query (this can be seen in the SQL Profiler)
// which causes the discrimenator to remain the same where it should change.
db.SaveChanges();
}
How to change this entity behavior so that delete and insert happens instead of the update?
A possible solution is to make the changes in 2 different steps: before someClass.Behavior = new BehaviorB() { BehaviorBSpecific = "Behavior B" }; insert
someClass.Behaviour = null;
db.SaveChanges();
The behaviour is related to the database model. BehaviourA and B in EF are related to the same EntityRecordInfo and has the same EntitySet (Behaviors).
You have the same behaviour also if you create 2 different DbSets on the context because the DB model remains the same.
EDIT
Another way to achieve a similar result of 1-1 relationship is using ComplexType. They works also with inheritance.
Here an example
public class TestContext : DbContext
{
public TestContext(DbConnection connection) : base(connection, true) { }
public DbSet<Friend> Friends { get; set; }
public DbSet<LessThanFriend> LessThanFriends { get; set; }
}
public class Friend
{
public Friend()
{Address = new FullAddress();}
public int Id { get; set; }
public string Name { get; set; }
public FullAddress Address { get; set; }
}
public class LessThanFriend
{
public LessThanFriend()
{Address = new CityAddress();}
public int Id { get; set; }
public string Name { get; set; }
public CityAddress Address { get; set; }
}
[ComplexType]
public class CityAddress
{
public string Cap { get; set; }
public string City { get; set; }
}
[ComplexType]
public class FullAddress : CityAddress
{
public string Street { get; set; }
}

DbContext set<> include dont work

//Site entity
public class Site
{
public long Id { get; set; }
public string Title { get; set; }
public virtual List<Language> Languages { get; set; }
}
//language entity
public class Language
{
public long Id { get; set; }
public string Title { get; set; }
public string ShortName { get; set; }
public virtual List<Site> Sites { get; set; }
}
//my context with custom connection and transaction
public class PortalBaseContext : DbContext
{
public PortalBaseContext(DbConnection conn)
: base(conn, false)
{
Database.UseTransaction((DbTransaction)PersistContext.Transaction);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Site>().ToTable("TblSite");
modelBuilder.Entity<Language>().ToTable("TblLanguage");
modelBuilder.Entity<Site>().
HasMany(c => c.Languages).
WithMany(p => p.Sites).
Map(
m =>
{
m.MapLeftKey("LanguageId");
m.MapRightKey("SiteId");
m.ToTable("TblSiteLanguage");
});
}
public DbSet<Site> TblSite { get; set; }
public DbSet<Language> TblLanguage { get; set; }
}
PortalBaseContext c = new PortalBaseContext( );
//Part1
DbSet<Site> query = c.Set<Site>();
query.Include("Languages");
lst = query.ToList();
//Part2
//lst = c.TblSite.Include("Languages").ToList();
in part1 include not work!!
if comment part1 and use part2 include work correctly!!
i use custom connection and transaction per request and want load
sub property for each class.
in part1 include not work!!
if comment part1 and use part2 include work correctly!!
i use custom connection and transaction per request and want load
sub property for each class.
Include is a non destructive method. It returns a new enumerable where each item will have the specified related entities loaded when materialized; the original DBSet you're invoking it on isn't modified in any way.
You need to apply ToList to the value returned by Include. Change this:
DbSet<Site> query = c.Set<Site>();
query.Include("Languages");
lst = query.ToList();
to this:
DbSet<Site> query = c.Set<Site>();
lst = query.Include("Languages").ToList();

EF 5.0 not removing optional one to one navigation property

Setting an optional one to one navigation property to null in Entity Framework 5 does not seem to make it to the database. Is this expected behavior?
In the example below, Person is a proxy object. I would expect setting the address to null will cause the address to be removed from the database.
The code below works if I lazy load the address before setting it null. But loading the address before
Any help will be greatly appreciated.
namespace ConsoleApplication2
{
using System;
using System.Data.Entity;
internal class Program
{
private static void Main(string[] args)
{
using (PersonContext context = new PersonContext())
{
// Make sure person with Id = 1 exists with an address.
Person person = context.People.Find(1) ?? context.People.Add(new Person { Id = 1 });
if (person.Address == null)
{
person.Address = new Address
{
Street = "123 Main Street",
City = "SomeCity",
State = new State
{
Code = "NY",
Name = "New York"
},
Zip = "11771"
};
}
context.SaveChanges();
}
// Setting address to null should remove relationship
using (PersonContext context = new PersonContext())
{
Person person = context.People.Find(1);
Console.WriteLine("Person is a " + person.GetType());
person.Address = null;
context.SaveChanges();
if (person.Address == null)
{
Console.WriteLine("Success: Person.Address is null.");
}
else
{
Console.WriteLine("Failure: Person.Address is not null.");
}
}
}
}
public class Person
{
public int Id { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int Person_Id { get; set; }
public string Street { get; set; }
public string City { get; set; }
public int StateId { get; set; }
public State State { get; set; }
public string Zip { get; set; }
}
public class State
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
public class PersonContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<State> States { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Address>()
.HasKey(x => x.Person_Id);
modelBuilder.Entity<Person>()
.HasOptional<Address>(x => x.Address)
.WithRequired()
.WillCascadeOnDelete();
}
}
}
Where you are not using lazy loading, the related properties such as Address will not be loaded and so will already be null.
To ensure it is always loaded use eager loading :
Person person = context.People.Include(x => x.Address).Single(x => x.Id == 1);