我使用OData V4,EF6和MySql 5.6 / 5.7 以下型号和表格。我通过调用odata / Applications获得了Application资源的结果,但是当我扩展角色时我得到了这个错误,如下odata / Applications?$ expand = roles。
错误:执行命令定义时发生错误。有关详细信息,请参阅内部异常未知栏' Project3.ApplicationId'在' where子句'
我知道它有映射的东西,但我看不出来。
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;
这是OData操作方法。
[HttpGet]
[ODataRoute("Applications")]
public IQueryable<ApplicationView> Get()
{
var result = IdentityRepository.Applications
.Include("Role")
.ProjectTo<ApplicationView>();
return result;
}
映射类:
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);
}
}
我首先使用代码和数据库优先edmx来尝试存储库并继续获得相同的错误。
答案 0 :(得分:0)
EF映射似乎是正确的。我的猜测是问题来自ProjectTo
方法。这似乎是AutoMapper可查询扩展的一部分。 The documentation说(关于ProjectTo
):
请注意,要使此功能正常工作,所有类型转换必须是 在您的映射中明确处理。例如,你不能依赖 Item类的ToString()重写以通知实体框架 只能从Name列中选择,并且任何数据类型都会发生变化 因为必须明确处理Double to Decimal。
尝试用ProjectTo
替换您的查询Select
,或者将其删除,以确认错误是否来自那里,或检查您的AutoMapper映射。