当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How do I ignore default EntityData properties?

问题描述:

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?

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: