I have a dotnetcore 2.0 angular project. Inside the project, I have Project, Owner, and Status domain classes. The relationships are like below: 1 Owner can have multiple Projects, 1 Project should have only one status.
I use AutoMapper to map those classes in order to create API resources. My domain classes are:
public class Owner
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[StringLength(255)]
public string Email { get; set; }
[StringLength(255)]
public string Phone { get; set; }
[StringLength(255)]
public string Title { get; set; }
public DateTime? Updated { get; set; }
public ICollection<Project> Projects { get; set; }
public Owner()
{
Projects = new Collection<Project>();
}
}
Status Class:
public class Status
{
public int Id { get; set; }
[StringLength(255)]
public string Name { get; set; }
public ICollection<Project> Projects { get; set; }
public Status()
{
Projects = new Collection<Project>();
}
}
Project class:
public class Project
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public string Description { get; set; }
[StringLength(255)]
public string Development { get; set; }
[StringLength(255)]
public string Production { get; set; }
[StringLength(255)]
public string Link { get; set; }
[StringLength(255)]
public string DbName { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? Updated { get; set; }
public int OwnerId { get; set; }
public Owner Owner { get; set; }
public int StatusId { get; set; }
public Status Status { get; set; }
}
My resources classes are identical to the domain classes temporarily. Here is my project resource class that causes the problem:
public class ProjectResource
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Development { get; set; }
public string Production { get; set; }
public string Link { get; set; }
public string DbName { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? Updated { get; set; }
public OwnerResource Owner { get; set; }
public StatusResource Status { get; set; }
}
Here is my MappingProile class:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Owner, OwnerResource>();
CreateMap<Project, ProjectResource>()
.ForMember(pr => pr.Owner, opt => opt.MapFrom(p => p.Owner.Projects))
.ForMember(pr => pr.Status, opt => opt.MapFrom(p => p.Status.Projects))
.ForMember(pr => pr.Status, opt => opt.MapFrom(p => new StatusResource{Id = p.Status.Id, Name = p.Status.Name}))
.ForMember(pr => pr.Owner, opt => opt.MapFrom(p => new OwnerResource{
Id = p.Owner.Id,
Name = p.Owner.Name,
Email = p.Owner.Email,
Title = p.Owner.Title,
Updated = p.Owner.Updated
}));
CreateMap<Status, StatusResource>();
CreateMap<OwnerResource, Owner>()
.ForMember(o => o.Id, opt => opt.Ignore());
CreateMap<ProjectResource, Project>()
.ForMember(p => p.Id, opt => opt.Ignore())
.ForMember(p => p.Status, opt => opt.MapFrom(p => p.Status.Name))
.ForMember(p => p.Owner, opt => opt.MapFrom(p => p.Owner.Name));
}
}
This profile class is giving me the projects without any problems. However, when I create a new project using controller classes, it gives me this error :
InvalidOperationException: The property 'OwnerId' on entity type 'Project' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
I am guessing I have mistakes inside the mapping profile class. Please let me know if this information is enough or you need more details.
What I have tried:
I've changed Mapping profile class.
Modified domain and resources classes.