문제

이 테이블이 있다고 가정합니다.

Fruits
 - FruitID       INT PK
 - FruitName     NVARCHAR(30)
 - FruitStatusID INT FK: Statuses

Statuses
 - StatusID      INT PK
 - StatusName    NVARCHAR(30)

이러한 상황에서 과일 이름과 데이터베이스의 상태를 모두 업데이트하려면 어떻게해야합니까? :

  1. 이전 L2E 전화에서 나온 과일을 업데이트하십시오.
  2. Fruitid에 해당하는 정수가 주어지면 (즉, 처음에는 과일 대상이 없습니다).

VB 또는 C#은 괜찮습니다. 감사합니다!

도움이 되었습니까?

해결책

This works but isn't what I was hoping for:

int StatusID = 4; // Some ID
Fruit.FruidIDReference.EntityKey = 
    New EntityKey("MyEntities.Statuses", "StatusID", StatusID)

Surely there's a cleaner way to do this that doesn't require hard-coding strings (which introduces run-time exceptions if I make a typo).

다른 팁

If a Fruit object is passed on to another method, you can:

  1. Pass a reference to the context object along with the Fruit and call SaveChanges()

  2. Make your edits to the Fruit object in the downlevel method and call SaveChanges() in the calling method (you can check to see if it has been modified if you want to avoid unnecessary DB calls.)

Code:

//Change the name
FruitEntities fe = new FruitEntities();
Fruit f = fe.Friuts.First();
f.FruitName = "NewName";
fe.SaveChanges();

//Get a fruit by ID and change the status
//Statuses will need to be included as an Entity in your model with an association to Fruits
int Id = 2;
int newStatusID = 0;
FruitEntities fe = new FruitEntities();
Fruit f = (from x in fe.Fruits
           where x.FruitID == Id
           select x).First();
Status s = (from y in fe.Statuses
            where y.StatusID = newStatusID
            select y).First();
f.Status = s;
fe.SaveChanges();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top