简体   繁体   中英

ef core .Include().Contains() null reference exception

I have my models setup like this...

public class Model1 : IEquatable<Model1>
{
    public int Model1Id { get; set; }
    public string Name1 { get; set; }
    public Model2 Model2 { get; set; }
    public int Model2Id { get; set; }

    public bool Equals(Model1 other)
    {
        return this.Model2.Equals(other.Model2)
            && this.Name1 == other.Name1;
    }
}

public class Model2 : IEquatable<Model2>
{
    public int Model2Id { get; set; }
    public string Name2 { get; set; }

    public bool Equals(Model2 other)
    {
        return this.Name2 == other.Name2;
    }
}

public class ModelContext : DbContext
{
    public DbSet<Model1> Model1 { get; set; }
    public DbSet<Model2> Model2 { get; set; }
    public ModelContext(DbContextOptions<ModelContext> options) : base(options) { }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Model1>(b =>
        {
            b.HasOne(m1 => m1.Model2).WithMany().HasForeignKey(m1 => m1.Model2Id);
        });
    }
}

then I get a null reference exception when I do this...

static void Main(string[] args)
{
    var myModel1 = new Model1
    {
        Name1 = "myName1",
        Model2 = new Model2
        {
            Name2 = "myName2"
        }
    };
    var connection = new SqliteConnection("DataSource=:memory:");
    connection.Open();
    try
    {
        var options = new DbContextOptionsBuilder<ModelContext>()
            .UseSqlite(connection)
            .Options;

        //create database
        using(var ctx = new ModelContext(options))
        {
            ctx.Database.EnsureCreated();
        }

        //add model objects
        using (var ctx = new ModelContext(options))
        {
            ctx.Database.EnsureCreated();
            ctx.Model1.Add(myModel1);
            ctx.SaveChanges();
        }

        //check if exists
        using(var ctx = new ModelContext(options))
        {
            //exception here
            bool isExists = ctx.Model1.Include(m1 => m1.Model2).Contains(myModel1); 
            Console.WriteLine(isExists);
        }
    }
    finally
    {
        connection.Close();
    }

    Console.ReadKey();
}

I'm expeting the Model2 instance of my m1 to be populated when I call the Include but it is still null .

but If I add AsEnumerable() to my query like..

ctx.Model1.Include(m1 => m1.Model2).AsEnumerable().Contains(model1);

then everything works fine.

EDIT:

my question is... why do I need to call AsEnumerable()? I was expecting it to work without calling AsEnumerable()..

The difference is one is an entityframe work call the other is linq to objects Entity Framework Does not understand contains for a CLR Object

public void AddIfNotExists(Model1 model1)
{

    //No Need for the include this is executed in sql,  assuming the model 2 
    //property has already been included in your model1 this should work fine
    if(false == _context.Model1.Any(x => x.Name1 == model1.Name1 
                        && x.Model2.Name2 == model1.Model2.Name2))
    {
        _context.Model1.Add(model1);
    }
}

I made this based off of your logic, but chances are you really just want to check if model1.id is the the model1 set. But I have no Idea what your architecture is doing so this is what you probably want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM