如何制作CRUD m:m关系c#

时间:2012-07-11 01:46:35

标签: c# model

如何在我的模型中建立m:m的CRUD关系?

示例:

我的桌子

  • 人(PeopleId,姓名)
  • Thing(ThingId,Name)
  • 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();
        }
    }

1 个答案:

答案 0 :(得分: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; }
}

您可以通过仅包含表格中的相关字段来使用3表模型进行处理。 Things 表中只有 thing 相关字段。

然后,您将根据需要在PeopleHasThing表中包含所有关系。

在您创建/编辑人物时事情,你需要知道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