如何使用实体框架在联接表中插入数据? [英] How to insert data in a joined table with Entity framework?

查看:101
本文介绍了如何使用实体框架在联接表中插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Entity Framework的新手,我希望能对将数据插入联接表"有所帮助.

I am new to Entity Framework and I am hoping for some help to insert data in a "joined table".

我有三个表ProfilesTags和一个称为ProfilesTags的表,它们将这两个表连接在一起.类是从数据库/DB First自动生成的.

I have three tables, Profiles, Tags and one called ProfilesTags that joins these two tables. Classes are autogenerated from database / DB First.

public partial class Profiles
{
    public Profiles()
    {
        this.ProfilesTags = new HashSet<ProfilesTags>();
    }

    public int ProfileId { get; set; }
    public string Name { get; set; }
    ...

    public virtual ICollection<ProfilesTags> ProfilesTags { get; set; }
}


public partial class Tags
{
    public Tags()
    {
        this.ProfilesTags = new HashSet<ProfilesTags>();
    }

    public int TagId { get; set; }
    public string Tag { get; set; }

    public virtual ICollection<ProfilesTags> ProfilesTags { get; set; }
}

public partial class ProfilesTags
{
    public int Id { get; set; }
    public int ProfileId { get; set; }
    public int TagId { get; set; }

    public virtual Tags Tags { get; set; }
    public virtual Profiles Profiles { get; set; }
}

我有一个SaveTags方法,如下所示:

I have a SaveTags method that looks like this:

public void SaveTags(int profileId, IEnumerable<TagsNameValue> tags)
    {
        var pr = Context.Profiles.First(p => p.ProfileId == profileId);

    // remove any existing
        pr.ProfilesTags.Clear();

        if (tags == null || !tags.Any())
            return;

        var ids = tags.Select(value => value.Value);
        var names = tags.Select(value => value.Name);


        // get a list of tags for lookup from [Tags]-table
        var tagsList = Context.Tags.Where(t => ids.Any(v => t.TagId == v) || names.Any(v => t.Tag == v)).ToList();

        foreach (var nameValue in tags)
        {
            var tag = tagsList.FirstOrDefault(t => t.TagId == nameValue.Value || t.Tag.ToLower() == nameValue.Name.ToLower());

            // Tag is already in [Tags], no need to recreate id, just associate it.
            if (tag != null)
            {
                var tagModel = new ProfilesTags()
                {
                    TagId = nameValue.Value,
                    ProfileId = profileId
                };

                pr.ProfilesTags.Add(tagModel);
            }

            // create new item in [Tags] table first and add association [ProfilesTags]
            else
            {
                var newTag = new Tags { Tag = nameValue.Name};

            // how do I associate this newly added tag to pr.ProfilesTags ?
                // what to do / how to procede?

                Context.Tags.Add(newTag);
            }
        }
        Context.SaveChanges()
    }

如何将newTagpr.ProfilesTags关联?

推荐答案

似乎newTag首先应具有有效的ID,然后再由ProfilesTags建立关系.

It seems newTag should have a valid id first, then build the relationship by ProfilesTags later.

// create new item in [Tags] table first and add association [ProfilesTags]
else
{
     var newTag = new Tags { Tag = nameValue.Name};

     // how do I associate this newly added tag to pr.ProfilesTags ?
     // what to do / how to procede?

     Context.Tags.Add(newTag);

     // Let newTag has a valid tagId
     Context.SaveChanges();

     // Build the relationship between `Profiles` and `Tags`.
     var newProfileTag = new ProfilesTags();

     /// Build the relationship by ForeignKey,
     /// Or, call pr.ProfilesTags.Add(newProfileTag)
     newProfileTag.ProfiledId = pr.ProfileId; 
     newProfileTag.TagId = newTag.TagId; //< It SHOULD NOT be zero...
     Context.ProfilesTags.Add(newProfileTag);

     // Save the newProfileTag.....
     // Context.SaveChanges();
}

这篇关于如何使用实体框架在联接表中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆