alizx
alizx

Reputation: 1188

what is entity in entityframework context?

I want to implement this action with use of EntityFramework.BulkInsert ,I checked the documentation but I'm new to EF i couldn't figure out what is entity in this context.what is entity ?

foreach (string tag in tags)
    {
        TagUser tagUser = new TagUser()
        {
            //Initialize TagUser
        };


        applicationUser.TagUsers.Add(tagUser);
    }

db.SaveChanges();

this is the sample code in its documention:

using (var ctx = GetContext())
{
  using (var transactionScope = new TransactionScope())
  {
    // some stuff in dbcontext

    ctx.BulkInsert(entities);

    ctx.SaveChanges();
    transactionScope.Complete();
  }
}

and this is my TagUserclass:

public class TagUser
    {
        [Required]
        [Key, Column(Order = 0)]
        [ForeignKey("Tag")]
        public Guid TagId { get; set; }

        [Required]
        [Key, Column(Order = 1)]
        [ForeignKey("ApplicationUser")]
        public string Id { get; set; }


        public virtual Tag  Tag { get; set; }


        public virtual ApplicationUser ApplicationUser { get; set; }


        [Required]
        public bool InEnglish { get; set; }

        [Required]
        public Int16 Priority { get; set; }

    }

Upvotes: 1

Views: 136

Answers (2)

Yuliam Chandra
Yuliam Chandra

Reputation: 14640

The entities are the tag users collection and before doing the bulk insert, provide the FK id first.

using (var ctx = GetContext())
{
    using (var transactionScope = new TransactionScope())
    {
        var entities= new List<TagUser>();
        foreach (string tag in tags)
        {
            TagUser tagUser = new TagUser()
            {
                //Initialize TagUser
                TagId = ..., // get something like tag.Id
                Id = applicationUser.Id
            };
            entities.Add(tagUser);
        }

        ctx.BulkInsert(entities);

        ctx.SaveChanges();
        transactionScope.Complete();
    }
}

Upvotes: 1

barnacle.m
barnacle.m

Reputation: 2200

In this context entities would be your list of entities that you're trying to insert. So instead of saying TagUsers.Add(taguser) every time, you might get the list of TagUsers you want to insert in a List<TagUser> tagusers (maybe like that, I haven't used BulkInsert personally), and say applicationUser.BulkInsert(tagusers)

Upvotes: 1

Related Questions