How do I ignore default EntityData properties?

拜拜、爱过 提交于 2019-11-28 12:50:24

问题


I am creating an MVC mobile application service using Entity Framework

I have created an Entity Model like so:

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

    public string Name { get; set; }
}

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": "Invalid column name 'Id'.\r\nInvalid column name >'Version'.\r\nInvalid column name 'CreatedAt'.\r\nInvalid column name 'UpdatedAt'.",

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

So the query Generated is

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

Is it possible to exclude these default EntityData columns as they are not in my table Regions?


回答1:


First you need to hide base class attributes with key new:

public new DateTimeOffset? CreatedAt { get; set; }

Then add attribute [NotMapped]:

[NotMapped]
public new DateTimeOffset? CreatedAt { get; set; }

Finally result:

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

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.



来源:https://stackoverflow.com/questions/34723280/how-do-i-ignore-default-entitydata-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!