none
Problems to write data relationship one to one in the bank with the entity framework RRS feed

  • Question

  • Good afternoon, I'm having problems in the generation of records relating to the class Person and Individual.
    The PessoFisica table has a relationship with the Person table would be 1: 1 relationship, it happens that the primary key of Table PessoaFisica is being inserted in place the primary key table Person. And the person of ForeingKey is not being filled with shows in the picture:

    It is normal to happen this Part class has the same ID the whole class?

    public ActionResult Index()
        {
            var rnd = new Random();
     
            // Pessoa
            var pessoa = new Pessoa()
            {
                DataCadastro = DateTime.Now,
                Ativo = true,
                NegarCredito = false
            };
     
         // PF de Pessoa
            var pessoaFisica = new PessoaFisica()
            {
                Nome = "Nome " + rnd.Next(1, 13),
                Cpf = "321456987" + rnd.Next(1, 13),
                Pessoa = pessoa
            };
     
            // Instancia do Contexto do EF
            var contexto = new ApplicationDbContext();
     
            //#### INICIO SETUP de PF, PJ e Endereços ####
    
        // Adicionando PF em Pessoa
            pessoa.PessoaFisica = pessoaFisica;
     
            // Adicionando Pessoa no Contexto
            contexto.Pessoa.Add(pessoa);
     
            // Salvando todas as inclusões
            contexto.SaveChanges();



    What I have tried:

    I've tried this way and does not work. It is right in a relationship one to one the key to the whole class appear as a primary key in the table of the case: PessoaFisicaId = PeopleID

    http://imgur.com/vCGQSyv[^]

    Saturday, July 23, 2016 9:23 PM

Answers

  • Hi Thyago A. Developer,

    According to your description, it seems that you define a one-to-many relationship. if you want to use one-to-one, please modify your entities like this:

     public class Pessoa
        {
            public Pessoa() {
                PessoaId = Guid.NewGuid();
            }
            public Guid PessoaId { get; set; }
            public DateTime DataCadastro { get; set; }
            public bool Ativo { get; set; }
            public virtual PessoaFisica PessoaFisica { get; set; }
        }
    
        public class PessoaFisica
        {
            public PessoaFisica()
            {
                PessoaFisicaId = Guid.NewGuid();
            }
            public Guid PessoaFisicaId { get; set; }
            public string Name { get; set; }
            public string Cpf { get; set; }
            public virtual Pessoa Pessoa { get; set; }
        }

    And Fluent API like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // Configure PessoaFisicaId as PK for PessoaFisica
                modelBuilder.Entity<PessoaFisica>()
                    .HasKey(e => e.PessoaFisicaId);
    
                // Configure PessoaFisicaId as FK for PessoaFisica
                modelBuilder.Entity<Pessoa>()
                            .HasOptional(s => s.PessoaFisica)
                            .WithRequired(ad => ad.Pessoa);
            }

    Usage:

     using (var db = new EFDemoContext())
                {
                    var pessoa = new Pessoa()
                    {
                        DataCadastro = DateTime.Now,
                        Ativo = true
                    };
                    // PF de Pessoa
                    var pessoaFisica = new PessoaFisica()
                    {
                        Name = "Nome " ,
                        Cpf = "321456987",
                    };
                    pessoa.PessoaFisica = pessoaFisica;
                    db.Pessoas.Add(pessoa);
                    db.SaveChanges();
                  
                }

    For more information about one-to-one relationship, please refer to:

    https://msdn.microsoft.com/en-us/data/jj591620.aspx#RequiredToOptional

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 26, 2016 2:31 AM
    Moderator

All replies

  • var context = new Somecontext();

    var parent = new Parent();

    parent.ChildCollection = new List<Child>();

    //populate parent properties

    var child = new Child();

    //populate child properties

    parent.ChildCollection.Add(child);

    //create new child and populate it

    parent.ChildCollection.Add(child);

    //create new child objects as you need adding each one into parent.ChildCollection

    context.Parent.Add(parent);

    context.SaveChanges();

    Sunday, July 24, 2016 1:51 AM
  • Hi Thyago A. Develope,

    Thank you for posting here.

    Since your issue is related to ADO.NET Entity Framework forum. So I will move the thread to that forum for better support.

    Visual C# forum is a place for people discussing and asking questions about C# programming language,IDE,
    libraries,samples and tools, and your question is out of the range we supported .

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, July 25, 2016 9:36 AM
  • Hi Thyago A. Developer,

    According to your description, it seems that you define a one-to-many relationship. if you want to use one-to-one, please modify your entities like this:

     public class Pessoa
        {
            public Pessoa() {
                PessoaId = Guid.NewGuid();
            }
            public Guid PessoaId { get; set; }
            public DateTime DataCadastro { get; set; }
            public bool Ativo { get; set; }
            public virtual PessoaFisica PessoaFisica { get; set; }
        }
    
        public class PessoaFisica
        {
            public PessoaFisica()
            {
                PessoaFisicaId = Guid.NewGuid();
            }
            public Guid PessoaFisicaId { get; set; }
            public string Name { get; set; }
            public string Cpf { get; set; }
            public virtual Pessoa Pessoa { get; set; }
        }

    And Fluent API like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // Configure PessoaFisicaId as PK for PessoaFisica
                modelBuilder.Entity<PessoaFisica>()
                    .HasKey(e => e.PessoaFisicaId);
    
                // Configure PessoaFisicaId as FK for PessoaFisica
                modelBuilder.Entity<Pessoa>()
                            .HasOptional(s => s.PessoaFisica)
                            .WithRequired(ad => ad.Pessoa);
            }

    Usage:

     using (var db = new EFDemoContext())
                {
                    var pessoa = new Pessoa()
                    {
                        DataCadastro = DateTime.Now,
                        Ativo = true
                    };
                    // PF de Pessoa
                    var pessoaFisica = new PessoaFisica()
                    {
                        Name = "Nome " ,
                        Cpf = "321456987",
                    };
                    pessoa.PessoaFisica = pessoaFisica;
                    db.Pessoas.Add(pessoa);
                    db.SaveChanges();
                  
                }

    For more information about one-to-one relationship, please refer to:

    https://msdn.microsoft.com/en-us/data/jj591620.aspx#RequiredToOptional

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 26, 2016 2:31 AM
    Moderator