What ORM should I use for a ASP.Net MVC project?

前端 未结 9 2115
清酒与你
清酒与你 2021-02-06 09:23

I am starting a new ASP.Net MVC project and was wondering what is the best Model approach to take.

Most demos recommend LinqToSQL but I know Microsoft are not really en

9条回答
  •  梦毁少年i
    2021-02-06 10:11

    Castle ActiveRecord works really well for small-to-medium projects based on MVC - I'm using it on a couple of projects at the moment and finding it a very good fit with ASP.NET MVC.

    It's an implementation of the active record pattern that uses NHibernate under the hood - in other words, as long as you're happy having one class per database table, it'll do most of the NHibernate configuration for you, leaving you free to write code like:

    Customer bob = Customer.Find(/* customer Id goes here */);
    bob.FirstName = "Robert";
    bob.Save();
    
    Invoice newInvoice = new Invoice();
    newInvoice.Products.Add(Product.Find(/* product ID goes here */);
    bob.Invoices.Add(newInvoice);
    bob.Save();
    

    with very little explicit NHibernate configuration (assuming you have Customer, Invoice and Product tables in your DB, of course)

    Because it's NHibernate under the hood, migrating to NHibernate if you need to should be straightforward - you'll already have all the necessary references and libraries. There are a few aspects of NHibernate behaviour that you can't ignore (like session scopes and flushing) - but it's not too hard to work out what's going on with these, and using them in ActiveRecord will give you a head start if/when you need to move to using NHibernate directly.

提交回复
热议问题