Method to edit data with Entity

Asked

Viewed 285 times

0

I am developing a file manager and have already implemented some methods like addArquivo, listarArquivo, listarVersoes, etc..

But I need to edit it, check my DAL methods:

Add:

 internal void AddArquivo(Model.Arquivo arquivo)
    {
        using (var ctx = new TESTEntities())
        {
            var versao = arquivo.ArquivoVersoes[0];
            ctx.ARQUIVO.Add(new ARQUIVO()
            {
                ARQUIVO_GUID = arquivo.ARQUIVO_GUID,
                ARQUIVO_VERSAO = new ARQUIVO_VERSAO()
                {
                    ARQUIVO_GUID = arquivo.ARQUIVO_GUID,
                    ARQUIVO_VERSAO_GUID = Util.Util.GUIDs.GetGUID(),
                    ARQUIVO = versao.ARQUIVO,
                    DATAHORA = versao.DATAHORA,
                    TAMANHO = versao.TAMANHO,
                    USUARIO_PESSOA_GUID = versao.USUARIO_PESSOA_GUID
                },
                DIRETORIO_GUID = arquivo.DIRETORIO_GUID,
                EXTENSAO = arquivo.EXTENSAO,
                IS_STREAM = arquivo.IS_STREAM,
                TAG = arquivo.TAG,
                TIPO_DE_ARQUIVO_GUID = arquivo.TipoDeArquivo.TIPO_DE_ARQUIVO_GUID,
                ULT_ARQUIVO_VERSAO_GUID = arquivo.ULT_ARQUIVO_VERSAO_GUID,
                URL = arquivo.URL,
                XARQUIVO = arquivo.XARQUIVO
            });
            ctx.SaveChanges();
        }

Get a file:

/// <summary>
    /// Método que busca um arquivo conforme parametro
    /// </summary>
    /// <param name="termo"></param>
    /// <returns></returns>
    internal Arquivo GetArquivo(string termo)
    {
        using (var ctx = new TESTEntities())
        {
            var arquivo = (from arq in ctx.ARQUIVO
                           where arq.ARQUIVO_GUID == termo
                           || arq.XARQUIVO == termo
                           select new Arquivo()
                           {
                               ARQUIVO_GUID = arq.ARQUIVO_GUID,
                               DIRETORIO_GUID = arq.DIRETORIO_GUID,
                               EXTENSAO = arq.EXTENSAO,
                               IS_STREAM = arq.IS_STREAM,
                               TAG = arq.TAG,
                               TIPO_DE_ARQUIVO_GUID = arq.TIPO_DE_ARQUIVO_GUID,
                               ULT_ARQUIVO_VERSAO_GUID = arq.ULT_ARQUIVO_VERSAO_GUID,
                               URL = arq.URL,
                               XARQUIVO = arq.XARQUIVO,
                           }).FirstOrDefault();
            arquivo.ArquivoVersoes.Add(GetArquivoVersao(arquivo.ULT_ARQUIVO_VERSAO_GUID));
            return arquivo;
        }
    }

Mapping:

    public DbSet<ARQUIVO> ARQUIVO { get; set; }
    public DbSet<ARQUIVO_PRODUTO> ARQUIVO_PRODUTO { get; set; }
    public DbSet<ARQUIVO_VERSAO> ARQUIVO_VERSAO { get; set; }
    public DbSet<DIRETORIO> DIRETORIO { get; set; }
    public DbSet<DIRETORIO_PASTA> DIRETORIO_PASTA { get; set; }
    public DbSet<TIPO_DE_ARQUIVO> TIPO_DE_ARQUIVO { get; set; }

What I did with the help of Thiago Custodio’s answer:

internal void UpdateArquivo(String termo, Arquivo arquivoEditado)
    {

        var arquivo = GetArquivo(termo);
        using (var ctx = new TESTEntities())
        {



            arquivo.XARQUIVO = arquivoEditado.XARQUIVO;
            arquivo.TAG = arquivoEditado.TAG;
            arquivo.EXTENSAO = arquivoEditado.EXTENSAO;
            arquivo.URL = arquivoEditado.URL;
            arquivo.IS_STREAM = arquivoEditado.IS_STREAM;

            ctx.Entry(arquivo).State = System.Data.EntityState.Modified;
            ctx.SaveChanges();

    }
    }

But you’re giving this Exception:

the entity type arquivo not part of the model for the current context
  • Have you noticed that your questions always need to be edited? Take a look, get better. You’re getting great help here at website help us keep everything organized, avoid giving extra work to the other users who help you. Especially careful to put tags, almost always using an existing one is better than creating another, especially with the wrong name. Programming requires care. Be careful training in everything in life.

  • All right, I’ll try to improve.

  • @Warlock, before starting to work with a tool, try to know the same one first, I see that you are trying to use the EF, without having the least knowledge of its functioning. In this case I advise you to read the following article: http://www.macoratti.net/10/11/c_efcrud.htm

  • @Tobymosque updated the question, with what I tried with the help of friend Thiago, thanks for the link I have to study, but see what can help me at the moment.

2 answers

0

something like that:

var termo = "";
var arquivo = GetArquivo(termo);

arquivo.TAG  = "outra tag";

using (var ctx = new TESTEntities())
{
    ctx.Entry(arquivo).State = System.Data.Entity.EntityState.Modified;
    ctx.SaveChanges();
}
  • that other tag would be q?

  • Voce put file.tag , as example of q? of changing a given? in case I would have to change all attributes?

  • a property of your File class, no!?

  • vc can set any prop you want to change...I just gave this example of how to change the TAG property.

  • I know, but in case I would have to do in all properties neh?

  • but like, ai esta manual neh, what would be the best way to edit what the user has gone through?

  • I would have to bring from the customer, an object with everything already set neh?]

  • I’ll test here and return you worth

  • ta dando essa Exception: The Entity type File is not part of the model for the Current context.

  • you know what can be?

Show 5 more comments

0


I got it using the following way:

internal void UpdateArquivo(Model.Arquivo arquivoEditado)
    {
        using (var ctx = new TESTEntities())
        {
            var file = ctx.ARQUIVO.FirstOrDefault(a => a.ARQUIVO_GUID == arquivoEditado.ARQUIVO_GUID);
            if (file == null)
                throw new ArquivoException("Arquivo não encontrado");

            file.XARQUIVO = arquivoEditado.XARQUIVO;
            file.TAG = arquivoEditado.TAG;
            file.EXTENSAO = arquivoEditado.EXTENSAO;
            file.URL = arquivoEditado.URL;
            file.IS_STREAM = arquivoEditado.IS_STREAM;

            ctx.Entry(file).State = System.Data.EntityState.Modified;
            ctx.SaveChanges();

        }

I was just using the :

 var file = ctx.ARQUIVO.FirstOrDefault(a => a.ARQUIVO_GUID == arquivoEditado.ARQUIVO_GUID);  

that so it caught exactly the file that I wanted to edit, but thank you all.

  • instead of using . Firstordefault(), use . Find(), as it has a more appropriate name for this situation and is about twice as fast.

  • could you join the following chat? http://chat.stackoverflow.com/rooms/70597/mvc-wcf-e-entity-framework

  • I’m coming in, y'all. net?

  • brazilian?....

  • had created this channel to try gives you some tips.

  • if you can get in there now

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.