简体   繁体   中英

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; }

    }

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM