簡體   English   中英

實體框架僅填充域模型中的數據庫字段

[英]Entity Framework populate only the database fields in domain model

我有一個域模型,該模型具有數據庫中不存在的屬性。 例如 ...

public class DomainModel: IKeyedEntity
{
    //Id for the table
    public int DomainModelID { get; set; }

    //Foreign key value
    public int FooID { get; set; }

    //Text value
    public string Value { get; set; }

    //Generic reference to the id of the table
    public int Key { get { return this.DomainModelID; } }

    //No a column in the database
    public Guid ArchiveIdentifier
    {
        get { return Foo.ArchiveIdentifier; }
        set { Foo.ArchiveIdentifier = value }
    }

    //Navigation Property
    public virtual Foo Foo { get; set; }
}

如您所見,屬性KeyArchiveIdentifier不是DomainModels表中的列。 當我運行查詢時,Entity Framework會將屬性ArchiveIdentifier視為數據庫中的一列。 這是獲取所有DomainModel時生成的SQL的示例。

SELECT 
[Extent1].[DomainModelID] AS [DomainModelID], 
[Extent1].[FooID] AS [FooID], 
[Extent1].[Value] AS [Value],
[Extent1].[ArchiveIdentifier] AS [ArchiveIdentifier] 
FROM  [dbo].[DomainModels] AS [Extent1]

實體框架不嘗試填充Key屬性,因為它沒有設置方法,但是它將ArchiveIdentifier視為表中的一列。 是否有任何注釋或方法告訴Entity Framework ArchiveIdentifier不是列?

使用NotMappedAttribute

[NotMapped]
public Guid ArchiveIdentifier
{
    get { return Foo.ArchiveIdentifier; }
    set { Foo.ArchiveIdentifier = value; }
}

在ArchiveIdentifier屬性上使用[NotMapped]數據注釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM