C#Entity Framework在提交(SaveChanges)

时间:2016-04-26 10:24:21

标签: c# entity-framework-6

我们在系统中发现了一个错误。我们正在使用实体框架6.看来,EF在提交后没有更新或刷新(saveChanges())。

过程:

向EntityFramework添加项目:

ShareRefund e = new ShareRefund()
        {
            Client_id = message.Client_id,
            SubcriptionCertificate_id = suct.Id,
            DTAUS = message.DTAUS,
            Accounting_id = deposit.Id,
            Amount = maxRefund,
            Created_id = message.Created_id,
            Employee = _context.Employees.FirstOrDefault(p=> p.Id == message.Employee_id),
            SubscriptionCertificate = suct,
            Accounting = deposit,
            Client = _context.Clients.FirstOrDefault(p => p.Id == message.Client_id),
            Login = _context.Logins.FirstOrDefault(p => p.Id == message.Created_id),
            CreatedDate = DateTime.Now,
            CustomId = suct.CustomId,
            Employee_id = message.Employee_id,
            NoInterest = message.NoInterest,
            RefundDate = message.RefundDate

        };

        _context.ShareRefunds.Add(e);

在此之后我们致电_context.Commit(); 其定义如下:

public void Commit() {
        try {
            this.SaveChanges();
        }
        catch () {
            some code here
        }
    }

在完成方法之前,我们进入另一个类并使用_context的新实例。 我们希望使用以下内容获取添加的项e

int refundedId = shareRefundId.First();
ShareRefund refund = context.ShareRefunds
   .Where(t => t.Client_id == client.Id)
       .FirstOrDefault(t => t.Id == refundedId);

当我使用调试器进入它并在此时查看我的数据库时,我的数据库中存在具有给定id的条目。但_context始终返回null。

提交后是否有任何已知问题或解决方案正确刷新上下文?

这是我的构造函数,其中创建了`context的新实例:

private readonly Context context;

    #region constructor/destructor

    public ReportingManager() {
        context = new Context("Context");
        workingDirectory = String.Format(workingDirectory, InstanceConfiguration.DefaultAppFolder);

        configDirectory = workingDirectory + @"configs\";
        templateDirectory = workingDirectory + @"templates\";
        temporaryDirectory = workingDirectory + @"temp\";
    }

    public ReportingManager(string connectionString, string appFolder) {
        context = new Context(connectionString);
        workingDirectory = String.Format(workingDirectory, appFolder);

        configDirectory = workingDirectory + @"configs\";
        templateDirectory = workingDirectory + @"templates\";
        temporaryDirectory = workingDirectory + @"temp\";
    }

0 个答案:

没有答案