简体   繁体   English

实体框架上下文中的实体是什么?

[英]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 ? 我想使用EntityFramework.BulkInsert来执行此操作,我检查了文档,但是我是EF的新手,我无法弄清楚在此上下文中什么是实体。什么是实体?

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: 这是我的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. 在此背景下entities将是你的,你要插入的实体的名单。 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) 因此,您TagUsers.Add(taguser)每次都说TagUsers.Add(taguser) ,而是可以在List<TagUser> tagusers获得要插入的List<TagUser> tagusers (也许那样,我个人没有使用BulkInsert),而是说applicationUser.BulkInsert(tagusers)

The entities are the tag users collection and before doing the bulk insert, provide the FK id first. 实体是标签用户集合,在进行批量插入之前,请首先提供FK ID。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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