问题描述
我正在使用实体框架
$ b创建一个
MVC移动应用程序服务
$ b 我已经创建了一个实体模型
:
public class Regions:EntityData
{
public string Id {get;组; }
public string Name {get;组; }
}
我创建了一个 TableController
所以我通过获取请求进行查询:
http:// localhost:3000 / tables / Regions
这会返回一条错误,说:
查看 EntityData
class我可以看到这些是 EntityData
类的属性:
public abstract class EntityData:ITableData
{
protected EntityData();
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Index(IsClustered = true)]
[TableColumn(TableColumnType.CreatedAt)]
public DateTimeOffset? CreatedAt {get;组; }
[TableColumn(TableColumnType.Deleted)]
public bool Deleted {get;组;
[Key]
[TableColumn(TableColumnType.Id)]
public string Id {get;组; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[TableColumn(TableColumnType.UpdatedAt)]
public DateTimeOffset?已更新组; }
[TableColumn(TableColumnType.Version)]
[Timestamp]
public byte [] Version {get;组;
}
所以生成的查询是
$ b $ {pre>
'SELECT
[Extent1]。[Id] AS [Id],
[Extent1]。[Name] AS [Name],
[Extent1]。[版本] AS [Version],
[Extent1]。[CreatedAt] AS [CreatedAt],
[Extent1]。[已更新] AS [已更新],
[Extent1]。[已删除] AS [已删除]
FROM [dbo]。[Region] AS [Extent1]''
这显然是查询失败的原因。
是否可以排除这些默认的 EntityData
列,因为它们不在我的表中 Regions
?
首先你需要用新的关键字隐藏基类属性:
public new DateTimeOffset? CreatedAt {get;组; }
然后添加属性 [NotMapped]
[NotMapped]
public new DateTimeOffset? CreatedAt {get;组;
最终结果:
public class Regions:EntityData
{
public string Id {get;组; }
public string Name {get;组;
[NotMapped]
public new DateTimeOffset? CreatedAt {get;组;
[NotMapped]
public new DateTimeOffset?已更新组;
[NotMapped]
public new byte [] Version {get;组; }
}
在这种情况下,我不知道如何处理id。如果您需要隐藏基类属性,请在类型之前添加新的。
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:
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.
这篇关于如何忽略默认的EntityData属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!