Skip to content
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

NullReferenceException on call to Ignore() #3865

Closed
divega opened this issue Nov 23, 2015 · 1 comment
Closed

NullReferenceException on call to Ignore() #3865

divega opened this issue Nov 23, 2015 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@divega
Copy link
Contributor

divega commented Nov 23, 2015

Note that I haven't tried to repro this with the latest bits buts I am copying from comment #3727 (comment) by @miroslavsiska and is the same repro as #3863 but with the call to Ignore<AllergenQuantity>() added. In the end this is an invalid model.

According to the comment the Ignore() call fails with NullReferneceException:

public class AllergenQuantity
{              
    public Guid UnitId { get; set; }
    public virtual Unit Unit { get; set; }         

    [Display(Name = "Množství obsahu")]
    public decimal Quantity { get; set; }
}


 public class Unit 
{
    public Unit()
    {
        this.Id = Guid.NewGuid();
    }
    public virtual ICollection<AllergenQuantity> AllergenQuantity { get; set; }

    [Key]
    public Guid Id { get; set; }     

    [Display(Name = "Jednotka")]
    public string Name { get; set; }

    [Display(Name = "Typ Jednotky")]
    public string UnitType { get; set; }

    [Display(Name = "Použité v kulturách")]
    public string Cultures { get; set; }
}


  public class TenantDbContext : IdentityDbContext<TenantUser, TenantRole, Guid>
{
    private string _connectionString { get; set; }

    public TenantDbContext(DbContextOptions<TenantDbContext> options) : base(options) 
    {
        this._connectionString = "CON STRING";
    }

    public TenantDbContext()       
    {
    }   
    public TenantDbContext(string ConnectionString)
    {
        this._connectionString = ConnectionString;
    }
    public static TenantDbContext Create(string ConnectionString)
    {
        return new TenantDbContext(ConnectionString);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    { 
        optionsBuilder.UseSqlServer(_connectionString);  
    }

    //  BASE
    public DbSet<Unit> Unit { get; set; }

    //  Stock
    public DbSet<Allergen> Allergen { get; set; }  
    public DbSet<AllergenQuantityMaterial> AllergenQuantityMaterial { get; set; }        //Inherited from AllergenQuantity
    public DbSet<Material> Material { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {      
        base.OnModelCreating(builder);
        // throws reference not set to object instance...
        builder.Ignore<AllergenQuantity>();   
        //Composites keys mapping
        builder.Entity<AllergenQuantityMaterial>().HasKey(l => new { l.MaterialId, l.AllergenId });  
    }
}

.

@divega divega changed the title NullReferenceException on calll to Ignore() NullReferenceException on call to Ignore() Nov 23, 2015
@smitpatel smitpatel self-assigned this Nov 24, 2015
@rowanmiller rowanmiller added this to the 7.0.0 milestone Nov 24, 2015
@smitpatel
Copy link
Contributor

This is working in latest code now.
Possible reason for the exception
https://github.com/aspnet/EntityFramework/blob/7.0.0-rc1/src/EntityFramework.Core/Metadata/Internal/InternalEntityTypeBuilder.cs#L302
The base entity type builder returning null. Since the implementation in metadata changed in this area after rc1 it does not repro anymore.

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants