自定义类型作为主键 [英] Custom Type as Primary Key

查看:112
本文介绍了自定义类型作为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Visual Studio 2015上使用EF6代码优先。
我正在使用希望将用户定义类型用作主键的数据库。

I'm currently working with EF6 code-first on Visual Studio 2015. I'm working on a database that I want to use user-defined types as primary keys.

这是我想要的一个简单示例:

This is a simple example of what I want:

public class ObjectIdType
{
    public string id { get; set; }
}

这是用户定义的类型。

public class ClasseProva
{

    [Key][DatabaseGenerated(DatabaseGeneratedOption.None)]
    public ObjectIdType Id { get; set; }

}

这将是我要使用的实体我的自定义类型为PrimaryKey。

And this will be the entity where I want to use my custom type as PrimaryKey.

当我执行此示例操作时,尝试创建控制器时发生错误,错误是:属性类型无效密钥类型。

When I do this like the example an error occur when I try to create the controller, the error is: The property type is not a valid key type.

我发现您只能将标量类型用作PK,但是我想知道是否让我的ObjectIdType继承自String会起作用吗?
我试图这样做(显然不起作用):

I found that you just can use scalar types as PK, but i'm wondering if I make my ObjectIdType inherit from String it will work? I tried to do it like this (obviously don't work):

public class ObjectIdType : String
{
    public string id { get; set; }
}

有没有办法将我的ObjectIdType定义为从String继承?还是有任何方法可以使用户定义的类型为PK?

Is there any way to define my ObjectIdType as inheritance from String? Or there is any way to make a user-defined type a PK?

推荐答案


将我的ObjectIdType定义为继承自String的方法?

Is there any way to define my ObjectIdType as inheritance from String?

否。


或者有什么方法可以使用户定义类型为PK?

Or there is any way to make a user-defined type a PK?

不是100%肯定,但是我认为您不能这样做。

Not 100% sure, but i think you can not do that.

这篇关于自定义类型作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆