Doubt about insert in bank with foreign key using linq

1

I have a question, how to insert data into a mysql table, using linq, I have the tables below:

I'm using the method below to insert:

public void inserirCarteira(float cpf, string codigo, double valor, int quant, double vtotal)
    {
        try
        {

            validarInserir(cpf, codigo, valor, quant, vtotal);
            // instancia  o banco de dados
            bancotccEntities1 bco = new bancotccEntities1();
            bco.Database.Connection.Open();
            // cria um objeto 
            carteira cart = new carteira();
            // popula o objeto
            cart.cpf = cpf;
            cart.codigo = codigo;
            cart.valoracao = valor;
            cart.qtdacao = quant;
            cart.vtotalacao = vtotal;

            // adciona um objeto ao banco
            bco.AddTocarteira(cart);
            //salva o objeto no banco
            bco.SaveChanges();
            bco.Database.Connection.Close(); 
        }
        catch (Exception e1)
        {

            throw new Exception(e1.Message.ToString());
        }
    }

I know that I have to use my foreign key but I do not know what it would look like: cart.card_card ?

If this is how I am going to enter values since the idusuario is the primary key of the user table and is auto increment, then I do not know what the value of this field will be at the time of insertion. >

Can anyone give me an idea how to do it?

    
asked by anonymous 11.06.2014 / 04:12

1 answer

1

The problem is that you have placed a wallet as a user dependent and a wallet dependent user, ie you can only insert a wallet if there is a user linked to it and a user if there is a wallet.

Try to fill in the key and / or relationship object between types before you run SaveChanges ()

This mapping is somewhat confusing, you have the Portfolio object and user with the CPF and Id Id attributes, what are the relationship attributes?

Can the User have 1 and only 1 Wallet? Can the User have 0 or N Portfolios? Can the user have 1 or N Wallets?

You need to define these points before you create the objects.

    
11.08.2014 / 21:26