Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Dapper create a model class from DB tables?

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?

like image 883
Casey Crookston Avatar asked Aug 22 '17 20:08

Casey Crookston


2 Answers

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.

like image 72
Win Avatar answered Oct 11 '22 09:10

Win


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

like image 29
Dan Nguyen Avatar answered Oct 11 '22 07:10

Dan Nguyen