EF VS NHibernate的合并断开连接的对象图 [英] EF vs Nhibernate Merge Disconnected Object Graph

查看:347
本文介绍了EF VS NHibernate的合并断开连接的对象图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始一个新的项目在几个星期前,决定给EF code首先尝试,
我用的NHibernate之前,我喜欢有一个ORM超出了弓从MS的想法,到目前为止它已经很大 - 直到我开始做复杂的对象

I just started a new project a few weeks ago and decided to give EF Code First a try, I used NHIbernate before and i loved the idea of having an ORM out-of-the-bow from MS, and so far it's been great - until i started making complex objects.

我的项目层次如下:
Azure的WCF角色 - 来处理与EF code首先DAL。
与MVC 4淘汰赛Azure的网页角色 - 对付客户端。
(我创建了WCFRole的未来,我们需要访问来自不同平台的服务)

My project tiers are as follows: Azure WCF Role - to handle the DAL with EF Code First. Azure WebSite Role with MVC 4 with Knockout - to deal with the client. (I created the WCFRole for the future where we'll need access to the service from different platforms)

下面是我遇到麻烦了非常基本的EF code首先设计:
- 我的服务和网站之间只传输的DTO,并提出了一个通用的映射器如果存在于内心的DTO映射。
- 我有一个城市的表,和一个地址对象与市物业(我们需要城市作为特殊功能的属性并不仅仅是名称)

Here is the very basic EF Code First design that i'm having trouble with: -I'm transferring only DTOs between the service and site, and made a generic mapper to map inner DTOs if any exist. - I have a City table, and an Address Object with a City Property (we need the City as a property for special functions and not just name)

客户知道城市名单,并与现有的城市返回新地址
当我尝试添加新的地址,一个新的城市与旧现有的数据创建的,我已经得到了,这是因为EF不知道如何合并断开连接的对象,并从我读不支持任何合并abitiy,而简单的不那么舒适的解决方案仅仅是管理对象的状态 - 城市对象状态更改为不变

The client knows the list of Cities, and is returning a new Address with an existing city When i try to add the new Address, a new city is created with the data of the old existing one, I already got that this happens because EF doesn't know how to merge disconnected objects and from what i read doesn't support any merge abitiy, and that the simple not so comfortable solution is just managing the Object State - change the City object state to Unchanged.

但有一个大的复杂分贝的设计处理这听起来很可怕的。

But handling this with a big complex db design sounds horrible

我的问题 -
什么是处理这个最佳实践/简单的方法?
我想到了喜欢的解决方案 - 覆盖SaveChanges方法去,尽管所有的对象,如果ID不是null / 0 /其他约定将其从添加更改为不变 - 这可以解决方案来实现。

my question - What is the best practice/ easy way of handling this? I thought about solutions like - overriding the SaveChanges method going though all objects and if ID is not null/0/some other convention to change it from Added to Unchanged - can this solution be done?

我的第二个问题 -
因为我有很多的经验与NHibernate(带连接的对象) - 我想知道这个是什么NHibernate的时间?我读的地方,NHibernate的确实有重新连接断开的复杂对象的AUTOMAGIC合并的能力,这是真的吗?将我的基本断开寻址>城市设计将工作外的开箱与AUTOMAGIC合并?
什么是使用的后果?

my 2nd question - because i have a lot of experience with NHibernate (with connected objects) - I was wondering what NHibernate's take on this? i read somewhere that NHibernate does have an AutoMagic Merge ability to reconnect disconnected complex objects, is this true? will my basic disconnected Address->City design will work out-of-the-box with that AutoMagic Merge? And what are the ramifications of using that?

非常感谢:)

更新:简化code的问题

Update: a simplified code for the issue.

public class Address
{
    public int ID { get; set; }

    public virtual City City { get; set; }
}

public class City
{
    public int ID { get; set; }

    public string Name { get; set; }
    public virtual Zone Zone { get; set; }
}

public class MyContext : DbContext
{
    public MyContext() : base("TransportService") { }

    public virtual DbSet<City> Cities { get; set; }
    public virtual DbSet<Address> Addresses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Address>()
            .HasRequired(x => x.City)
            .WithMany().WillCascadeOnDelete(true);

    }
}

添加新地址:

    public void Add(AddressDto address)
    {
        using (var context = new MyContext())
        {
            context.Addresses.Add(address.FromDto<Address>());

            context.SaveChanges();
        }
    }

在FromDto是通用的映射器扩展,它创建的所有信息新的地址,包括市(和City.ID属性)

The "FromDto" is the generic Mapper extension, it creates the new address with all the Information, including the City (and the City.ID property)

这将导致建立一个新的城市,而不是使用老城区的参考。

This will result in creating a new city instead of using a reference to the old city.

推荐答案

我发现EF没有自动合并,它们的合并功能至今都只能手工合并,如果上下文对象在内存中只会工作该对象的所有依赖关系。

I found that EF doesn't have auto merge, their merge capabilities so far are only manual merge, and it will work only if the Context object has in memory all the dependencies of the object.

与多级断开连接的对象打交道,并处理手动重新连接他们的是大量的工作,可能会导致很奇怪和难以处理漏洞(就像在新的城市里创建,因为这不是问题陈述之一合并,所以它只是创建即使它有一个ID一个新的)

Dealing with multilevel disconnected objects, and dealing with reconnecting them manually is a lot of work and can result in really weird and hard to handle bugs (just like the one stated in the question where new cities where created because it wasn't merged , so it just created a new one even if it had an ID)

所以现在 - NHibernate的胜这一仗,
NHibernate的有AUTOMAGIC合并功能,如果你的断开连接的对象都有一个ID它会尝试进行合并,并获得成功(从我的经验)
这是一个有点多设置使其正常运行相比,EF,但它是值得的麻烦。

so for now - Nhibernate wins this battle, Nhibernate has an automagic merge functionality, if your disconnected object has an ID it will try to merge it, and be successful (from my experience) It's a little more setting up to get things running compared to the EF, but it is worth the trouble.

这篇关于EF VS NHibernate的合并断开连接的对象图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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