我目前正在尝试使用 Entity Framework 4.1 的 RC 及其代码优先方法为我的域对象实现存储库。
现在我有一个域实体“Voyage”,它有一个封装在“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; }
}
现在,当我在 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.
当我尝试这个时:
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'
我真的必须丢弃我的 VoyageNumber 并用原始类型替换它吗?
最佳答案
这是限制。键成员只能是实体中直接的标量属性。复杂类型表示为不支持的复杂属性。
关于.net - EF4.1 Code First 复杂类型作为主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5522584/