简体   繁体   English

使用NHibernate或Linq to SQL是否可能?

[英]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? 是否可以仅通过使用模型上的属性使用Linq to SQL或NHibernate在类和数据库之间进行ORM映射?

For eg: If you use Parse.com as the backend, establishing a relationship between the DB and the class is as simple as: 例如:如果您使用Parse.com作为后端,那么在数据库和类之间建立关系就很简单:

[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? 我想知道,如果必须使用SQL Server替换Parse后端,是否可以实现类似的功能?

Linq to SQL and NHibernate seems to need configuration files for it to work. Linq to SQL和NHibernate似乎需要配置文件才能工作。 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. 在我们公司的大多数项目中,我们仅使用Linq to SQL来实现启用linq的数据库视图查询,并且仅使用属性将其行映射到类。 We use ColumnAttribute and TableAttribute to indicate our view name (table name) and its column names. 我们使用ColumnAttribute和TableAttribute来指示我们的视图名称(表名称)及其列名称。 More info: https://msdn.microsoft.com/en-us/library/system.data.linq.mapping(v=vs.110).aspx 更多信息: https : //msdn.microsoft.com/zh-cn/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. 我们也经常将NHibernate与属性映射一起使用。 We use FluentNhibernate to set up conventions and use no mapping classes. 我们使用FluentNhibernate设置约定,不使用任何映射类。 Just conventions and attributes. 只是约定和属性。

Here is code that returns NHibernate configuration object with all conventions set. 这是返回设置了所有约定的NHibernate配置对象的代码。

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. IMO这个主题太高级了,用户场景特定于一个答案。 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). AutomappingConfiguration-这是在FluentNhibernate.Automapping.DefaultAutomappingConfiguration类之后继承的类,它可以帮助NHibernate猜测将哪些类映射为实体,以及应该映射哪些属性(ShouldMap,IsVersion,IsComponent,IsCollection方法可能对您来说很有趣)。

Yo can see that we use a lot of "model.Conventions.Add" method. 可以看到,我们使用了很多“ model.Conventions.Add”方法。 We instantiate and add to NHibernate configuration our convention instances. 我们实例化我们的约定实例并将其添加到NHibernate配置中。 They implement NHibernate from FluentNhibernate.Conventions namespace like: IPropertyConvention, IReferenceConvention, IHasManyConvention, ICollectionConvention etc. 他们从FluentNhibernate.Conventions命名空间实现NHibernate,例如:IPropertyConvention,IReferenceConvention,IHasManyConvention,ICollectionConvention等。

You can learn more about FluentNHibernate conventions automapping here: https://github.com/jagregory/fluent-nhibernate/wiki/Conventions . 您可以在此处了解有关FluentNHibernate自动映射的更多信息: 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. 正如我所说,自动映射是一个很大且非常个人化的话题,但我希望我能向您展示一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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