实体框架 CTP5 Code First:使用非原始类型作为键
我有一个项目,有很多数据结构,我想在其中引入一个数据库。我正在研究实体框架 CTP5 的代码优先功能。
现在,我为每种类型使用不同的标识符类型(例如 TypeAIdentifier、TypeBIdentifier 等)。这很好,因为它在使用这些标识符时允许类型安全。问题是我似乎无法让 EF 使用非基本类型作为标识符。
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.
确实,以下程序抛出异常:
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(“没有为此对象定义无参数构造函数”),但我确信这与 KeyAttribute 有关,因为以下任一情况都会使该程序运行:
- 删除[Key];或
- 将 [Key] 移至 IntValue。
如何让 EF 将我的标识符类映射为 PK?我想到了两个选项:
- 找出一种方法让 EF 使用非原始类型作为 PK。
- 将“int”作为 PK 并让 EF 将此“int”转换为 MyIdentifier(这样做的缺点是,如果我决定使用“int”以外的其他内容,我将必须更新 MyIdentifier 和映射,但它至少可以让我保留标识符类型的安全性)。我以为函数导入可以让我做到这一点,但它似乎是建立在存储过程之上的。
有办法实现这一点吗?
I have a project which has quite a few data-structures, and I want to introduce a database into it. I'm looking into Entity Framework CTP5's code-first feature.
Now, I'm using a distinct Identifier type for each of my types (e.g. TypeAIdentifier, TypeBIdentifier, etc.). It's nice because it allows for type safety when working with these identifiers. The problem is that I can't seem to get EF to use a non-primitive-type as an identifier. There's a hint in 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; }
}
Strangely enough this program throws a MissingMethodException ("no parameterless constructor defined for this object"), but I'm sure this is related to the KeyAttribute because either of the following makes the program run:
- Remove [Key]; or
- Move [Key] to IntValue.
How can I get EF to map my Identifier classes as PKs? Two options that come to mind:
- Figure out a way to get EF to use a non-primitive-type as a PK.
- Put an 'int' as the PK and get EF to convert this 'int' to MyIdentifier (this has the disadvantage that if I ever decide to use something other than an 'int' I'll have to update both MyIdentifier and the mapping, but it will at least let me preserve the identifier type safety). I thought Function Imports would let me do this, but it seems that are built on top of stored procedures.
Is there a way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将带有属性的密钥转换为自定义类型。
I'd convert the key with a property to the custom type.