10

I'm looking over some alternatives to EF, and Dapper seems like a pretty good option. But I do have one question. I understand that Dapper, being a Micro ORM doesn't rely on or use a class of models, like EF creates. That being said, what if I still want a buncha models that mirror my db tables? How would I generate them and keep them up to date? Or, at least, some of my tables?

This is from the Dapper GutHub page:

public class Dog
{
    public int? Age { get; set; }
    public Guid Id { get; set; }
    public string Name { get; set; }
    public float? Weight { get; set; }

    public int IgnoredProperty { get { return 1; } }
}            

var guid = Guid.NewGuid();
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

Assert.Equal(1,dog.Count());
Assert.Null(dog.First().Age);
Assert.Equal(guid, dog.First().Id);

So in this exampe, was the Dog class manually created?

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • What do you mean by "manually created"? Typed by a programmer? Constructor called? –  Aug 22 '17 at 21:00
  • https://stackoverflow.com/questions/11056141/how-to-generate-model-from-database-using-dapper – maccettura Aug 22 '17 at 21:00
  • Good question. I meant typed by a programmer, but what I really should have said was, "Was the creation of the Dog class somehow automated? And if so, how?" – Casey Crookston Aug 22 '17 at 21:01
  • 1
    @maccettura - yup, that's it. Thanks – Casey Crookston Aug 22 '17 at 21:02
  • Possible duplicate of [How to generate model from database using Dapper?](https://stackoverflow.com/questions/11056141/how-to-generate-model-from-database-using-dapper) – Liam Oct 04 '18 at 14:39

2 Answers2

14

You will need to create Dog class by yourself. Dapper is just a wrapper around ADO.NET.

If you want to save time, you could just generate POCO from EF Code first from Database or Entity Framework Power Tools, and use it in Dapper.

Win
  • 61,100
  • 13
  • 102
  • 181
3

Why dont you utilize EF code first migration to maintain/sync your domain models and database. Dapper will work with these entities that generated by EF code first migration

Dan Nguyen
  • 1,308
  • 6
  • 17