хранение значений в базе данных - PullRequest
0 голосов
/ 29 марта 2012

Мне нужно добавить несколько записей в таблицу «StaffSectionInCharge», в ней есть только два столбца StaffId и SectionId, у меня есть значения StaffId и StudentId ..... проблема в том, что я не могу добавить записи непосредственно к этому таблица ..... я использую сущность рамки и дизайн этой таблицы

[EdmRelationshipNavigationPropertyAttribute("Model", "StaffSectionInCharge", "Section")]
    public EntityCollection<Section> Sections
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Section>("Model.StaffSectionInCharge", "Section");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Section>("Model.StaffSectionInCharge", "Section", value);
            }
        }
    }

Мне нужно получить доступ к этой таблице через таблицу персонала, я пробовал как

DataAccess.Staff staff = buDataEntities.Staffs.First(s=>s.StaffId==StaffId);
                staff.Sections.Add();

я застрял здесь, и не могу двигаться дальше, может кто-нибудь помочь мне здесь

1 Ответ

1 голос
/ 29 марта 2012

Вы можете попробовать:

Staff staff = buDataEntities.Staffs.First(s => s.StaffId == StaffId);
Section section = buDataEntities.Sections.First(s => s.SectionId == SectionId);

staff.Sections.Add(section);

buDataEntities.SaveChanges();

Или (для которого не требуется второй запрос к базе данных):

Staff staff = buDataEntities.Staffs.First(s => s.StaffId == StaffId);

Section section = new Section { SectionId = SectionId };
buDataEntities.Sections.Attach(section);

staff.Sections.Add(section);

buDataEntities.SaveChanges();

Или (для которого не требуется запрос кбаза данных вообще):

Staff staff = new Staff { StaffId = StaffId };
buDataEntities.Staffs.Attach(staff);

Section section = new Section { SectionId = SectionId };
buDataEntities.Sections.Attach(section);

staff.Sections.Add(section);

buDataEntities.SaveChanges();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...