views:

164

answers:

1

I'm using the Microsoft.Data.Entity.CTP (in the Entity Framework CTP) under the .NET 4 framework to create the EDMX metadata from my C# classes to create a database schema.

I setup a simple model as such:

public class AModelContainer : ObjectContext
{
    public IObjectSet<RegularClass> RegularClasses { 
        get { return CreateObjectSet<RegularClass>(); }
    }
}

I follow the simple pattern of defining a new ContextBuilder based on my model.

var builder = new ContextBuilder<AModelContainer>();

using(var context = builder.Create(new SqlConnection(connString)))
{
    context.RegularClasses.AddObject(new RegularClass());

    context.SaveChanges();
}

This works fine. Until I try to do something a little more complex...

I extend my model with a generic class

public class AModelContainer : ObjectContext
{
    public IObjectSet<SpecialClass<string>> SpecialClasses { 
        get { return CreateObjectSet<SpecialClass<string>>(); }
    }
}

Now on the save I get an exception:

Mapping and metadata information could not be found for EntityType 'Prototype.SpecialClass`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

On this line in the AModelContainer:

return CreateObjectSet<SpecialClass<string>>();

The default constructor of my generic 'SpecialClass' does nothing at the moment, should it?

public class SpecialClass<T> 
{
    public SpecialClass()
    { }
}

Or is this an issue with the ContextBuilder not knowing what to do exactly, is there a way to use builder.ComplexType(), or other method to guide it?

Or the CTP can't deal with this scenario yet...

That "`1" after my class name also doesn't sit well with me in the exception...

+4  A: 

You can't use Generic classes as Entities with the EF (and by extension Code-Only) today.

This is a limitation of the Mapping capabilities between the CLR and the Conceptual Model.

Post Beta2 we added the ability to allow this sort of thing:

public class Entity<TKey>
{
   public TKey ID {get;set;}
}

public class Person: Entity<int>
{
   public string Firstname {get;set;}
   public string Surname {get;set;}
}

Where only Person is an 'Entity' in the EF's model, and the ID property from the base class shows up as a property of Person.

But there are no plans in .NET 4.0 / EF 4 to support mapping a generic class itself to an Entity in the model.

Hope this helps

Alex

Alex James
Fair enough, thanks :)
Nick Josevski