Hascolumnname does not recognize field

Asked

Viewed 250 times

2

I am working with Entity Framework 6.1, using the approach code first. I mapped my class in the database through the class EntityTypeConfiguration.

When mapping a property with a table field, using the code below, for example:

namespace Dominio
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class UF
    {
        private int iduf;
        private string nome;
        private string sigla;

        public  int IdUf
        {
            get { return this.iduf; }
            set { this.iduf = value; }
        }

        public  string Nome
        {
            get { return this.nome; }
            set { this.nome = value; }
        }

        public  string Sigla
        {
            get { return this.sigla; }
            set { this.sigla = value; }
        }

    }
}



namespace DAO
{
    class UFConfig:EntityTypeConfiguration<UF>
    {
        public UFConfig()
        {


            this.HasKey(u => u.IdUf);
            this.Property(u =>u.IdUf).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(u => u.Sigla);
            this.Property(u => u.Nome);

            this.ToTable("uf");
            this.Property(u => u.IdUf).HasColumnName("IDUF") ;
            this.Property(u => u.Sigla).HasColumnName("SIGLA");
            this.Property(u => u.Nome).HasColumnName("nome_UF");


        }
    }
}

Table in the Bank

CREATE TABLE [dbo].[uf](
    [iduf] [int] NOT NULL,
    [sigla] [varchar](2) NOT NULL,
    [nome_uf] [varchar](20) NOT NULL,
 CONSTRAINT [PK_uf] PRIMARY KEY CLUSTERED 
(
    [iduf] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

In return, I have the following:

The data Reader is incompatible with the specified 'DAO.UF'. A Member of the type, 'Name', does not have a corresponding column in the data Reader with the same name.

I ask you: i must use the Entity Framework standard to name the columns of my database table?

  • we need the DAO.UF and we need you to put all the mapeamento (Entitytypeconfiguration) in its entirety, because Hascolumnname recognizes this yes!!!

  • when I modify the field in the table to name, it works normally. The problem is that I own the classes in one pattern and the bank in another, it would be very difficult if I had to put my bank in the default of the Entity framework, just as the reverse would also be bad.

  • I did not say different, what I want you to edit the question and put all the code matches your question, understood @dhielyton

  • OK, I got it, I’ve done the editing. @Fccdias

  • You could post the table schema in the database?

1 answer

2


Place:

class UFConfig:EntityTypeConfiguration<UF>
{
    public UFConfig()
    {
        this.ToTable("uf");
        this.HasKey(u => u.IdUf).HasColumnName("IDUF").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);        
        this.Property(u => u.Sigla).HasColumnName("SIGLA");
        this.Property(u => u.Nome).HasColumnName("nome_UF");
    }
}

Use the command just below on PM (Package Manage Consoler):

Add-Migration InitialSchema -IgnoreChanges

In your case enable this item in Configuration.Cs

internal sealed class Configuration : DbMigrationsConfiguration<YourContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }
}

Then use a

Update-Database

How did you choose CodeFirst, will be required to use Migrations for adjustments and changes

Reference:

  • did, what you guided me, continued the same problem. @Fccdias

  • The bank already exists?

  • yes, the bank already exists. @Fccdias

  • I edited the post ... as you had a base ready and started to develop by code first make these commands

  • 1

    worked, Valew @fccdias, thanks.

Browser other questions tagged

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