参考此问题:EF4.1 Code First Complex Type as primary key
此后有变化吗?或者除了创建重复的持久性对象之外是否还有可能的解决方法?
答案 0 :(得分:1)
解决方法是在基类中实现与PK相关的逻辑,从中派生实体。
编辑:基于引用问题的快速示例
class TestDbContext : DbContext
{
public DbSet<Voyage> Voyages { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Voyage>()
.HasKey(v => v.Id);
base.OnModelCreating(modelBuilder);
}
}
public class VoyageNumber
{
private string _number;
public VoyageNumber() { }
public string Id
{
get
{
return _number;
}
set
{
if( !string.IsNullOrEmpty( _number ) )
{
throw new InvalidOperationException("Id already set");
}
if( string.IsNullOrEmpty( value ) )
{
throw new ArgumentException("Value cannot be null or empty string");
}
_number = value;
}
}
}
public class Voyage : VoyageNumber
{
}