简体   繁体   中英

Entity Framework Code First NullReferenceException

I'm using Entity Framework Code First, I've an entity Articolo and ZefiroContext : DbContext which contains the following declaration:

public DbSet<Articolo> Articoli { get; set; }

When I run the following code:

var a = new ZefiroContext().Articoli;
List<Articolo> r;
if (a != null) 
    r = a.ToList();
else
    r = new List<Articolo>();

I get a NullReferenceException at line r = a.ToList(); . I have no Articolo entites in my database so I expect my variable r to point to an empty List<Articolo> .

您只需要。

List<Articolo> r = new ZefiroContext().Articoli.ToList();  

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