添加/插入petapoco与dapper的风格

Eli*_*eth 4 dapper petapoco

我为此感到高兴:

//用peta poco插入记录

var a = new Article();
a.title="My new article";
a.content="PetaPoco was here";
a.date_created=DateTime.UtcNow;
db.Insert(a);
Run Code Online (Sandbox Code Playgroud)

我对此感到分心:

//用dapper插入记录

var a = new Article();
a.title="My new article";
a.content="PetaPoco was here";
a.date_created=DateTime.UtcNow;
string articleQuery= "INSERT INTO Article VALUES (@Title, @Content, @Date)";        
connection.Execute(articleQuery, new { Title = a.Title, Content = a.Content, Date = a.Date });
Run Code Online (Sandbox Code Playgroud)

我是dapper和peta poco的新手.可能是我没有找到更多的小巧玲珑,但我真的不喜欢我必须插入的方式.Peta poco似乎非常有意义.

能不能用这种方式做事吗?

Edu*_*eni 12

如果你喜欢PetaPoco风格更好,那就去吧.尽管Dapper更有名,但PetaPoco具有相同的性能,具有相同的概念,但它更灵活(IMO)


Voi*_*Ray 7

检查出短小精悍的扩展与小巧玲珑的"魔力" CRUD操作:

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    Person person = new Person { FirstName = "Foo", LastName = "Bar" };
    int id = cn.Insert(person);
    cn.Close();
}
Run Code Online (Sandbox Code Playgroud)

另请参阅主题以获取更多信息