0

ASP.NETMVC3アプリでコードファーストの手法を使用しています。ここでは、非常に基本的な問題、つまりナビゲーションプロパティを更新する方法があります。以下は詳細なコードです。

    public class Destination
    {
        public int ID {get;set;}
       // some other properties
        public Country {get;set;}
    }

    public class Country
    {
     int ID {get;set;}
     string Name {get;set;}
    }

    //i have simple structure as above. when i go to update destination entity. Country is not getting updated.even i tried following:

    _db.Entry(Destination.Country).State = System.Data.EntityState.Modified;
    _db.Entry(Destination).State = System.Data.EntityState.Modified;
    //_db.ChangeTracker.DetectChanges();
    _db.SaveChanges();

第二に、私が追加に行くとき、それはうまくいきます。foriegnKeyの関係を明示的に要求する必要はありますか?

4

1 に答える 1

0

エンティティは次の3つの方法で追加できます。

Add()DbSetのメソッドを呼び出します。これにより、エンティティは追加状態になります。つまり、次にSaveChanges()呼び出されたときにデータベースに挿入されます。

context.Destination.Add(yourDestination);

状態を追加に変更します。

context.Entry(yourDestination).State = EntityState.Added;

すでに追跡されている別のエンティティに接続して、コンテキストに新しいエンティティを追加する

context.Destination.Country.Add(yourCountry);

よろしく。

于 2012-06-03T11:32:04.953 に答える