-
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
System.Object being discovered as EntityType through navigation. #3727
Comments
With the mapping
Result StackTrace:
|
Tried running both repro using |
@smitpatel The second exceptions seems to be related to the first one. I have put out all related parts in a new project, Project is working in beta8, in later versions it will throw exception. Have tested with the rc1 from aspnetrelease and rc2 from aspnetvnext myget feeds. beta8: https://dl.dropboxusercontent.com/u/16590863/EF-DeriviedExceptionTest-beta8.zip |
Thanks for the codes. The only change I had to make was fixing Everything works fine in beta8, following exception is thrown in rc1 & rc2 |
We should only include base types in the model if they are explicitly configured. Are you saying we automatically pull base types into the model now? |
I am debugging into it. The exception is thrown because we have set base type to |
Found the issue. The model entity type Fix should be - do not discover object as navigation type since the target type is never valid entity type. |
@smitpatel Agreed that it is safe to always discard |
@divega - Our logic for discard entities without keys happen in |
Ok, understood that it works that way, but how would this case be different from having any other base type that doesn't have keys and appears as the type of a property in in the model? Or are you saying a model in which there is a property of type System.Object is valid without an Ignore() on it? I think that is debatable 😄 |
In general we don't know whether an entity type will have a primary key before discovering it. |
If type mapper supports mapping |
I don't disagree we can special case System.Object, I just want to make sure that we don't miss other examples of the same issue with other types. |
Agreed... Hang on, |
And that will be base type of all other entity types in the model. 😄 |
We could use KeyDiscoveryConvention to test whether the PK would be discovered before trying to add the entity type to the model, but we decided not to do that since it would make it harder to find out why an entity type wasn't discovered when the user expected that it would |
@Tasteful - for the issue you reported, |
@smitpatel The fix you was doing, will that popup in the |
@Tasteful There will not be any Json datatype in SQL 2016... http://blogs.msdn.com/b/jocapc/archive/2015/05/16/json-support-in-sql-server-2016.aspx |
@ErikEJ SQL2016 will have JSON support, even if the datatype in SQL will be nvarchar (regarding to the link you was referring to). But that article will not handle how the conversion between object and JSON is made in the .net side, that can be manual part as today or full automatic. I haven't dig deeper into that than I have read that SQL2016 will support JSON. |
@Tasteful - The change will appear in nightly builds - that is EF supports SQL2008 onwards, further to support a datastore type, we need conversion from data store type to CLR type so mapping json datatype to |
I have the same problem. beta 8 worked, rc1-final not working... Here is my model:
Fluent API:
Why i need it:
Of coarse similar pattern has been used by me for many other tables. I have complex model... You can use in Beta 8 one table with defined columns and next tables derived from it, until you can specify keys in derived tables... Now is this not working. Is it bug or new feature please... ????
.....but previous pattern was better... Thank you |
@miroslavsiska this seems to be a different issue, even if the symptoms are similar. I believe what is happening is that Code First is bringing Since you have simplified your model there are a couple of things that aren't clear:
cc @smitpatel |
@miroslavsiska - that is a different scenario that the one being discussed in this issue. Can you open a new issue with details of your scenario. Please be sure to include a complete code listing to reproduce the issue as I tried creating a console app and everything worked as expected... using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;
namespace Repro
{
class Program
{
static void Main(string[] args)
{
using (var db = new MyContext())
{
db.Database.EnsureCreated();
var peanutButter = new Material { Name = "Peanut Butter" };
db.Materials.Add(peanutButter);
var peanuts = new Allergen { Name = "Peanuts" };
db.Allergens.Add(peanuts);
db.AllergenQuantityMaterials.Add(new AllergenQuantityMaterial
{
Allergen = peanuts,
Material = peanutButter,
Quantity = 1000
});
db.SaveChanges();
}
}
}
public class MyContext : DbContext
{
public DbSet<Material> Materials { get; set; }
public DbSet<Allergen> Allergens { get; set; }
public DbSet<AllergenQuantityMaterial> AllergenQuantityMaterials { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<AllergenQuantityMaterial>()
.HasKey(l => new { l.MaterialId, l.AllergenId });
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Repro;Trusted_Connection=True;");
}
}
public class AllergenQuantity
{
public decimal Quantity { get; set; }
}
public class AllergenQuantityMaterial : AllergenQuantity
{
public Guid MaterialId { get; set; }
public virtual Material Material { get; set; }
public Guid AllergenId { get; set; }
public virtual Allergen Allergen { get; set; }
}
public class Material
{
public Material()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }
}
public class Allergen
{
public Allergen()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }
}
} |
@divega
If is this new Issue, I send you my complete solution... But according your comment. maybe is problem this: |
Yes, I think it would be very useful to have this as a new issue. From what I see it must be the If you are actually planning to use that navigation property to hold instances of different derived types of
Regardless of that, the exception you are getting should be improved. The NullReferenceException from the call to |
@miroslavsiska Sorry, what I meant is to ask you to open a new bug with this information. But don't worry. I can do it myself. |
I recently ran across this problem, and tried to solve it by calling Ignore() before doing anything else in OnModelCreating(). Unfortunately, that doesn't solve the problem -- System.Object still gets discovered. However, applying the NotMapped annotation to the property in the class definition does solve the problem. It seems odd that the annotation approach works but the fluent approach does not. Not sure why that is. |
@markolbert what version of the bits are you using? This was fixed post RC1. |
Diego, |
@markolbert RC2 hasn't shipped the closest we have is staged in this NuGet feed: https://www.myget.org/F/aspnetrelease/api/v3/index.json (More information about the different feeds at https://github.com/aspnet/Home/wiki/NuGet-feeds) I don't think we have any comprehensive guidance on how to move an existing RC1 project to RC2 yet. |
I tried today to update my solution to RC1 from beta8 and get the exception
The derived type 'FieldDefinitionEntity' cannot have keys other than those declared on the root type.
. The root type is not mapped, only the derivied type. Should the mapping throw this exception anyway?Mapping
The text was updated successfully, but these errors were encountered: