如何忽略默认的EntityData属性?

时间:2016-01-11 13:56:35

标签: c# asp.net asp.net-mvc entity-framework azure-mobile-services

我正在使用MVC mobile application service

创建Entity Framework

我创建了Entity Model,如此:

public class Regions : EntityData
{
    public string Id { get; set; }

    public string Name { get; set; }
}

我创建了一个TableController,因此我通过向:

发出获取请求进行查询
http://localhost:3000/tables/Regions

这会返回错误说明:

  

" exceptionMessage":"列名称无效' ID'。\ r \ n无效列名>'版本'。\ r \ n无效列name' CreatedAt'。\ r \ n无效列   name' UpdatedAt'。",

查看EntityData类我可以看到这些是EntityData类的属性:

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; }
}

因此查询Generated是

'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]''

这显然是查询失败的原因。

是否可以排除这些默认的EntityData列,因为它们不在我的表Regions中?

1 个答案:

答案 0 :(得分:4)

首先,您需要使用new 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。如果需要隐藏基类属性,请在键入之前添加new。