使用NHibernate或Linq to SQL是否可能? [英] Is this possible using NHibernate or Linq to SQL?

查看:89
本文介绍了使用NHibernate或Linq to SQL是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅通过使用模型上的属性,是否可以使用Linq to SQL或NHibernate在类和数据库之间进行ORM映射?

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?

例如:如果您使用Parse.com作为后端,则在数据库和类之间建立关系就很简单:

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); } 
            }
}

没有其他要求.没有映射文件,没有设计器文件. 我想知道,如果必须使用SQL Server替换Parse后端,我是否可以实现类似的目的?

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和NHibernate似乎需要配置文件才能运行.还有其他我可以使用的东西吗?

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

推荐答案

在我们公司的大多数项目中,我们仅使用Linq to SQL来实现启用linq的数据库视图查询,并且仅使用属性将其行映射到类.我们使用ColumnAttribute和TableAttribute指示我们的视图名称(表名称)及其列名称.更多信息: https://msdn.microsoft.com/zh-CN/library/system.data.linq.mapping(v=vs.110).aspx

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.

我们还经常将NHibernate与属性映射一起使用.我们使用FluentNhibernate来设置约定,并且不使用任何映射类.只是约定和属性.

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.

这是返回设置了所有约定的NHibernate配置对象的代码.

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这个主题太高级了,用户场景特定于一个答案.我将为您列出一些可能对您要实现的目标有用的课程.

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-这是在FluentNhibernate.Automapping.DefaultAutomappingConfiguration类之后继承的类,它可以帮助NHibernate猜测将哪些类映射为实体,以及应该映射哪些属性(ShouldMap,IsVersion,IsComponent,IsCollection方法可能对您来说很有趣).

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).

您可以看到我们使用了很多"model.Conventions.Add"方法.我们实例化我们的约定实例并将其添加到NHibernate配置中.他们从FluentNhibernate.Conventions命名空间实现NHibernate,例如:IPropertyConvention,IReferenceConvention,IHasManyConvention,ICollectionConvention等.

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.

您可以在此处了解有关FluentNHibernate自动映射的更多信息: https://github.com/jagregory/fluent-nhibernate/wiki/Conventions

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.

这篇关于使用NHibernate或Linq to SQL是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆