Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:
C#
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:
C#
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:

C#
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:

C#
public class MappingProfile : Profile
{
    public MappingProfile()
    {
        //Domain to API Resource
        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>();

        //API Resource to Domain
        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.
Posted
Updated 18-Sep-17 9:01am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900