Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Created Unassigned: Code First :: foreign key nullability is not determined correctly if its part of a primary key [1072]

$
0
0
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



Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>