如何使用实体框架在连接表中插入数据?

2023-12-02

我是实体框架的新手,我希望获得一些帮助来在“连接表”中插入数据。

我有三张桌子,Profiles, Tags和一个叫ProfilesTags连接这两个表。类是从数据库/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方法看起来像这样:

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()
    }

我怎样才能关联newTag with pr.ProfilesTags?


它似乎newTag首先应该有一个有效的 id,然后通过以下方式建立关系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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用实体框架在连接表中插入数据? 的相关文章

随机推荐