什么使实体框架/结果相信我的对象图"包含周期"? [英] What would make Entity Framework / Upshot believe my object graph "contains cycles"?

查看:164
本文介绍了什么使实体框架/结果相信我的对象图"包含周期"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试淘汰赛2.1.0和结果1.0.0.2与实体框架4.3(code-前)和我遇到了以下错误:

I am testing Knockout 2.1.0 and Upshot 1.0.0.2 with Entity Framework 4.3 (Code-First) and am running into the following error:

{对象图
  System.Collections.Generic.HashSet`1 [KnockoutTest.Models.Person,
  KnockoutTest,版本= 1.0.0.0,文化=中立,公钥=空]]'
  包含周期,如果引用跟踪是不能被序列化
  禁用。}

{"Object graph for type 'System.Collections.Generic.HashSet`1[[KnockoutTest.Models.Person, KnockoutTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled."}

我使用的是相当典型的模式有一些基本的亲子实体测试。

I am using a fairly typical model for testing with some basic parent-child entities.

public class Client
{
    public Client()
    {
        Projects = new HashSet<Project>();
        Persons = new HashSet<Person>();
    }

    [Key]
    public int ClientId { get; set; }

    [Required]
    [Display(Name = "Client Name", Description = "Client's name")]
    [StringLength(30)]
    public string Name { get; set; }

    public ICollection<Project> Projects { get; set; }
    public ICollection<Person> Persons { get; set; }

}

public class Project
{
    public Project()
    {

    }

    [Key]
    public int ProjectId { get; set; }

    [StringLength(40)]
    public string Name { get; set; }


    public int? ClientId { get; set; }
    public virtual Client Client { get; set; }
}

public class Person
{
    public Person()
    {
        PhoneNumbers=new HashSet<PhoneNumber>();    
    }

    [Key]
    public int PersonId { get; set; }

    [Required]
    [Display(Name="First Name", Description = "Person's first name")]
    [StringLength(15)]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "First Name", Description = "Person's last name")]
    [StringLength(15)]
    public string LastName { get; set; }

    [ForeignKey("HomeAddress")]
    public int? HomeAddressId { get; set; }
    public Address HomeAddress { get; set; }

    [ForeignKey("OfficeAddress")]
    public int? OfficeAddressId { get; set; }
    public Address OfficeAddress { get; set; }

    public ICollection<PhoneNumber> PhoneNumbers { get; set; }

    public int? ClientId { get; set; }
    public virtual Client Client { get; set; }
}

public class Address
{
    [Key]
    public int AddressId { get; set; }

    [Required]
    [StringLength(60)]
    public string StreetAddress { get; set; }

    [Required]
    [DefaultValue("Laurel")]
    [StringLength(20)]
    public string City { get; set; }

    [Required]
    [DefaultValue("MS")]
    [StringLength(2)]
    public string State { get; set; }

    [Required]
    [StringLength(10)]
    public string ZipCode { get; set; }
}

public class PhoneNumber
{
    public PhoneNumber()
    {

    }

    [Key]
    public int PhoneNumberId { get; set; }

    [Required]
    [Display(Name = "Phone Number", Description = "Person's phone number")]
    public string Number { get; set; }

    [Required]
    [Display(Name = "Phone Type", Description = "Type of phone")]
    [DefaultValue("Office")]
    public string PhoneType { get; set; }

    public int? PersonId { get; set; }
    public virtual Person Person { get; set; }
}

我的环境是非常通用的。

My context is very generic.

public class KnockoutContext : DbContext

{
    public DbSet<Client> Clients { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<Person> Persons { get; set; }
    public DbSet<Address> Addresses { get; set; }
    public DbSet<PhoneNumber> PhoneNumbers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }
}

我也有样本数据的一点点 - 尽管它不应该是相关的

I also have a little bit of sample data--though it should not be relevant.

 protected override void Seed(KnockoutContext context)
        {
            base.Seed(context);

            context.Clients.Add(new Client
                                    {
                                        Name = "Muffed Up Manufacturing",
                                        Persons = new List<Person> { 
                                            new Person {FirstName = "Jack", LastName = "Johnson",
                                                PhoneNumbers = new List<PhoneNumber>
                                                    {
                                                        new PhoneNumber {Number="702-481-0283", PhoneType = "Office"}, 
                                                        new PhoneNumber {Number = "605-513-0381", PhoneType = "Home"}
                                                    }
                                            },
                                            new Person { FirstName = "Mary", LastName = "Maples", 
                                                PhoneNumbers = new List<PhoneNumber>
                                                    {
                                                        new PhoneNumber {Number="319-208-8181", PhoneType = "Office"}, 
                                                        new PhoneNumber {Number = "357-550-9888", PhoneType = "Home"}
                                                    }
                                            },
                                            new Person { FirstName = "Danny", LastName = "Doodley", 
                                                PhoneNumbers = new List<PhoneNumber>
                                                    {
                                                        new PhoneNumber {Number="637-090-5556", PhoneType = "Office"}, 
                                                        new PhoneNumber {Number = "218-876-7656", PhoneType = "Home"}
                                                    }
                                            }
                                        },
                                        Projects = new List<Project>
                                                       {
                                                           new Project {Name ="Muffed Up Assessment Project"},
                                                           new Project {Name ="New Product Design"},
                                                           new Project {Name ="Razor Thin Margins"},
                                                           new Project {Name ="Menial Managerial Support"}
                                                       }

                                    }
                );

            context.Clients.Add(new Client
                                    {
                                        Name = "Dings and Scrapes Carwash",
                                        Persons = new List<Person> { new Person {FirstName = "Fred", LastName = "Friday"},
                                            new Person { FirstName = "Larry", LastName = "Lipstick" },
                                            new Person { FirstName = "Kira", LastName = "Kwikwit" }
                                        },
                                        Projects = new List<Project>
                                                       {
                                                           new Project {Name ="Wild and Crazy Wax Job"},
                                                           new Project {Name ="Pimp Ride Detailing"},
                                                           new Project {Name ="Saturday Night Special"},
                                                           new Project {Name ="Soapy Suds Extra"}
                                                       }
                                    }
                );


            IEnumerable<DbEntityValidationResult> p = context.GetValidationErrors();

            if (p != null)
            {
                foreach (DbEntityValidationResult item in p)
                {
                    Console.WriteLine(item.ValidationErrors);
                }
            }
        }

    }

基本上,只要我尝试使用包括从客户端,人员,项目等我得到一个类似的错误上面列出的之一。

Basically, whenever I attempt to use an "Include" from Client, Person, Project, etc. I get a similar error to the one listed above.

namespace KnockoutTest.Controllers
{

    public class ClientController : DbDataController<KnockoutTest.Models.KnockoutContext>
    {
        public IQueryable<Client> GetClients()
        {
            return DbContext.Clients.Include("Persons").OrderBy(o => o.Name);
        }
    }


    public class ProjectController : DbDataController<KnockoutTest.Models.KnockoutContext>
    {
        public IQueryable<Project> GetProjects()
        {
            return DbContext.Projects.OrderBy(o => o.Name);
        }
    }


    public class PersonController : DbDataController<KnockoutTest.Models.KnockoutContext>
    {
        public IQueryable<Person> GetPersons()
        {
            return DbContext.Persons.Include("Client").OrderBy(o => o.LastName);
        }
    }

    public class AddressController : DbDataController<KnockoutTest.Models.KnockoutContext>
    {
        public IQueryable<Address> GetAddresses()
        {
            return DbContext.Addresses.OrderBy(o => o.ZipCode);
        }
    }

    public class PhoneNumberController : DbDataController<KnockoutTest.Models.KnockoutContext>
    {
        public IQueryable<PhoneNumber> GetPhoneNumbers()
        {
            return DbContext.PhoneNumbers.OrderBy(o => o.Number);
        }
    }
}

您可以看到任何理由.NET应该抱怨这个模式?

Can you see any reason why .NET should be complaining about this model?

无论如何,我有什么办法来解决呢?

Regardless, what options do I have to work around it?

感谢您的任何援助!

推荐答案

简短的回答是,淘汰赛,结果,和Entity Framework 4.x版code-先建一个单页应用程序是史蒂夫·桑德森的示范(而伟大!)也许有点误导。这些工具不玩,因为他们第一眼看上去几乎一样很好地在一起。 [剧透:我相信有一个合理的解决方法,但它涉及到不断所以,稍微加强微软领域之外]

The short answer is that Steve Sanderson's demonstration of Knockout, Upshot, and Entity Framework 4.x Code-First to build a Single Page Application was (while great!!!) maybe a little misleading. These tools do not play nearly as nicely together as they appear at first glance. [Spoiler: I do believe there is a reasonable workaround but it involves stepping outside of the Microsoft arena ever-so-slightly.]

(对于史蒂夫的梦幻般的单页应用(SPA)presentation,请访问<一个href=\"http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159\">http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159.这是非常值得的手表。)

(For Steve's fantastic Single Page Application (SPA) presentation, please visit http://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2159. It is well worth a watch.)

在大多数任何Web应用程序,我们在概念需要以以下方式移动,并操纵数据

In most any Web application, We conceptually need to move and manipulate data in the following way:

数据源(通常是一个数据库) - > Web应用程序 - >浏览器

Data Source (often a Database) -> Web Application -> Browser Client

浏览器 - > Web应用程序 - >数据源(通常是一个数据库)

Browser Client -> Web Application -> Data Source (often a Database)

在过去,操作数据从接收它,并将其发送到数据库是一个真正的梦魇。如果你要在.NET 1.0 / 1.1日环游,你可能还记得一个发展的过程,其中包括像步骤:

In the past, manipulating data to receive it from and transmit it to the database was a real nightmare. If you have to be around in the .NET 1.0/1.1 days, you may recall a development process that included steps like:


  • 手动定义数据模型

  • 创建所有的表,建立关系,手动定义索引和约束等。

  • 创建和测试存储过程来访问数据 - 通常手动指定每个字段要包含在每一道工序

  • 创建POCO(普通旧CLR对象)来保存数据

  • code打开与数据库的连接,反复递归返回的每条记录,并将其映射到POCO对象。

此只是将数据进入系统。再回到我们不得不重复几个以相反的顺序这些步骤的其他方式。问题的关键是该数据库的编码是非常耗时的(真的很无聊)。显然,一些code生成和其他工具一起,简化的事情就来了。

This was just to get the data into the system. Going back the other way we had to repeat several of these steps in reverse order. The point is that database coding was very time consuming (and really quite boring). Obviously, a number code generation and other tools came along and simplified things.

真正的突破是与NHibernate,实​​体框架4(code-第一种方法),以及(几乎)完全抽象从开发数据库中其他类似的ORM工具。这些工具不仅增加了开发速度,同时也提高了整体code质量,因为他们人少的机会,错误地引入错误。

The real breakthroughs were with NHibernate, Entity Framework 4 (Code-First approach), and other similar ORM tools which (almost) completely abstracted the database from the developer. These tools not only increased development speed, but also improved overall code quality since their were fewer opportunities to mistakenly introduce bugs.

现在,在许多应用中,连接并与数据库交互(几乎),因为大多数的数据库细节都隐藏起来了事后的想法。

Now, in many applications, connectivity to and interaction with a database is (almost) an afterthought since most database details are hidden away.

微软还提供Upshot.js和的想法,这两种工具,以彼此结合使用​​时,将要革命性的服务器和以同样的方式,浏览器之间的通信的WebAPI该NHibernate和实体框架4已在服务器和数据库之间进行。

Microsoft has also provided Upshot.js and the WebAPI with the idea that these two tools, when used in conjunction with one another, are going to revolutionize the communication between the server and the browser in the same way that NHibernate and Entity Framework 4 have done between the server and the database.

这确实是一个非常值得的成就 - 尤其是客户正在推动更多的交互式Web应用程序

This would indeed be a very worthy accomplishment--especially as clients are pushing for more interactive Web applications.

其中一个主要的障碍是prevents开发从移动更多的用户界面向(浏览器)的客户端的是所需的编码显著量。的一些步骤包括:

One of the main stumbling blocks that prevents developers from moving more of the user interface to the (browser) client is the significant amount of coding required. Some of the steps include:


  • 将数据传输到客户端(通常以JSON格式)

  • 所有从.NET对象的属性映射到JavaScript对象

  • 重新创建所有元数据对对象及其属性

  • 数据绑定到客户端浏览器中的元素

  • 监控更改

  • 重新映射数据(用于发送回服务器)一旦它已被修改

  • 发送数据到服务器

这似乎真的很喜欢似曾相识,因为它是复杂的传统工艺获取数据移入和移出数据库颇为相似。

This really seems quite like "deja vu" because it is quite similar in complexity to the legacy processes for getting data into and out of a database.

根据Web应用的配置方式,可以存在附加的乐趣映射数据一旦它返回到服务器,以实际的数据库对象。 (这将是这种情况往往不是。)

Depending on how the Web application is configured, there may be additional fun mapping the data once it returns to the server to actual database objects. (This will be the case more often than not.)

本服务器 - >客户端 - >服务器的数据传输需要大量的编码,并提供了许多机会,难以预料的挑战,不要忘记它是多么有趣调试JavaScript! (好吧,这是痛苦少现在是几年前比它,但它仍然没有开发者友好在Visual Studio中调试C#code)

This server->client->server data transmission requires a lot of coding and offers many opportunities for unexpected challenges Don't forget how much fun it is to debug JavaScript! (Ok, it's less painful now than it was a couple of years ago, but it is still not as developer-friendly as debugging C# code in Visual Studio.)

在单页应用史蒂夫·桑德森的presentation提供了一个完全不同的(更好的)解决方案。

Steve Sanderson's presentation on Single Page Applications offers a far different (and better) solution.

的想法是,的WebAPI,Upshot.js,和敲除将能够从浏览器的客户端无缝提供数据和接收数据,同时提供高度交互的用户体验。哇!那岂不是让你只是想伸手去拥抱别人?

The idea is that WebAPI, Upshot.js, and Knockout would be able to seamlessly deliver data to and receive data from a browser client while providing a highly interactive user experience. Wow! Doesn't that make you just want to reach out and hug someone?

虽然这个想法并不新鲜,它是第一个认真的努力,我认为已经看到真正做到这一点的.NET之一。

While this idea is not new, it is one of the first serious efforts I think have seen to really do this in .NET.

在数据被通过的WebAPI输送并到达客户端(通过结果),则框架一样敲除将能够使用数据,并提供交互性非常高的水平刀刃Web应用程序需要。 (虽然它可能不会立即清楚,我什么都说明它通过加载页面不主要功能的应用,而是通过AJAX请求主要通信JSON格式的数据。)

Once the data is delivered through the WebAPI and reaches the client (via Upshot), then frameworks like Knockout would be able to consume the data and deliver the very high level of interactivity cutting edge Web applications require. (While it may not be immediately clear, what I describing are applications which do not primarily function by loading "pages" but rather by primarily communicating JSON formatted data through AJAX requests.)

这减少了对所有这些编码中的任何工具显然会通过开发者社区进行快速接受。

Any tool that cuts down on all of this coding is obviously going to be quickly embraced by the developer community.

Upshot.js(RIA / JS的更名和升级版)应该采取若干上面列出的日常任务照顾。它应该是的WebAPI和淘汰赛之间的粘合剂。它的目的是动态映射这些tansmitted在JSON或XML从.NET对象,也暴露出相关的元数据之类的对象属性,所需的字段,字段长度,显示名称,说明等(元数据是是什么使得映射和验证中使用可以访问。)

Upshot.js (the renamed and upgraded version of RIA/JS) was supposed to take care of several of the mundane tasks listed above. It is supposed to be the glue between the WebAPI and Knockout. It is intended to dynamically map objects which are tansmitted in JSON or XML from .NET and also expose the associated meta-data for things like object properties, required fields, field lengths, display names, descriptions, etc. (The meta-data is what allows the mapping and MAY be accessed for use in validation.)

注:我还不确定如何访问结果的元数据,并将其绑到像jQuery验证或验证淘汰赛插件之一验证框架。这是我的待办事项列表测试。

注:我这些类型的元数据的支持不确定性。这是我的待办事项列表上测试。作为一个方面说明,我还计划有元数据的实验System.ComponentModel.DataAnnotations之外也看看NHibernate的属性的支持,以及自定义属性。

所以,在考虑这一切,我开始使用同一套的,史蒂夫在他的演示中使用的一个真实世界的Web应用程序的技术。这些措施包括:

So with all of this in mind, I set out to use the same set of technologies that Steve used in his demo in a real-world Web application. These included:


  • 实体框架4.3使用code-First方法

  • ASP.NET MVC4用的WebAPI

  • Upshot.js

  • Knockout.js

的期望是,所有这些技术会发挥得很好,因为)他们是最新的Microsoft工具(开源淘汰赛除外),也因为史蒂夫·桑德森,目前微软,在各大微软$使用它们放在一起p $ psentation证明单页应用程序的开发。

The expectation is that all of these technologies would function well together because a) they are the latest Microsoft tools (with the exception of the open source Knockout) and because Steve Sanderson, currently of Microsoft, used them together in major Microsoft presentation demonstrating the development of single page application.

不幸的是,我在实践中发现的是,实体框架4.x和Upshot.js以非常不同的方式看​​待这个世界和他们的方向是一定程度上比互补的比较矛盾。

Unfortunately, what I found in practice was that Entity Framework 4.x and Upshot.js view the world in very different ways and their orientations are somewhat more contradictory than complementary.

正如前面提到的,实体框架code首先做一个真正出色的工作,允许开发人员定义功能强大的对象模型,它近乎神奇地转化为功能性的数据库。

As mentioned, Entity Framework Code First does a really fantastic job allowing developers to define highly functional object models which it near-magically translates into a functional database.

一个实体框架4.x的code首先的强大功能是从父对象导航到一个孩子,从孩子的对象导航回其父的能力。这些双向协会是EF的基石。他们节省了大量时间,大大简化开发。此外,微软一再吹捧这个功能伟大的理由使用实体框架。

One of the great features of Entity Framework 4.x Code First is the ability to navigate from a parent object to a child AND navigate from a child object back to its parent. These two-way associations are a cornerstone of EF. They save a tremendous amount of time and greatly simplify development. Moreover, Microsoft has repeatedly touted this functionality as great reason to use Entity Framework.

在斯科特Guthrie的,博客文章(<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/07/16/$c$c-first-development-with-entity-framework-4.aspx\">http://weblogs.asp.net/scottgu/archive/2010/07/16/$c$c-first-development-with-entity-framework-4.aspx)在那里,他开始介绍和解释EF 4 code第一种方法,他演示了双向导航与以下两类概念:

In Scott Guthrie's, blog post (http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx) where he initially introduced and explained EF 4 Code First approach, he demonstrates the concept of two-way navigation with the following two classes:

public class Dinner
{
    public int DinnerID { get; set; }
    public string Title { get; set; }
    public DateTime EventDate { get; set; }
    public string Address { get; set; }
    public string HostedBy { get; set; }

    public virtual ICollection<RSVP> RSVPs { get; set; }
}

public class RSVP
{
    public int RsvpID { get; set; }
    public int DinnerID { get; set; }
    public string AttendeeEmail { get; set; }
    public virtual Dinner Dinner { get; set; }
}

如你所见,晚餐包含回函及回函关联包含一个协会晚宴。有上网本的许多变化发生在其他无数的例子。

As you can see, Dinner contains an association with RSVPs AND RSVPs contains an association to Dinner. There are countless other examples of this on the Internet occurring in many variations.

由于这两种方法协会是实体框架这样的核心功能,一个合理的人可能会认为,微软将支持此功能在库(Upshot.js),它使用从.NET服务器应用带来的数据到客户端。如果功能没有得到支持,很可能东西,他们会想分享,因为这将显著关键架构决策和最希望不与任何正确设计的EF 4 code首先实施工作。

Because these two way associations are such a core feature of Entity Framework, a reasonable person might expect that Microsoft would support this functionality in the library (Upshot.js) it uses to bring data from a .NET server application to the client. If the functionality was not supported, that is likely something they would want to share as it would significantly key architectural decisions and would most like not work with any properly designed EF 4 Code First implementation.

在我的测试code(在上面的原题列出),我自然认为正常的EF code-第一功能(如双向绑定/导航)的支持,因为这正是presentation似乎显示。

In my test code (listed in the original question above), I naturally assumed normal EF Code-First functionality (like two-way binding/navigation) was supported because that is what the presentation appeared to show.

不过,我立刻开始接受像讨厌的小运​​行时错误:

However, I immediately started receiving nasty little run-time errors like:

类型对象图
  System.Collections.Generic.HashSet`1 [KnockoutTest.Models.Person,
  KnockoutTest,版本= 1.0.0.0,文化=中立,公钥=空]]'
  包含周期,如果引用跟踪是不能被序列化
  已禁用。

"Object graph for type 'System.Collections.Generic.HashSet`1[[KnockoutTest.Models.Person, KnockoutTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled."

我试过很多不同的方法来尝试解决问题。根据我的想法和我的阅读,这里有一些失败的解决方案,我尝试的。

I tried numerous different approaches to try and fix the problem. Based on my ideas and my reading, here are a few of the failed solutions I attempted.


  • 移除从关系的一侧的关联。这不是一个很好的解决方案,因为它是非常方便的是在父母和孩子之间的每个方向能导航。 (这可能就是为什么这些关联属性的导航属性简称)。取出的关系,任何一方有副作用。当从父除去的关系,也除去导航孩子的列表的能力。当从孩子辗转的关系,.NET为我提供了另一种友好的错误。

无法检索关联关联信息
  KnockoutTest.Models.Client_Persons。只有包括外国的模式
  关键信息的支持。请参阅实体框架文件
  创建模型,包括外键信息的详细信息。

"Unable to retrieve association information for association 'KnockoutTest.Models.Client_Persons'. Only models that include foreign key information are supported. See Entity Framework documentation for details on creating models that include foreign key information."


  • 以防万一的问题是该系统成为困惑那里是一个外键的结果,我明确规定对儿童实体[ForeignKey的]属性。一切编译,但.NET返回对象图表类型...包含周期,不能被序列化。

  • Just in case the issue was the result of the system becoming confused about there being a foreign key, I explicitly specified a [ForeignKey] attribute on the child entity. Everything compiles but .NET returns the "Object graph for type... contains cycles and cannot be serialized..."

    我的一些阅读表明,添加像[DataContract(IsReference =真)]在WCF可能保持.NET从弄不清循环引用的属性。这时候,我得到这个美丽的。

    Some of my reading indicated that adding an attribute like [DataContract(IsReference = true)] in WCF might keep .NET from getting confused about cyclical references. That's when I get this beauty.

    类型KnockoutTest.Models.Person'不能被序列化到JSON
      因为它的IsReference设置为真。 JSON格式不
      支持引用因为无标准格式
      再presenting引用。要启用系列化,禁用
      IsReference的类型或适当的父类的设置
      键入

    "The type 'KnockoutTest.Models.Person' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type."

    此错误是非常重要的,因为它基本上告诉我们,我们不打算要能够在他们的正常配置一起使用结果和Entity Framework code-第一。为什么?实体框架被设计为利用双向绑定。然而,当双向绑定被实现,结果说,它不能处理循环引用。当循环引用的管理,结果基本上说,它不能处理父和子对象之间的引用,因为JSON不支持它。

    This error is very important because it basically tells us that we NOT going to be able to use Upshot AND Entity Framework Code-First together in their normal configuration. Why? Entity Framework is designed to utilize two-way binding. However, when two-way binding is implemented, Upshot says that it cannot handle cyclical references. When cyclical references are managed, Upshot basically says that it cannot handle references between parent and child objects because JSON doesn't support it.

    当我看着史蒂夫的演示,我回忆说,他确实有客户和交付之间的关系。我决定回去,并采取在他的对象模型更仔细看看。

    When I watched Steve's demo, I recalled that he DID have a relationship between Customers and Deliveries. I decided to go back and take a much closer look at his object model.

    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }
    
    public class Delivery
    {
        // Primary key, and one-to-many relation with Customer
        public int DeliveryId { get; set; }
        public virtual int CustomerId { get; set; }
        public virtual Customer Customer { get; set; }
    
        // Properties for this delivery
        public string Description { get; set; }
        public bool IsDelivered { get; set; } // <-- This is what we're mainly interested in
    

    我们发现的是,在史蒂夫的演示是,他只关系去的一种方式,并将其绑定孩子的父母,而不是父母给孩子。

    What we find is that in Steve's demo is that his relationship only goes one way, and it binds the child to the parent rather than the parent to the children.

    在本演示中,它种工作。然而,在许多实际应用中,这种方法使数据访问是不切实际的。举个例子,演示场景我包含在我原来的问题。我们有:

    In this demo, it kind of works. However, in many real-world applications, this approach makes data access impractical. Take, for example, the demo scenario I included in my original question. We have:

    Clients
        Projects
        Persons
            Addresses
            PhoneNumbers
    

    大多数开发人员,我想也不会想开始从地址或电话号码的查询。他们会希望能够选择客户或项目或人员名单,然后导航到其后代的列表。

    Most developers, I think would not want to start their query from addresses or phone numbers. They would expect to be able to select a list of clients or Projects or Persons and then navigate to a list of its descendants.

    我不是100%肯定这是不可能使用具有双向绑定启用的实体,但我不知道这是可能只使用微软工具,以产生成功的任何配置。

    I am not 100% certain that is impossible to use entities which have two-way binding enabled, but I am unaware of any configuration that is likely to yield success using only the Microsoft tools.

    幸运的是,我认为有一个解决方案(这需要照顾的循环依赖问题),我计划在未来几天内进行测试。该解决方案... JSON.Net

    Fortunately, I do think there is a solution (which takes care of the cyclic dependency issue) which I plan to test in the next few days. That solution is... JSON.Net.

    JSON.Net支持循环依赖,并保持到子对象的引用。如果它按预期工作,它会采取两个我在我的测试得到了错误的照顾。

    JSON.Net supports cyclic dependencies and maintains references to child objects. If it works as expected, it will take care of two of the errors I got in my testing.

    有一次,我已经测试过,我会在这里报告结果。

    我认为史蒂夫的演示是辉煌的,我喜欢他的演示。我相信,淘汰赛是惊人的。我是,微软似乎是想非常感谢。如果在工具值得注意的限制,我认为微软也许应该已经与他们更到位。

    I think Steve's demo was brilliant, and I loved his demo. I believe Knockout is amazing. I am very thankful for where Microsoft seems to be going. If there are noteworthy limitations in the tool, I think Microsoft maybe should have been more forthcoming with them.

    我并不想过于批判微软(绝对不是史蒂夫在所有的关键),因为我认为他们已经做了出色的工作。我喜欢什么结果的承诺,我期待着看到哪里就有奇迹。

    I don't mean to be overly critical of Microsoft (and definitely am not critical of Steve at all) because I think they have done a wonderful job. I love what upshot promises and am looking forward to seeing where it goes.

    我更喜欢看到有人拿结果并重新因子它(和的WebAPI),以便它可以与实体框架完全集成,无需使用第三方工具。

    I would really love to see someone take upshot and re-factor it (and the WebAPI) so that it can integrate fully with Entity Framework without the use of a third-party tool.

    我不知道是否类似的工具存在NHiberbnate,但我很想甚至看到有人伸出结果与集成(或开发一个类似的库)NHibernate的。通过扩展,我主要是说从消费NHibernate的元数据。

    I don't know if a similar tool exists for NHiberbnate, but I would love to maybe even see someone extend upshot to integrate with (or develop a similar library for) NHibernate. By extend, I am primarily talking about consuming the meta-data from NHibernate.

    当我测试JSON.Net,我还计划测试NHibernate的为好。

    这篇关于什么使实体框架/结果相信我的对象图&QUOT;包含周期&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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