Reputation: 2229
Refering to this question: EF4.1 Code First Complex Type as primary key
Has this changed since? Or are there possible workarounds other than creating duplicate persistence objects?
Upvotes: 1
Views: 1536
Reputation: 16498
A work around would be implementing PK-related logic in a base class from which you derive your entities.
Edit: quick example based on referenced question
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
{
}
Upvotes: 1