entity-framework - 对象层类型 'MyProject.Model.Applications' 已经有一个生成的代理类型

标签 entity-framework model-view-controller ef-code-first dbcontext

我正在使用代码优先的方法。我正在设置一个过程,每天将表行从 sourcedb 复制到 targetdb。此外,我需要在两者中维护主键值。 (这是必需的)即两个数据库都应该对给定的行具有相同的主键。

我通过引用相同的类创建了 2 个不同的上下文。两个上下文都完好无损。我正在将 sourcedb 中的所有行放入一个对象列表中,并将其传递给另一个上下文以将该范围插入到 targetdb 中。但是在这样做的同时,我收到的错误是' 已经为对象层类型“MyProject.Model.Applications”生成了一个代理类型。当 AppDomain 中的两个或多个不同模型映射相同的对象层类型时会发生这种情况 .

我检查了一些其他链接。但到目前为止一切都没有奏效。我也查了is it possible to share POCO object between two DbContext? .

以下是一些伪代码,

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(6000)))
    {
        using (var dbContext = new SourceDbContext())  
        {

        DateTime dateToBeCompared = DateTime.UtcNow.Date.AddMonths(-11);
        dbContext.Configuration.LazyLoadingEnabled = false;
        dbContext.Configuration.AutoDetectChangesEnabled = false;
            //get data from application related tables.
            var applications = dbContext.Applications.AsNoTracking().Where(a => a.UpdatedOn <= dateToBeCompared)
                                    .ToList();
            using (var connection1 = new System.Data.SqlClient.SqlConnection("TargetDbConnectionString"))
            {
                connection1.Open();
                using (var targetDbContext = new TargetDbContext(connection1, true))
                    using (TransactionScope tsSuppressed = new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        targetDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Applications] ON");
                    }

                    try
                    {
                        targetDbContext.Applications.AddRange(applications);
                        targetDbContext.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }

                    using (TransactionScope tsSuppressed = new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        targetDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Applications] OFF");
                    }
                }

                connection1.Close();
            }
        scope.Complete();
    }

还有一些外键约束。但无论那里有什么,这两种情况都是一样的。

最佳答案

都没有 dbContext.Configuration.LazyLoadingEnabled = false;也不是 AsNoTracking()阻止 EF 在源上下文中创建代理对象,从而阻止在另一个上下文中使用它们。

你真正需要的是转 ProxyCreationEnabled 离开:

Gets or sets a value indicating whether or not the framework will create instances of dynamically generated proxy classes whenever it creates an instance of an entity type. Note that even if proxy creation is enabled with this flag, proxy instances will only be created for entity types that meet the requirements for being proxied. Proxy creation is enabled by default.



它还可以防止延迟加载,因为它依赖于拦截虚拟属性的代理类。所以只需更换
dbContext.Configuration.LazyLoadingEnabled = false;


dbContext.Configuration.ProxyCreationEnabled = false;

问题将得到解决。请注意,这并不意味着代码将正常工作。实体必须具有明确的 FK 定义(不仅仅是导航属性),并且必须首先处理相关实体以防止违反 FK 约束。

关于entity-framework - 对象层类型 'MyProject.Model.Applications' 已经有一个生成的代理类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42689121/

相关文章:

c# - DDD - 如何在 EF Core 中跨有界上下文管理实体

php - 将应用程序与应用程序库、共享库、组件等分离

ruby-on-rails - 在使用 cancancan 时添加没有对应模型的 Controller

c# - 使外键(字符串字段)可为空

c# - "EntityType has no key defined"异常,尽管键是用 HasKey 定义的

c# - Entity Framework - 带有参数列表 <> 的 SqlQuery

c# - EntityFramework Fluent API 外键排序

c# - 将 MVC 项目从本地迁移到 Azure - SQL Server 问题

QT:QTableView中行的内部拖放,改变QTableModel中行的顺序

linq - 如何使用流利的 linq 获取当前不在映射表中的对象