I have a project that contains many data structures, and I want to introduce a database into it. I am learning the Entity Framework CTP5 function with a codename.
Now I use a separate identifier type for each of my types (e.g. TypeAIdentifier, TypeBIdentifier, etc.). This is good because it allows you to provide type security when working with these identifiers. The problem is that I cannot force EF to use a non-primitive type as an identifier. There is a tip there http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx :
KeyAttribute
KeyAttribute is used to specify that a property/column is part of the primary
key of the entity and applies to scalar properties only.
Indeed, the following program throws an exception:
class Program
{
static void Main(string[] args)
{
MyDb db = new MyDb();
db.SaveChanges();
}
}
class MyDb : DbContext
{
public DbSet<MyObject> MyObjects { get; set; }
}
class MyObject
{
[Key] public MyIdentifierType MyId { get; set; }
public int IntValue { get; set; }
}
class MyIdentifierType
{
public int UniqueId { get; set; }
}
, MissingMethodException ( "no parameterless constructor, " ), , KeyAttribute, :
EF PK? :
- EF - .
- "int" PK EF "int" MyIdentifier ( , , - - , "int" , MyIdentifier , , , ). , Imports , , .
?