none
Code first error 0040 RRS feed

  • Question

  • Hi,

    I've this situation:

        public class CategoriaProdotto
        {
            public int ID { get; set; }
            public string Descrizione { get; set; }

            public virtual ICollection<Prodotto> Prodotti { get; set; }
        }

        public class Prodotto
        {
            public int ID { get; set; }
            public int CategoriaID { get; set; }
            public string Descrizione { get; set; }
            public int UnitàMisuraID { get; set; }
            public int TariffaID { get; set; }
            public int CostoID { get; set; }

            public virtual CategoriaProdotto Categoria { get; set; }
            public virtual Importo Tariffa { get; set; }
            public virtual Importo Costo { get; set; }
        }

        public class Importo
        {
            public int ID { get; set; }
            public decimal Valore { get; set; }

            public virtual ICollection<Prodotto> Prodotti { get; set; }
        }

        public class Context:DbContext
        {
            public Context()
                :base("name=DB")
            {
            }

            public DbSet<CategoriaProdotto> tbCategoriaProdotto { get; set; }
            public DbSet<Prodotto> tbProdotti { get; set; }
            public DbSet<Importo> tbImporti { get; set; }


            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<CategoriaProdotto>()
                    .ToTable("tbCategoriaProdotti");
                modelBuilder.Entity<Prodotto>()
                    .ToTable("tbProdotti");
                modelBuilder.Entity<Importo>()
                    .ToTable("tbImporti");

                modelBuilder.Entity<CategoriaProdotto>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<Prodotto>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<Importo>()
                    .HasKey(c => c.ID);

                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Categoria)
                    .WithMany(c => c.Prodotti)
                    .HasForeignKey(c => c.CategoriaID)
                    .WillCascadeOnDelete();
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Tariffa)
                    .WithMany(c => c.Prodotti)
                    .HasForeignKey(c => c.TariffaID)
                    .WillCascadeOnDelete();
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Costo)
                    .WithMany(c => c.Prodotti)
                    .HasForeignKey(c => c.CostoID)
                    .WillCascadeOnDelete();
            }
        }

    I get this error:

    "Specificated schema not valid. Errors:
    (39,6) : error 0040: Tipo Prodotto_Tariffa not definited in namespace Gestione.ModelsContext (Alias=Self)."

    Where is my wrong?

    Wednesday, November 28, 2012 9:51 AM

Answers

  • Hi Cracken,

    Welcome to the MSDN forum.

    It is notpossible to map two navigation properties (Tariffa and Costo in Prodotto) to a single collection property (Prodotti in Importo). For example, if you have an object typeof Importo with a collection Prodotti, it is impossible for each object (typeof Prodotto) in collection to know this object is Tariffa or Costo. Thus, I recommend you map two navigation properties to two different types like this:

        public class CategoriaProdotto
        {
            public int ID { get; set; }
            public string Descrizione { get; set; }
    
            public virtual ICollection<Prodotto> Prodotti { get; set; }
        }
    
        public class Prodotto
        {
            public int ID { get; set; }
            public int CategoriaID { get; set; }
            public string Descrizione { get; set; }
            public int UnitàMisuraID { get; set; }
            public int TariffaID { get; set; }
            public int CostoID { get; set; }
    
            public virtual CategoriaProdotto Categoria { get; set; }
            public virtual ImportoTariffa Tariffa { get; set; }
            public virtual ImportoCosto Costo { get; set; }
        }
    
        public class ImportoTariffa
        {
            public int ID { get; set; }
            public decimal Valore { get; set; }
    
            public virtual ICollection<Prodotto> ProdottiTriffa { get; set; }
        }
    
        public class ImportoCosto
        {
            public int ID { get; set; }
            public decimal Valore { get; set; }
    
            public virtual ICollection<Prodotto> ProdottiCosto { get; set; }
        }
    
        public class Context : DbContext
        {
            public Context()
                : base("name=DB")
            {
            }
    
            public DbSet<CategoriaProdotto> tbCategoriaProdotto { get; set; }
            public DbSet<Prodotto> tbProdotti { get; set; }
            public DbSet<ImportoCosto> tbImportiC { get; set; }
            public DbSet<ImportoTariffa> tbImportiT { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<CategoriaProdotto>()
                    .ToTable("tbCategoriaProdotti");
                modelBuilder.Entity<Prodotto>()
                    .ToTable("tbProdotti");
                modelBuilder.Entity<ImportoCosto>()
                    .ToTable("tbImportiC");
                modelBuilder.Entity<ImportoTariffa>()
                    .ToTable("tbImportiT");
    
                modelBuilder.Entity<CategoriaProdotto>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<Prodotto>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<ImportoTariffa>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<ImportoCosto>()
                    .HasKey(c => c.ID);
    
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Categoria)
                    .WithMany(c => c.Prodotti)
                    .HasForeignKey(c => c.CategoriaID)
                    .WillCascadeOnDelete();
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Tariffa)
                    .WithMany(c => c.ProdottiTriffa)
                    .HasForeignKey(c => c.TariffaID)
                    .WillCascadeOnDelete();
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Costo)
                    .WithMany(c => c.ProdottiCosto)
                    .HasForeignKey(c => c.CostoID)
                    .WillCascadeOnDelete();
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cracken66 Friday, November 30, 2012 12:41 PM
    Thursday, November 29, 2012 3:23 AM

All replies

  • Hi Cracken,

    Welcome to the MSDN forum.

    It is notpossible to map two navigation properties (Tariffa and Costo in Prodotto) to a single collection property (Prodotti in Importo). For example, if you have an object typeof Importo with a collection Prodotti, it is impossible for each object (typeof Prodotto) in collection to know this object is Tariffa or Costo. Thus, I recommend you map two navigation properties to two different types like this:

        public class CategoriaProdotto
        {
            public int ID { get; set; }
            public string Descrizione { get; set; }
    
            public virtual ICollection<Prodotto> Prodotti { get; set; }
        }
    
        public class Prodotto
        {
            public int ID { get; set; }
            public int CategoriaID { get; set; }
            public string Descrizione { get; set; }
            public int UnitàMisuraID { get; set; }
            public int TariffaID { get; set; }
            public int CostoID { get; set; }
    
            public virtual CategoriaProdotto Categoria { get; set; }
            public virtual ImportoTariffa Tariffa { get; set; }
            public virtual ImportoCosto Costo { get; set; }
        }
    
        public class ImportoTariffa
        {
            public int ID { get; set; }
            public decimal Valore { get; set; }
    
            public virtual ICollection<Prodotto> ProdottiTriffa { get; set; }
        }
    
        public class ImportoCosto
        {
            public int ID { get; set; }
            public decimal Valore { get; set; }
    
            public virtual ICollection<Prodotto> ProdottiCosto { get; set; }
        }
    
        public class Context : DbContext
        {
            public Context()
                : base("name=DB")
            {
            }
    
            public DbSet<CategoriaProdotto> tbCategoriaProdotto { get; set; }
            public DbSet<Prodotto> tbProdotti { get; set; }
            public DbSet<ImportoCosto> tbImportiC { get; set; }
            public DbSet<ImportoTariffa> tbImportiT { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<CategoriaProdotto>()
                    .ToTable("tbCategoriaProdotti");
                modelBuilder.Entity<Prodotto>()
                    .ToTable("tbProdotti");
                modelBuilder.Entity<ImportoCosto>()
                    .ToTable("tbImportiC");
                modelBuilder.Entity<ImportoTariffa>()
                    .ToTable("tbImportiT");
    
                modelBuilder.Entity<CategoriaProdotto>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<Prodotto>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<ImportoTariffa>()
                    .HasKey(c => c.ID);
                modelBuilder.Entity<ImportoCosto>()
                    .HasKey(c => c.ID);
    
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Categoria)
                    .WithMany(c => c.Prodotti)
                    .HasForeignKey(c => c.CategoriaID)
                    .WillCascadeOnDelete();
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Tariffa)
                    .WithMany(c => c.ProdottiTriffa)
                    .HasForeignKey(c => c.TariffaID)
                    .WillCascadeOnDelete();
                modelBuilder.Entity<Prodotto>()
                    .HasRequired(c => c.Costo)
                    .WithMany(c => c.ProdottiCosto)
                    .HasForeignKey(c => c.CostoID)
                    .WillCascadeOnDelete();
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cracken66 Friday, November 30, 2012 12:41 PM
    Thursday, November 29, 2012 3:23 AM
  • So, do I have to create another table into db?

    Thanks

    Thursday, November 29, 2012 8:49 AM