4
I have a method that adds almost 2000 records at once, I was using the Bulk Insert and it was working perfectly, but this extension is paid for.
So I decided to use the Sqlbulkcopy class but I’m having a hard time with how to use it.
I have this class:
public class PessoaNotificacao
{
public int PessoaNotificacaoId { get; set; }
public int PessoaUnidadeId { get; set; }
public int NotificacaoId { get; set; }
public bool Visualizado { get; set; }
public virtual Notificacao Notificacao { get; set; }
public virtual PessoaUnidade PessoaUnidade { get; set; }
}
I have a method with a foreach creates an insert list with approximately 2000 records, the tables that make links are these:
public class Notificacao
{
public int Posicao { get; set; }
public int NotificacaoId { get; set; }
public int CategoriaId { get; set; }
}
and :
public class Pessoa
{
public int PessoaId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Cpf { get; set; }
public string Senha { get; set; }
public string Rg { get; set; }
}
currently I do the insertion in this way:
foreach (PessoaUnidade pessoa in pessoas){
var pessoaNotificacao = new PessoaNotificacao
{
Visualizado = false,
PessoaUnidade = pessoa,
Notificacao = notificacao
};
_contexto.PessoaNotificacao.Add(pessoaNotificacao);
}
_contexto.SaveChanges();
But I don’t know how to use the SqlBulkCopy
, I am currently studying THIS EXAMPLE but I haven’t gotten anything yet. If any can explain how this insertion would work, I would be grateful.
Thank you very much from now on.
If Insert is with Entity can I understand that it is from SQL to SQL? If yes it is the same basis?
– Alexandre Cavaloti
The data will come from the same base, I search from the table
Pessoa
and I’m going to insert into thatPessoaNotificacao
.– William Cézar
Don’t you know how to apply Sqlbulkcopy? Could you go through where the information comes from and where it goes? do not compare to Entity Framework, already face your entity has an aggregation so it needs to be something simpler if put I put a functional example, or you want a Generic example?
– novic
@Virgilionovic, I get it, I’m going to try to rephrase the question, I’m going to do a little more research on how Sqlbulkcopy works, and that I was using Sqlbulkinsert but it stopped working because it was paid. I will give a studied and I will modify the question without the example of the entityframework.
– William Cézar
I made an issue, I will continue studying here to see if I can improve the question, any case she still gets confused I remove her.
– William Cézar
You need to write inside the table
PessoaNotificacao
and the fields are: Personal, Personal ?– novic
Exactly @Virgilionovic
– William Cézar
What I can propose is a dry example with Sqlbulkcopy with the connection of the Entity Framework! ai then you adpata to your?
– novic
Well, I found this explanation ,I think it will help me :.https://www.codeproject.com/Articles/18418/Transferring-Data-Using-SqlBulkCopy
– William Cézar
@Virgilionovic yes, if you propose a simple example would be enough.
– William Cézar