EF4.1 code首先复杂类型作为主键 [英] EF4.1 Code First Complex Type as primary key

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

问题描述

我目前正在实施的存储库我与实体框架4.1的RC和code第一种方法域对象。 现在我有一个域实体海角,它有封装类型VoyageNumber的唯一标识符

 公共类VoyageNumber
{
    私人只读串号;

    公共VoyageNumber(串号)
    {
        Validate.NotNull(数字,VoyageNumber需要);

        this.number =号;
    }

    公共字符串ID
    {
        {返回数; }
    }
 

现在我得到一个异常,当我做到这一点我的DbContext的配置:

  modelBuilder.Entity<航程>()HasKey< VoyageNumber>(K => k.VoyageNumber);
 

  

属性VoyageNumber不能   用作对实体关键属性   Domain.Model.Voyages.Voyage',因为   物业类型不是有效的密钥   类型。只有标量类型,串并   byte []的支持的密钥类型。

和还当我尝试这样的:

  modelBuilder.Entity<航程>()HasKey<字符串>(K => k.VoyageNumber.Id);
 

  

属性EX pression'K =>   k.VoyageNumber.Id'无效。该   EX pression应该重新present一个   属性:C#:T => t.MyProperty

难道我真的有垃圾我VoyageNumber,并将其与原始类型替换?

解决方案

这是限制。键部件可以直接在实体是唯一标量属性。复杂类型是重新presented作为不被支持的复杂属性

I'm currently trying to implement the repositories for my domain objects with the RC of Entity Framework 4.1 and its code first approach. Now I have a domain entity "Voyage" which has a unique identifier encapsulated in the type "VoyageNumber"

public class VoyageNumber
{
    private readonly string number;

    public VoyageNumber(string number)
    {
        Validate.NotNull(number, "VoyageNumber is required");

        this.number = number;
    }

    public string Id
    {
        get { return number; }
    }

Now I get an exception when i do this in the configuration of my DbContext:

modelBuilder.Entity<Voyage>().HasKey<VoyageNumber>(k => k.VoyageNumber);

The property 'VoyageNumber' cannot be used as a key property on the entity 'Domain.Model.Voyages.Voyage' because the property type is not a valid key type. Only scalar types, string and byte[] are supported key types.

and also when I try this:

modelBuilder.Entity<Voyage>().HasKey<string>(k => k.VoyageNumber.Id);

The properties expression 'k => k.VoyageNumber.Id' is not valid. The expression should represent a property: C#: 't => t.MyProperty'

Do I really have to trash my VoyageNumber and replace it with a primitive type?

解决方案

This is the limitation. Key members can be only scalar properties directly in the entity. Complex type is represented as complex property which is not supported.

这篇关于EF4.1 code首先复杂类型作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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