Question

I have a domain model that has a properties that don't exist in the database. For example ...

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

As you can see the properties Key and ArchiveIdentifier are not columns in the DomainModels table. When I run a query Entity Framework treats the property ArchiveIdentifier as if it was a column in the database. Here is an example of the generated SQL when fetching all DomainModels.

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

Entity Framework doesn't try to populate the Key property since it doesn't have a set method however it treats ArchiveIdentifier as if it was a column in the table. Is there any annotation or method to tell Entity Framework that ArchiveIdentifier is not a column?

Was it helpful?

Solution

Use the NotMappedAttribute:

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

OTHER TIPS

Use [NotMapped] data annotation on your ArchiveIdentifier property

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top