2

問題を再現するためのサンプル コードを次に示します。

顧客クラス

public class Customer
{
    private int _id;

    [Column("_id"), PrimaryKey, AutoIncrement]
    public int Id
    {
        get { return _id; }
        set
        {
            if (value != _id)
            {_id = value;}
        }
    }

   private string _name;

   [Column("_name")]
   public string Name
   {
       get { return _name; }
       set
       {
           if (value != _name)
           {_name = value;}
       }
   }

   private List<Order> _orders;
   [OneToMany(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead | CascadeOperation.CascadeDelete)]
   public List<Order> Orders
   {
       get { return _orders; }
       set
       {
           if (_orders != value)
           {
               _orders = value;
           }
       }
   } 

注文クラス

public class Order
{
    private int _id;

    [Column("_id"), PrimaryKey, AutoIncrement]
    public int Id
    {
        get { return _id; }
        set
        {
            if (value != _id)
                _id = value;
        }
    }

    private string _name;

    [Column("_name")]
    public string Name
    {
        get { return _name; }
        set
        {
            if (value != _name)
                _name = value;
        }
    }

    private int _customerId;

    [Column("_customerId"), ForeignKey(typeof(Customer))]
    public int CustomerId
    {
        get { return _customerId; }
        set
        {
            if (value != _customerId)
                _customerId = value;
        }
    }
}

DB操作

Customer customer = new Customer(){Name = "Customer One"};
context.InsertWithChildren(customer, true);
customer.Orders = new List<Order>();
customer.Orders.Add(new Order(){Name="Sample order"});
context.UpdateWithChildren(customer);

// get a new copy from the db
var result = context.GetWithChildren<Customer>(customer.Id,true); 

List<Order> orders = result.Orders; // Orders.Count is 0

ここで何か間違ったことをしていますかUpdateWithChildren、それともこのように動作するはずですか?

編集

DBに存在しない場合UpdateWithChildren、新しいものを挿入しないように見えます。Order最初に注文を挿入し、それを顧客に割り当ててから、電話をかけることUpdateWithChildrenで関係が確立されます。

Customer customer = new Customer(){Name = "Customer One"};
context.InsertWithChildren(customer, true);

List<Order> newOrders = new List<Order>();
newOrders.Add(new Order(){Name="Test order"});
context.InsertAllWithChildren(newOrders,true);

customer.Orders = newOrders;

context.UpdateWithChildren(customer);

var result = context.GetWithChildren<Customer>(customer.Id,true);

List<Order> orders = result.Orders; // Orders.Count is 1

私はこれが私がそれを行うことになっている方法だと思いますか?

4

1 に答える 1

7

すでに気づいたUpdateWithChildrenように、新しいオブジェクトはデータベースに挿入されません。関係を更新するだけです。InsertOrReplaceWithChildrenオブジェクトを挿入または更新する場合は、最初にオブジェクトを使用または挿入してから関係を更新できます。

または、再帰的な挿入操作を使用できます。

Customer customer = new Customer(){
    Name = "Customer One",
    Orders = new List<Order>{ new Order(){ Name="Test order" } }
};

// Recursively insert 'customer' and all its orders
context.InsertWithChildren(customer, true);
于 2015-01-18T10:36:52.307 に答える