Consider the following model:
public class Developer
{
public string Alias { get; set; }
public __int?__ TeamId { get; set; }
public Team DevTeam { get; set; }
}
public class Team
{
public int Id { get; set; }
public ICollection<Developer> Developers { get; set; }
}
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Developer>().HasKey(d => new {d.Alias, d.TeamId});
__ modelBuilder.Entity<Developer>().Property(d => d.TeamId).IsOptional();
__
}
}
This throws:
TeamId: Nullable: Key part 'TeamId' for type 'Developer' is not valid. All parts of the key must be non-nullable.
at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
I would expect this to work and generate 0..1 - Many relationship
public class Developer
{
public string Alias { get; set; }
public __int?__ TeamId { get; set; }
public Team DevTeam { get; set; }
}
public class Team
{
public int Id { get; set; }
public ICollection<Developer> Developers { get; set; }
}
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Developer>().HasKey(d => new {d.Alias, d.TeamId});
__ modelBuilder.Entity<Developer>().Property(d => d.TeamId).IsOptional();
__
}
}
This throws:
TeamId: Nullable: Key part 'TeamId' for type 'Developer' is not valid. All parts of the key must be non-nullable.
at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
I would expect this to work and generate 0..1 - Many relationship