0

私のモデルで CRUD 関係を m:m にする方法は?

例:

マイ テーブル

  • 人 (PeopleId、名前)
  • モノ (ThingId、名前)
  • PeopleHasThing (PeopleId、ThingId)

私のモデル

PeopleModel.cs:

    public int PeopleId { get; set; }
    public string Name
    {
        get { return _name; }
        set
        {
            if(value == _name) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public List<ThingModel> HasThing
    {
        get { return _hasThing; }

        set
        {
            if(value == _hasThing) return;
            _hasThing= value;
            OnPropertyChanged("HasThing");
        }
    }

    public static int Insert(PeopleModel m)
    {
        using (_context = new myDataContext())
        {
            var d = new People
            {
                Name = m.Name
                Thing = // I don't know how to end this line
            };

            _context.People.InsertOnSubmit(d);
            _context.SubmitChanges();

            return d.PeopleId;
        }
    } 

    // I don't know how to update or retrieve this from the database
    public static void Update(PeopleModel m);
    public static void ListAll();

    // With this I dont have problem! :P
    public static void Delete(int peopleId);

PeopleHasThingModel.cs

    public int PeopleId { get; set; }
    public int ThingId  { get; set; }

ThingModel.cs

    public int ThingId { get; set; }

    public string Name
    {
        get { return _name; }
        set
        {
            if(value == _name) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public bool IsMarked
    {
        get { return _isMarked; }

        set
        {
            if(value == _isMarked) return;
            _isMarked= value;
            OnPropertyChanged("IsMarked");                
        }

    }

モノは私のフォームのリストであり、モノのテーブルから入力されたチェックボックスのリストです。

thingsテーブルに 3 つのレコードがあります。

1, "house"
2, "dog"
3, "car"

私は新しいものpeopleと彼女thingのものを保存する必要があります:

人々:

   1, "ruben"

PeopleHasThing:

   1, 1 -- ruben has house
   1, 3 -- ruben has car

早期アプローチ

PeopleModel.cs

    public static int Insert(PeopleModel m)
    {
        using (_context = new myDataContext())
        {
            var people = new People
            {
                Name = m.Name
            };

            // Create the PeopleHasThing record
            m.HasThing.ForEach((e) =>
            {
                people.PeopleHasThing.Add(new PeopleHasThing
                {
                    ThingId = e.ThingId,
                    People = people
                });
            });


            _context.People.InsertOnSubmit(people);
            _context.SubmitChanges();

            return people.PeopleId;
        }
    }

    // The following method works!
    // I've tried but I have my doubts about deleting records
    public static void Update(PeopleModel p)
    {
        using (_context = new myDataContext())
        {
            var result = (from r in _context.People
                         where r.PeopleId == p.PeopleId
                         select r).SingleOrDefault();

            if (null == result) return;

            result.Name = p.Name;

            //Delete all PeopleHasThings with result.PeopleId ...

            EntitySet<PeopleHasThing> set = new EntitySet<PeopleHasThing>();
            m.HasThing.ForEach(e =>
            {
                if (e.IsMarked)
                {
                    set.Add(new PeopleHasThing
                    {
                        ThingId = e.ThingId,
                        People = result
                    });
                }
            });

            result.PeopleHasThing = set;

            _context.SubmitChanges();
        }
    }
4

1 に答える 1

0

3 つのモデルは次のようになります。

class PeopleModel {
  public int PeopleID { get; set; }
  public string Name  { get; set; }
  /* .. additional fields or values .. */
}

class ThingsModel {
  public int ThingsID { get; set; }
  public string Name  { get; set; }
  /* .. additional fields or values .. */
}

class PeopleHasThing {
  public int PersonID { get; set; }
  public int ThingsID { get; set; }
}

Peopleテーブルにに関連するフィールドのみを格納し、 Thingsテーブルにに関連するフィールドのみを格納することにより、3 テーブル モデルでこれにアプローチできます。

次に、必要な数の行を含む PeopleHasThing テーブルにすべての関係を格納します。

People & Things を作成/編集する際には、関係を構築するために ID 番号を知る必要があります

PeopleModel myPerson = new PeopleModel();
myPerson.PeopleID // ID of Person
ThingsModel myThing = new ThingsModel();
myThings.ThingsID // ID of Thing

PeopleHasThings relationship = new PeopleHasThings();
relationship.PeopleID = myPerson.PeopleID;
relationship.ThingsID = myThings.ThingsID;

人々:

1, "ruben"
2, "joe"

もの:

1, "house"
2, "dog"
3, "car"

PeopleHasThing:

1, 1 -- ruben has house
1, 3 -- ruben has car
2, 1 -- joe has house
于 2012-07-11T03:17:52.657 に答える