简体   繁体   中英

Is this possible using NHibernate or Linq to SQL?

Is it possible to do ORM mapping between classes and the DB using Linq to SQL or NHibernate just by using attributes on the models?

For eg: If you use Parse.com as the backend, establishing a relationship between the DB and the class is as simple as:

[ParseClassName("itemsForSale")] //Name of the table
public class Item : ParseObject
{
            [ParseFieldName("userId")] //Name of the column
            public string UserId
            {
                get { return GetProperty<string>(); }
                set { SetProperty<string>(value); } 
            }
}

Nothing else is required. No mapping files, no designer files. I was wondering, in the event I have to replace the Parse backend using SQL server, will I be able to achieve something similar?

Linq to SQL and NHibernate seems to need configuration files for it to work. Is there something else I can use?

In most of our company projects we use Linq to SQL just for linq enabled querying of database views and we use only attributes for mapping their rows to classes. We use ColumnAttribute and TableAttribute to indicate our view name (table name) and its column names. More info: https://msdn.microsoft.com/en-us/library/system.data.linq.mapping(v=vs.110).aspx

This way we use no external mapping mechanisms but attributes in our view result classes.

We also use NHibernate with attribute mappings very often. We use FluentNhibernate to set up conventions and use no mapping classes. Just conventions and attributes.

Here is code that returns NHibernate configuration object with all conventions set.

return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012.ConnectionString(c => c.FromConnectionStringWithKey(Stale.NazwaPolaczeniaERP)).DefaultSchema("dbo"))
.Mappings(m =>
{
    var model = AutoMap.Assemblies(new AutomappingConfiguration(),
    new Assembly[]
    {
        typeof(ERP.DomenaERP.ZnacznikZasobu).Assembly
    });

    model.Conventions.Add(new SetEnumTypeConvention());
    model.Conventions.Add(new ColumnConvention());
    model.Conventions.Add(new CollectionAccessConvention());
    model.Conventions.Add(new OptimisticLockIgnoreConvention());
    model.Conventions.Add(new SqlTimestampConvention());
    model.Conventions.Add(new SetTableNameConvention());
    model.Conventions.Add(new VersionConvention());
    model.Conventions.Add(new HasManyConvention());
    model.Conventions.Add(new InheritanceConvention());
    model.Conventions.Add(new ColumnNameConvention());

    model.Conventions.Add(DefaultLazy.Always());
    m.AutoMappings.Add(model);
})

IMO this topic is too advanced and user scenario specific to elaborate in single answer. I'll list for you few classes that might be useful for what you are trying to achieve.

AutomappingConfiguration - this is class inherits after FluentNhibernate.Automapping.DefaultAutomappingConfiguration class and it helps NHibernate to guess which classes to map as entities and which of their properties should be mapped (ShouldMap, IsVersion, IsComponent, IsCollection methods might be interesting for you).

Yo can see that we use a lot of "model.Conventions.Add" method. We instantiate and add to NHibernate configuration our convention instances. They implement NHibernate from FluentNhibernate.Conventions namespace like: IPropertyConvention, IReferenceConvention, IHasManyConvention, ICollectionConvention etc.

You can learn more about FluentNHibernate conventions automapping here: https://github.com/jagregory/fluent-nhibernate/wiki/Conventions .

As I said, automapping is large and very individual topic but I hope that I was able to show you a way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM