问题描述
我创建一个 MVC移动应用服务
使用实体框架
我已经创建了一个实体模型
像这样:
I have created an Entity Model
like so:
public class Regions : EntityData
{
public string Id { get; set; }
public string Name { get; set; }
}
和我创建了一个 TableController
让我通过一个GET请求来查询:
and I have created a TableController
so I query by making a get request to:
http://localhost:3000/tables/Regions
这将返回一个错误的说法:
This returns an error saying:
exceptionMessage:。无效列名'ID'\\ r \\ n无效列名>'版本'\\ r \\ n无效的列名'CreatedAt'\\ r \\ n无效列
命名UpdatedAt'。
展望 EntityData code>类,我可以看到这些都是
EntityData code>类的属性:
Looking into the EntityData
class I can see these are properties of the EntityData
class:
public abstract class EntityData : ITableData
{
protected EntityData();
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Index(IsClustered = true)]
[TableColumn(TableColumnType.CreatedAt)]
public DateTimeOffset? CreatedAt { get; set; }
[TableColumn(TableColumnType.Deleted)]
public bool Deleted { get; set; }
[Key]
[TableColumn(TableColumnType.Id)]
public string Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[TableColumn(TableColumnType.UpdatedAt)]
public DateTimeOffset? UpdatedAt { get; set; }
[TableColumn(TableColumnType.Version)]
[Timestamp]
public byte[] Version { get; set; }
}
所生成的查询是
'SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Version] AS [Version],
[Extent1].[CreatedAt] AS [CreatedAt],
[Extent1].[UpdatedAt] AS [UpdatedAt],
[Extent1].[Deleted] AS [Deleted]
FROM [dbo].[Region] AS [Extent1]''
这显然是为什么查询失败。
Which is obviously why the query fails.
是否可以排除这些默认的 EntityData code>列,因为它们不是在我的表
地区
?
Is it possible to exclude these default EntityData
columns as they are not in my table Regions
?
推荐答案
首先,你需要隐藏基类属性与重点新:
First you need to hide base class attributes with key new:
public new DateTimeOffset? CreatedAt { get; set; }
然后,添加属性 [NotMapped]
:
[NotMapped]
public new DateTimeOffset? CreatedAt { get; set; }
最后结果是:
public class Regions : EntityData
{
public string Id { get; set; }
public string Name { get; set; }
[NotMapped]
public new DateTimeOffset? CreatedAt { get; set; }
[NotMapped]
public new DateTimeOffset? UpdatedAt { get; set; }
[NotMapped]
public new byte[] Version { get; set; }
}
我不知道你是怎么想在这种情况下,治疗的ID。如果你需要隐藏基类属性类型之前添加新的。
I don't know how you want to treat id in this case. If you need to hide the base class property add new before type.
这篇关于如何忽略默认EntityData属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!