简体   繁体   中英

entity framework invalid column name

i'm having some more problems with entity framework. one of them was resolved here entity framework multiple tables same name

now when i try to insert into either table bo or table bi i get the following error:

{"Invalid column name 'Bo_obrano'.\\r\\nInvalid column name 'Bo_boano'.\\r\\nInvalid column name 'Bo_ndos'."}

or

{"Invalid column name 'Bi_bistamp'}

as i reverse engineered the database through the use of power tools i now have my mapping for bo like this:

public boMap()
    {
    // Primary Key
    HasKey(t => new { t.obrano, t.boano, t.ndos });

     Property(t => t.obrano)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
     Property(t => t.boano)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
     Property(t => t.ndos)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    }

and bi mapping like this:

public BiMap()
        {
            // Primary Key
            HasKey(t => t.bistamp);
....

my context class looks like this:

public class PHCDbContext:DbContext
    {
        //classes mapeadas via reverse
        public DbSet<Bi> DadosLinhasEncomendas { get; set; }
        public DbSet<Bo> DadosCabecalhosEncomendas { get; set; }
...

     public PHCDbContext(string connection):base(connection)
            {

                Database.SetInitializer<PHCDbContext>(null);
            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new BiMap());
                modelBuilder.Configurations.Add(new boMap());
    ....

i exported the mapping as instructed in here: export code first model mapping

what i've found is that those fields do not exist per se. as i opened the edmx file in visual studio i found out that those fields are in the navigational property of the class, in the associations part of the model. they represent the primary keys in the tables, not foreign keys. but they are not mapped to any value in the poco class. much less to any column in the database. so how can i solve this? help would be apreciated thanks in advance

i got it to work by removing the navigational property. as there's no correlation between the tables whatsoever. both of them are isolated so far. thanks for the help once again

Your situation looks slightly unusual, but you need to look at your database and see what the column name is, then map it within the context:

protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
    modelBuilder.Entity<Bi>( ).ToTable( "dbo.Bi" );
    modelBuilder.Entity<Bi>( ).Property( p => p.bistamp).HasColumnName("actualName" ); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM