实体框架将现有Child添加到多对多关系的新Parent

时间:2017-07-31 06:47:47

标签: c# entity-framework

如果要将现有的Child添加到新的Parent(1对1或1-n关系),首先使用代码,您可以在Parent中定义Child,而ChileId和EF将该id自动映射到子级。 有没有办法在多对多关系中做同样的事情?

Parent
{
  int    ChildId {get;set;}
  aClass Child {get;set;}
}

架构数据: 实体框架,代码优先。 后端webapi / restfull断开连接UI将ParentData映射到ParentEntity子集合将类似于" countires",所以我不想添加新的,但只是将多个countires与Parent相关联。用户界面上有一个多选下拉列表,因此您可以选中/取消选中国家/地区。

e.g。

与美国,英国有关的家长

然后在UI,有人也检查ESP 3将与父

相关

2 个答案:

答案 0 :(得分:1)

在多对多中,使用ID而不是整个对象并不容易。

请考虑以下事项:

int existingChildId; // some value
var childPlaceholder = new Child { Id = existingChildId };

db.Children.Attach(childPlaceholder);

var newParent = new Parent();
newParent.Children.Add(childPlaceholder);
db.Parents.Add(newParent);

db.SaveChanges();

如果未加载现有子项(并且不希望预加载),则可以附加仅具有ID的子项以建立关系:

local

如果您不知道子项是否已在上下文中加载,并且您仍希望避免数据库查询加载它,请检查int existingChildId; // some value var childPlaceholder = db.Children.Local.FirstOrDefault(x => x.Id == existingChildId) ?? db.Children.Attach(new Child { Id = existingChildId }); // placeholder is ready to use 条目:

STA

答案 1 :(得分:0)

public function mainAction($displayedUser = "", $postSummarys = "", $topicIcons = "", $forumIcons = "", $displayedTopics = "", $displayOnlinebox = 0, $displayedPosts = "", $displayedForumMenus = "", $displayedAds = "") {
            // json array

        $content = [];

        if (!empty($_POST['tx_typo3forum_ajax']['displayedUser'])) {
            $displayedUser = str_replace('"', '', $_POST['tx_typo3forum_ajax']['displayedUser']);
            $content['onlineUser'] = $this->_getOnlineUser($displayedUser);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['displayedForumMenus'])) {
            $displayedForumMenus = str_replace('"', '', $_POST['tx_typo3forum_ajax']['displayedForumMenus']);
            $content['forumMenus'] = $this->_getForumMenus($displayedForumMenus);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['postSummarys'])) {
            $postSummarys = str_replace('"', '', $_POST['tx_typo3forum_ajax']['postSummarys']);
            $content['postSummarys'] = $this->_getPostSummarys($postSummarys);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['topicIcons'])) {
            $topicIcons = str_replace('"', '', $_POST['tx_typo3forum_ajax']['topicIcons']);
            $content['topicIcons'] = $this->_getTopicIcons($topicIcons);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['forumIcons'])) {
            $forumIcons = str_replace('"', '', $_POST['tx_typo3forum_ajax']['forumIcons']);
            $content['forumIcons'] = $this->_getForumIcons($forumIcons);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['displayedTopics'])) {
            $displayedTopics = str_replace('"', '', $_POST['tx_typo3forum_ajax']['displayedTopics']);
            $content['topics'] = $this->_getTopics($displayedTopics);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['displayedPosts'])) {
            $displayedPosts = str_replace('"', '', $_POST['tx_typo3forum_ajax']['displayedPosts']);
            $content['posts'] = $this->_getPosts($displayedPosts);
        }
        if (!empty($_POST['tx_typo3forum_ajax']['displayedPosts'])) {
            $displayedPosts = str_replace('"', '', $_POST['tx_typo3forum_ajax']['displayedPosts']);
            $content['posts'] = $this->_getPosts($displayedPosts);
        }
        if ($displayOnlinebox == 1) {
            $content['onlineBox'] = $this->_getOnlinebox();
        }
        $displayedAds = json_decode($_POST['tx_typo3forum_ajax']['displayedAds']);
        if ((int)$displayedAds->count > 1) {
            $content['ads'] = $this->_getAds($displayedAds);
        }

        $this->view->assign('content', json_encode($content));
    }

可能会对你有帮助。