Insert data into table with foreign key

Asked

Viewed 1,784 times

2

Knowing that I have a table with the fields:

[ARQUIVO_GUID]
,[XARQUIVO]
,[TAG]
,[EXTENSAO]
,[URL]
,[IS_STREAM]

[ULT_ARQUIVO_VERSAO_GUID] FK
[TIPO_DE_ARQUIVO_GUID] FK
[DIRETORIO_GUID] FK

The last 3 fields are foreign keys to other tables, as I do to insert data in the table Filing cabinet?

  • 1

    You know what the ids of the other tables with which you have to fill this query?

  • updated the complete scheme, has no data insrido, I want to insert a file, as would be the Insert?

  • You first need to insert rows in tables DIRETORIO, ARQUIVO_VERSAO and TIPO_DE_ARQUIVO to take advantage of the ids of the rows inserted in the table ARQUIVO.

  • if you can post as answer all the Inserts would appreciate it very much, if you can not put only the same file.

1 answer

1


@Warlock, follow an example:

using (var dbContext in new SeuDBContext()) {
    var arquivoVersao = new ARQUIVO_VERSAO();
    //Set das Propriedades do objeto arquivoVersao 

    var tipoArquivo = new TIPO_DE_ARQUIVO();
    //Set das Propriedades do objeto tipoArquivo 

    var diretorio = new DIRETORIO();
    //Set das Propriedades do objeto diretorio 

    var arquivo = new ARQUIVO();
    arquivo.DIRETORIO = diretorio;
    arquivo.TIPO_DE_ARQUIVO = tipoArquivo;
    arquivo.ARQUIVO_VERSAO = arquivoVersao;
    //Set das Propriedades de arquivo 

    dbContext.ARQUIVOS.Add(arquivo);
    dbContext.SaveChanges();
}

In SQL can be something like this:

DECLARE @arquivoVersaoID as uniqueidentifier
DECLARE @tipoArquivoID as uniqueidentifier
DECLARE @diretorioID as uniqueidentifier

SET @arquivoVersaoID = NEWID();
SET @tipoArquivoID = NEWID();
SET @diretorioID = NEWID();

INSERT INTO ARQUIVO_VERSAO VALUES (@arquivoVersaoID, /*Demais propriedades*/);
INSERT INTO TIPO_DE_ARQUIVO VALUES (@tipoArquivoID, /*Demais propriedades*/);
INSERT INTO DIRETORIO VALUES (@diretorioID, /*Demais propriedades*/)
INSERT INTO ARQUIV VALUES (/*Demais propriedades*/, @arquivoVersaoID, @tipoArquivoID, @diretorioID)
  • I appreciate the help, but I want the same normal Sql

  • but this way already inserts data in everything?

  • but as specific data entry q did not understand, what to want is to fill the tables with all the data.

  • worth I will test, in the case of Entity that Voce put there, how would the data entry exactly?

  • I did an update on the response. in this case you will have to insert the data in all tables, first in which are referenced and last in the table Files.

  • you would normally enter the data, for example: file.ARQUIVO_GUID = Guid.Newguid().

  • got it, so how do I call the same, ie to run and insert the data. It can be in a page load.

  • can, can’t see much logic in doing a load of data on a page load.

  • The last line is dbContext.ARQUIVO.add or dbContext.FILES? as files will not go .

  • This will result from the modeling of your Context. you enabled the pluralization of names at the time of creation?

  • go on chat...

  • Firewall does not allow.

  • here gave an error: System.Data.Entity.Infrastructure.Dbupdateexception: Error updating entries. See the internal exception for details. ---> System.Data.Updateexception:

  • went by sql, and it worked worth.

Show 10 more comments

Browser other questions tagged

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