none
EF4.0 wrong namespace for System.Data.Metadata.Edm.EntityType RRS feed

  • Question

  • I am having a little problem in EF4 code-first (i think that may be a problem only using CF actually).

    I am trying to get some info of an entity type from my context metadata using a code similar to this: 

    return (context as System.Data.Entity.Infrastructure.IObjectContextAdapter)
    .ObjectContext
    .MetadataWorkspace
    .GetItems<EntityType>(DataSpace.CSpace)
    .Where(x => x.FullName == ModelType.FullName)
    .FirstOrDefault();

    ModelType is a parameter wich contains the System.Type of the entity to search for.
    It should return a System.Data.Metadata.Edm.EntityType instance with the type's metadata. 

    The model referenced in the DbSet is named MyApp.Models.User and the DbContext class was created under the MyApp.Servicing namespace

    Now the problem i am having is that the FullName property of System.Data.Metadata.Edm.EntityType is **MyApp.Servicing**.User (O_O) instead of MyApp.Models.User.

    *I think it may be that EF is mocking the edm Metadata as i don't have an EDMX in code-first but that's just guessing.*

    Any idea of why does this happens? i can solve it another way but would like to know why.

    (Also posted this in stacknbsp;http://stackoverflow.com/questions/18869312/ef4-0-wrong-namespace-for-system-data-metadata-edm-entitytype)


    Wednesday, September 18, 2013 10:57 AM

Answers

  • Hello,

    Welcome to this forum.

    If I understand correctly, you want to know why the EntityType is MyApp.Servicing.User instead of MyApp.Models.User.

    As far as I know, the DataSpace have five selections:

    1. OSpace , Represents the default name for the object model
    2. CSpace, Represents the default name for the conceptual model.
    3. SSpace, Represents the default name for the storage model.
    4. OCSpace, Represents the default name for the mapping between the object model and the conceptual model.
    5. CSSpace , Represents the default name for the mapping between the conceptual model and the storage model.

    So if you want EntityType to be MyApp.Models.User.

    We need to use OSpace like below:

    using (GuidContext db = new GuidContext())
    
                {
    
                    //db.Database.Create();
    
    
                    EntityType et = (db as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.OSpace).Where(x => x.FullName == "Code_FirstFortest.GuidClass").FirstOrDefault();
    
                } 
    
    GuidClass:
    
    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel.DataAnnotations;
    
    using System.ComponentModel.DataAnnotations.Schema;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace Code_FirstFortest
    
    {
    
        class GuidClass
    
        {
    
            [Key]
    
            [Column(Order = 0, TypeName = "UNIQUEIDENTIFIER")]
    
            [MaxLength(16)]
    
            public Guid Id { get; set; }
    
        }
    
    }
    
    GuidContext:
    
    using System;
    
    using System.Collections.Generic;
    
    using System.Data.Entity;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace Code_First
    
    {
    
        class GuidContext : DbContext
    
        {
    
            public DbSet<Code_FirstFortest.GuidClass> GuidClasses { get; set; }
    
        }
    
    }
    

    The type is Code_FirstFortest.GuidClass.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 20, 2013 4:05 AM
    Moderator

All replies

  • Hello,

    Welcome to this forum.

    If I understand correctly, you want to know why the EntityType is MyApp.Servicing.User instead of MyApp.Models.User.

    As far as I know, the DataSpace have five selections:

    1. OSpace , Represents the default name for the object model
    2. CSpace, Represents the default name for the conceptual model.
    3. SSpace, Represents the default name for the storage model.
    4. OCSpace, Represents the default name for the mapping between the object model and the conceptual model.
    5. CSSpace , Represents the default name for the mapping between the conceptual model and the storage model.

    So if you want EntityType to be MyApp.Models.User.

    We need to use OSpace like below:

    using (GuidContext db = new GuidContext())
    
                {
    
                    //db.Database.Create();
    
    
                    EntityType et = (db as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.OSpace).Where(x => x.FullName == "Code_FirstFortest.GuidClass").FirstOrDefault();
    
                } 
    
    GuidClass:
    
    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel.DataAnnotations;
    
    using System.ComponentModel.DataAnnotations.Schema;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace Code_FirstFortest
    
    {
    
        class GuidClass
    
        {
    
            [Key]
    
            [Column(Order = 0, TypeName = "UNIQUEIDENTIFIER")]
    
            [MaxLength(16)]
    
            public Guid Id { get; set; }
    
        }
    
    }
    
    GuidContext:
    
    using System;
    
    using System.Collections.Generic;
    
    using System.Data.Entity;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    
    namespace Code_First
    
    {
    
        class GuidContext : DbContext
    
        {
    
            public DbSet<Code_FirstFortest.GuidClass> GuidClasses { get; set; }
    
        }
    
    }
    

    The type is Code_FirstFortest.GuidClass.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 20, 2013 4:05 AM
    Moderator
  •  

    Just for your information only Code First was not introduced until entity framework version 4.1

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, September 20, 2013 4:28 AM