-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ForeignKeyAttribute prevents inverse navigation to be properly matched up #16704
Comments
Note for triage: this also repros on 3.0 preview 7. |
@ajcvickers please let me know whenever you have an estimate for when this will be resolved. I need it for my project (which isn't live yet) and might need to disable some functionality until it's fixed. Thanks in advance. |
@AndriySvyryd Can you take a look at this? @Wouter8 We'll have a better idea once we understand the issue. It may get fixed for the 3.0 release, but since that release is already over-booked we may have to push it out to a later release. |
@Wouter8 As a workaround remove the ForeignKeyAttributes and configure the relationships explicitly: builder.Entity<MultipleAnswers>().HasMany(m => m.Answers).WithOne(p => (MultipleAnswers)p.Answer).HasForeignKey(p => p.AnswerId);
builder.Entity<MultipleAnswersRepeating>().HasMany(m => m.Answers).WithOne(p => (MultipleAnswersRepeating)p.Answer).HasForeignKey(p => p.AnswerId); |
Awesome, I'll try that after my vacation. Thanks for getting back to me. |
Tried this solution. This does generate a new column in the new migration, This new column doesn't even work, since it'll leave the existing |
@AndriySvyryd Simplified repo and model built on current nightly: [Table("Answers")]
public abstract class Answer
{
public int Id { get; set; }
}
public class PartialAnswer : PartialAnswerBase
{
}
[Table("PartialAnswers")]
public class PartialAnswerBase
{
public int Id { get; set; }
public int AnswerId { get; set; }
[ForeignKey("AnswerId")]
public virtual Answer Answer { get; set; }
}
public class PartialAnswerRepeating : PartialAnswer
{
}
public class MultipleAnswers : Answer
{
public virtual ICollection<PartialAnswer> Answers { get; set; }
}
public class MultipleAnswersRepeating : Answer
{
public virtual ICollection<PartialAnswerRepeating> Answers { get; set; }
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
public DbSet<Answer> Answers { get; set; }
public DbSet<PartialAnswerBase> PartialAnswers { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
// Answers
builder.Entity<MultipleAnswers>()
.HasMany(m => m.Answers)
.WithOne(p => (MultipleAnswers)p.Answer)
.HasForeignKey(p => p.AnswerId);
builder.Entity<MultipleAnswersRepeating>()
.HasMany(m => m.Answers)
.WithOne(p => (MultipleAnswersRepeating)p.Answer)
.HasForeignKey(p => p.AnswerId);
// PartialAnswers
builder.Entity<PartialAnswer>();
builder.Entity<PartialAnswerRepeating>();
}
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
var model = context.Model;
}
}
}
|
@Wouter8 For this to work |
Some cleanup in relationship builders Resolves #16704
Some cleanup in relationship builders Resolves #16704
Some cleanup in relationship builders Resolves #16704
I've created a relatively complex structure of entities. I'm creating a quiz site and on this site I want to have multiple types of questions and therefor also custom types of answers.
I've created a simple method where an answer gets created based on the type of question it is answering. The object I'm getting is correct - it has the correct type and all fields are filled in correctly as well. Upon saving the context with a newly added answer of type
MultipleAnswersRepeating
I get an error that entity framework is trying to cast my object to another answer type,MultipleAnswers
.Unable to cast object of type xxx.Models.Answers.MultipleAnswersRepeating' to type 'xxx.Models.Answers.MultipleAnswers'
See the class definitions:
I'm getting exactly the same error in a very small project with just the relevant classes. You can find this project here.
I have specified all types that inherit from a base class in
OnModelCreating
.If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message:
Unable to cast object of type 'EFErrorTest.Models.Answers.MultipleAnswersRepeating' to type 'EFErrorTest.Models.Answers.MultipleAnswers'.
Stack trace:
The text was updated successfully, but these errors were encountered: