EF OData MySql Unknown column 'Project3.x'

2019-06-04 18:33发布

问题:

I'm using OData V4, EF6 and MySql 5.6/5.7 With below models and tables. I get the result of the Application resource fine with this call odata/Applications but I get this error when I expand on roles, as follows odata/Applications?$expand=roles.

Error: An error occurred while executing the command definition. See the inner exception for details. Unknown column 'Project3.ApplicationId' in 'where clause'

I know it's something with mapping, but I can't see what.

public class Role
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int ApplicationId { get; set; }

    //public virtual Application Application { get; set; }

}

public class Application
{
    public int Id { get; set; }

    public string Name { get; set; }

    public bool IsDeleted { get; set; }

    public virtual ICollection<Role> Roles { get; set; }

    public Application()
    {
        Roles = new List<Role>();
    }
}

public class ApplicationView
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<RoleView> Roles { get; set; }

    public ApplicationView()
    {
        Roles = new List<RoleView>();
    }
}

public class RoleView
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int ApplicationId { get; set; }

}

create table IF NOT EXISTS Application (
ApplicationId int not null auto_increment primary key,
Name varchar(255) not null,
IsDeleted bool not null default false,

CreatedBy varchar(255) not null,
CreatedOn Datetime not null,
UpdatedBy varchar(255) not null,
UpdatedOn DateTime not null

) ENGINE=INNODB;

create table IF NOT EXISTS Role (
RoleId int not null auto_increment primary key,
Name varchar(255) not null,
ApplicationId int not null,

CreatedBy varchar(255) not null,
CreatedOn Datetime not null,
UpdatedBy varchar(255) not null,
UpdatedOn DateTime not null,

index app_index (ApplicationId),
foreign key (ApplicationId) references Application(ApplicationId) on delete cascade

) ENGINE=INNODB;

This is the OData action method.

[HttpGet]
    [ODataRoute("Applications")]
    public IQueryable<ApplicationView> Get()
    {
        var result = IdentityRepository.Applications
            .Include("Role")
            .ProjectTo<ApplicationView>();
        return result;
    }

The mapping classes:

public class RoleMap : EntityTypeConfiguration<Role>
{
    public RoleMap()
    {
        ToTable("Role");

        HasKey(x => x.Id);
        Property(x => x.Id).HasColumnName("RoleId");

        // Tried with/without, no change.
        //HasRequired(x => x.Application).WithMany(x => x.Roles).HasForeignKey(x => x.ApplicationId);
    }
}

public class ApplicationMap : EntityTypeConfiguration<Application>
{
    public ApplicationMap()
    {
        ToTable("Application");

        HasKey(x => x.Id);
        Property(x => x.Id).HasColumnName("ApplicationId");

        // Tried with/without no change.
        HasMany(x => x.Roles).WithRequired().HasForeignKey(x => x.ApplicationId);
    }
}

I tried the repository with both code first and database first edmx and keep getting the same error.

回答1:

The EF mappings seem correct. My guess is that the problem comes from the ProjectTo method. This seems to be part of AutoMapper Queryable Extensions. The documentation says (about ProjectTo):

Note that for this feature to work, all type conversions must be explicitly handled in your Mapping. For example, you can not rely on the ToString() override of the Item class to inform entity framework to only select from the Name column, and any data type changes, such as Double to Decimal must be explicitly handled as well.

Try replacing in your query ProjectTo by a Select, or plain removing it, to confirm if the error comes from there, or check your AutoMapper mappings.