4

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

4
  • Which version of EF are you using? Commented Feb 4, 2014 at 16:59
  • Are you using "code first"? Also, if all your tables have MetaProps why don't you create a table for it instead of adding those columns to all the tables that need this. Isn't what the RDB is for? Commented Feb 4, 2014 at 17:10
  • @sam I use EF 6. Yes, code first, see first sentence. If I have dedicated table for the metadata then all queries will require join with that table to check for the deleteflag. This lowers the performance and increases the complexity. Commented Feb 4, 2014 at 18:43
  • Sorry I missed the code first in the first sentence. I understand your performance concerns however those can be improved by introducing index on different columns that are being joined. To answer your original question, I don't think you can just map part of a table (Few columns) to a class. Commented Feb 5, 2014 at 0:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.