EF OData MySql未知列'Project3.x'在'where子句' [英] EF OData MySql Unknown column 'Project3.x' in 'where clause'

查看:304
本文介绍了EF OData MySql未知列'Project3.x'在'where子句'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是OData V4,EF6和MySql 5.6 / 5.7
以下型号和表格。我使用这个调用odata / Applications得到应用程序资源的结果,但是当我扩展角色时,我收到这个错误,如下所示odata / Applications?$ expand = roles。



错误:执行命令定义时发生错误。查看内部例外情况。未知的列'Project»ApplicationId'在'where子句'



我知道这是映射的东西,但我看不到什么。

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

public string Name {get;组; }

public int ApplicationId {get;组; }

// public virtual Application Application {get;组;

}

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

public string Name {get;组; }

public bool IsDeleted {get;组; }

public virtual ICollection< Role>角色{get;组; }

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

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

public string Name {get;组; }

public ICollection< RoleView>角色{get;组; }

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

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

public string Name {get;组; }

public int ApplicationId {get;组;

}

创建表IF NOT EXISTS应用程序(
ApplicationId int not null auto_increment主键,
名称varchar(255)not null,
IsDeleted bool not null默认为false,

CreatedBy varchar(255)not null,
CreatedOn Datetime not null,
已更新B varchar(255)not null,
UpdatedOn DateTime not null

)ENGINE = INNODB;



$ pre> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ int int int int int int int int int int int int int int int int int int int int int int int int int int int int int不是null,

CreatedBy varchar(255)not null,
CreatedOn Datetime not null,
已更新B varchar(255)not null,
UpdatedOn DateTime not null,

索引app_index(ApplicationId),
外键(ApplicationId)引用删除级联上的应用程序(ApplicationId)

)ENGINE = INNODB;



这是OData操作方法。

  [Htt的pGet] 
[ODataRoute(Applications)]
public IQueryable< ApplicationView> Get()
{
var result = IdentityRepository.Applications
.Include(Role)
.ProjectTo< ApplicationView>();
返回结果;
}

映射类:

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

HasKey(x => x.Id);
属性(x => x.Id).HasColumnName(RoleId);

//试过/不,没有变化。
// 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);
属性(x => x.Id).HasColumnName(ApplicationId);

//尝试/不改变。
HasMany(x => x.Roles).WithRequired()。HasForeignKey(x => x.ApplicationId);
}
}

我尝试使用代码第一和数据库的存储库edmx并保持相同的错误。

解决方案

EF映射似乎是正确的。我的猜测是问题来自于 ProjectTo 方法。这似乎是AutoMapper可追查扩展的一部分。 文档说(关于 ProjectTo ):


请注意,要使此功能正常工作,所有类型转换必须在映射中明确处理
。例如,您不能依赖
Item类的ToString()覆盖来通知实体框架
只能从Name列中进行选择,并且任何数据类型更改,例如
作为Double对于十进制也必须明确处理。


尝试在查询中替换 ProjectTo 通过选择,或者将其简单删除,以确认错误是否来自那里,或查看AutoMapper映射。


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.

解决方案

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.

这篇关于EF OData MySql未知列'Project3.x'在'where子句'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆