none
EF Generate from database mapping error?

    Question

  • Hi All

    I am having some problems with my entity framework model, I am generating the model from the database and I have 6 tables that 'map' through to 6 entities. I originally had a foreign key relationship between these fields but I removed it as I have tried to track down the bug. as far as I can see all the fields in the edmx model are mapped correctly through to the database.

    In my constructor for the controller I create a new instance of my ClientContext which I want to use to manage my interface with my data layer:

      Public Class ClientContext
        Inherits DbContext
    
        Public Property CFManagers As DbSet(Of CF_Managers)
        Public Property IndivClients As DbSet(Of Contact)
        Public Property Companies As DbSet(Of MasterDetail)
        Public Property Coverage As DbSet(Of Coverage)
        Public Property Category As DbSet(Of Category)
      End Class

    When I instantiate the class I get an error saying:

    The type 'Clients.CF_Managers' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject. 


    Any ideas on what's causing this?

    Wednesday, March 30, 2011 11:54 AM

Answers

  • Hi Chris,

    In CTP5, there are two ways to ignore both types and properties. The first is to use the NotMappedAttribute data annotation:

    public class Product

    {

        public int Id { get; set; }

        public string Name { get; set; }

        [NotMapped]

        public string InternalCode { get; set; }

    }

     

    You can also use the fluent API in CTP5 to ignore properties using the ‘Ignore’ method:

    public class Product

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string InternalCode { get; set; }

    }

     

    public class MyContext : DbContext

    {

        public DbSet<Product> Products { get; set; }

     

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            modelBuilder.Entity<Product>()

                .Ignore(p => p.InternalCode);

        }

    }

    Hope this helps.

    Regards,

    Monday, April 04, 2011 8:53 AM

All replies

  • Hi Chris,

    In CTP5, there are two ways to ignore both types and properties. The first is to use the NotMappedAttribute data annotation:

    public class Product

    {

        public int Id { get; set; }

        public string Name { get; set; }

        [NotMapped]

        public string InternalCode { get; set; }

    }

     

    You can also use the fluent API in CTP5 to ignore properties using the ‘Ignore’ method:

    public class Product

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string InternalCode { get; set; }

    }

     

    public class MyContext : DbContext

    {

        public DbSet<Product> Products { get; set; }

     

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            modelBuilder.Entity<Product>()

                .Ignore(p => p.InternalCode);

        }

    }

    Hope this helps.

    Regards,

    Monday, April 04, 2011 8:53 AM
  • Since the 'answer' that was automatically selected as the answer by mods doesn't really answer your question ... 

    I came across this today as well. 

    Turns out I have 2 different tables from 2 different databases mapped to entities with the same names.
    It also turns out that one of my models was generated with EF4.0 (no .tt templates, entities inherit from EntityObject), whereas the new model was generated with EF5 (with the .tt DbContext code gen items)

    EF does not generate classes that respect the 'model namespace' you enter when creating your EDMX and generating your model from the DB; all generated entities within a given project will have that project's root namespace, NOT the model's. 

    For instance, suppose:

    • Models:
      -- MyModel.edmx (EF4.0 model, no .tt, EntityObject inheritance)
      -- MyOtherModel.edmx (EF5 model, .tt, DbContext code gen)
    • Mapping:
      -- MyModel: DB1.MyEntities table -> MyEntity object
      -- MyOtherModel: DB2.MyEntities table -> MyEntity object

    Workarounds:

    1. Name the entities in the Model Designer something different. 
    2. Give each EDMX its own namespace:

      EF 4.0:
      right-click EDMX -> properties. Change 'Custom Tool Namespace' to [Project's Default Namespace].[Model Namespace]
                  (using Model Namespace for consistency. it can be whatever you want)

      EF 5.0: do the same thing as EF 4.0, but on both .tt files nested under the EDMX: [ModelName].Context.tt & [ModelName].tt

      This will result in the entities for the EDMXes to be generated using the namespace you give it, and you should no longer get this error if this scenario is what caused it. This is kinda frustrating to me because if any namespaces change (like the Project Default Namespace), you have to remember to change these Custom Tool Namespace properties.

      So, for the example entities above, you'd get..

         namespace ProjectNamespace.MyModel
         {
             public partial class MyEntity : EntityObject {  }
         }
         namespace ProjectNamespace.MyOtherModel
         {
             public partial class MyEntity {  }
         }

      .. and so these won't be seen as partial classes that extend each other..



    Tuesday, April 30, 2013 9:13 PM