Entity Framework 4.1 Code First:关于从外部源持久保存数据的建议?

发布于 2024-11-09 15:39:06 字数 914 浏览 0 评论 0原文

我的项目的一部分是保留来自其他来源的数据。在本例中,我们有一个 SAP 数据源,需要从中提取数据。我需要从 SAP 获取数据并将其映射到我的应用程序中的实体。以下是我的应用程序中的实体示例:

    public class Project : BaseEntity
    {
        public string Name { get; set; }
        public string ProjectNumber { get; set; }
        public string Description { get; set; }

        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public string Currency { get; set; }

        #region Navigation Properties

        public virtual Address Address { get; set; }
        public virtual CompanyCode CompanyCode { get; set; }
        public virtual ICollection<Contact> TeamMembers { get; set; }

        #endregion
    }

如您所见,我也有从 SAP 映射的子对象。我需要一些有关插入和更新实体的最佳方法的建议。我很难知道何时将实体添加(插入)到我的上下文以及何时附加(更新)它们,因为 SAP 不知道我的应用程序可能有或没有什么。我也需要防止重复。例如,在将它们应用到父实体之前,我是否应该对父实体中的每个子实体执行查找以查看它们是否存在?然后,将整个父对象添加/附加到上下文或单独处理每个实体,同时仍然保持它们的关系?

Part of my project is to persist data from another source. In this case we have an SAP data source that we will need to pull data from. I need to take the data from SAP and map it to entities I have in my application. Here is an example of an entity I have in my application:

    public class Project : BaseEntity
    {
        public string Name { get; set; }
        public string ProjectNumber { get; set; }
        public string Description { get; set; }

        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public string Currency { get; set; }

        #region Navigation Properties

        public virtual Address Address { get; set; }
        public virtual CompanyCode CompanyCode { get; set; }
        public virtual ICollection<Contact> TeamMembers { get; set; }

        #endregion
    }

As you can see, I have child objects that I map from SAP as well. I need some advice on the best way to insert and update my entities. I am struggling with knowing when to add (insert) entities to my context and when to attach (update) them, because SAP doesn't have knowledge of what my application may or may not have. I need to guard against duplicates, too. For example, should I perform a lookup of each child entity in my parent entity to see if they exist before I apply them to the parent? Then, add / attach the entire parent object to the context or handle each entity separately while still maintaing their relationships?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-11-16 15:39:06

是的,您必须手动测试所有内容才能做出正确决定必须插入、更新或删除哪些内容。根据应用程序,您可以使用一些更复杂的查询来减少数据库的往返次数 - 例如,您可以使用带有 Contains 的单个查询来加载所需的所有 TeamMembers处理Project,或者如果您还需要测试项目是否存在,您可以加载Project并包含所有相关数据。

我之前做过大型同步应用程序,最终我在开始时预加载了所有实体,几乎没有查询,并且完全在内存中工作。

不要忘记使用 DbSet 的 Local 属性或 Find 方法来利用已加载的实体。

您还可以使用一些自定义存储过程来提高此操作的性能。

Yes you must manually test everything to make correct decision what must be inserted, updated or deleted. Depending on the application you can use some more complex queries to reduce number of round trips to the database - for example you can use single query with Contains to load all TeamMembers needed for processed Project or you can load Project with including all related data if you also need to test if project exists.

I did large synchronization application before and I end up with pre-loading all entities at the beginning with few queries and working completely in memory.

Don't forget to use DbSet's Local property or Find method to take advantage of already loaded entities.

You can also use some custom stored procedures to improve performance of this operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文