简体   繁体   English

如何在EF中创建和保存序列化然后反序列化的实体

[英]How to create and save a serialized and then deserialized entity in EF

I have the follow flow: 我有以下流程:

  1. Entity is created 实体已创建
    • Current context is closed 当前上下文已关闭
  2. Updated 更新
    • By property setters, also by navigation properties 通过属性设置器,也通过导航属性
  3. Put in ViewState 放入ViewState
    • Serialized, type marked with the [Serializable] attribute) 序列化,类型标记为[Serializable]属性)
  4. Read from ViewState 从ViewState读取
    • Deserialized 反序列化
  5. Save
    • New context is open 新的环境是开放的

How should I properly create an entity: stub (by default constructor) or DbSet<T>.Create() ? 我应该如何正确创建一个实体:存根(默认为构造函数)或DbSet<T>.Create()

How should I properly save the entity: DbSet<T>.Add() or DbSet<T>.Attach() ? 如何正确保存实体: DbSet<T>.Add()DbSet<T>.Attach()

I'm getting various exceptions: 我遇到各种异常:

  • "Violation of PRIMARY KEY constraint 'PK_currency_types'. Cannot insert duplicate key in object 'dbo.currency_types'. The duplicate key value is (1). The statement has been terminated." “违反了PRIMARY KEY约束'PK_currency_types'。无法在对象'dbo.currency_types'中插入重复键。重复键值为(1)。该语句已终止。”

  • "A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship." “发生了引用完整性约束冲突:定义引用约束的属性值在关系中的主体对象和从属对象之间不一致。”

I'm using EF 4.3 Database First (I have database, designer but entities are auto-generated POCOs) with DbContext Generator extension. 我正在使用带有DbContext Generator扩展的EF 4.3 Database First(我有数据库,设计器,但实体是自动生成的POCO)。 I'm new to EF and completely stuck. 我是EF的新手,完全陷入困境。

Update: Here's my code, it's simple so I haven't added it from the beginning: 更新:这是我的代码,很简单,因此我从一开始就没有添加它:

public IStatement Create()
{
    using (var db = new ModelContainer())
    {
        // ID is auto-generated by db, INT IDENTITY(1,1)
        return new Statement();
        // or
        // return b.Statement.Create();
    }
}

// somewhere in the middle, for example:    
statement.Currency = db.Currency.Single(c => c.Name == "Euro");
statement.Amount = 1000;

public void Save(IStatement[] statement)
{
    using (var scope = new TransactionScope())
    using (var db = new ModelContainer())
    {
        foreach (var s in statement)
        {
            // statement has a number of navigation properties, i.e. referenced by FK entities

            // need to add/attach each back to db                
        }

        db.SaveChanges();
        scope.Complete();
    }
}

} }

var statement = new Statement();

using (var scope = new TransactionScope())
{
    var result = 0;
    foreach (var statement in arr.Cast<Statement>())
    {
        using (var db = new ModelContainer())
        {
            db.StatementTypes.Attach(statement.StatementType);
            db.Entry(statement.StatementType).State = EntityState.Unchanged;

            db.Currencies.Add(statement.Currency);
            db.Entry(statement.Currency).State = EntityState.Unchanged;

            db.Subjects.Attach(statement.Firm);
            db.Entry(statement.Firm).State = EntityState.Unchanged;

            db.Statement.Add(statement);

            db.SaveChanges();
        }
    }
    scope.Complete();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM