Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

Entity Framework native sql mapping to class

I got a code first EF and I want to use native sql for the more complex select statements. When I try to execute:

using (VaultsDbContext db = new VaultsDbContext())
{
   var contracts = db.Contracts.SqlQuery("select * from Contracts").ToList<Contract>();
}

I got:

Cannot create a value for property 'MetaProps' of type 'DskVault.Models.DbModels.MetaProps'. Only properties of primitive or enumeration types are supported.

MetaProps is a class that holds deleteflag, creator etc. and it's a property of all my classes. It's not mapped to a different table, every table has deleteflag, createor, etc.

public class Contract
{
    public long Id { get; set; }
    ...
    public MetaProps MetaProps { get; set; }
}

Is there a way to map from the native sql to the class if the class contains a complex type or does EF not support that? Also what if the complex type is entity mapped to another table(join)?

Edit: Version: Entity Framework 6

Upvotes: 4

Views: 657

Answers (1)

Ramoth
Ramoth

Reputation: 237

I know from experience not all the fields in your table have to be contained in your model. This is a good thing when it comes to installing updates into production. Have you tried reverse engineering your tables on a SEPARATE temporary project using the Entity Framework Power tools? This is a Nuget package that I have found to be extremely useful in code first programming. Reverse engineering will overwrite existing files, so make sure not to do this on your live code.

Upvotes: 1

Related Questions