Navigation property must be virtual - not required in ef core?

As I recall, in EF the navigation property should be virtual :

public class Blog 
{  
    public int BlogId { get; set; }  
    public string Name { get; set; }  
    public string Url { get; set; }  
    public string Tags { get; set; }  

    public virtual ICollection<Post> Posts { get; set; }  
}

But I am watching EF Core and not seeing it virtual:

public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }

        public ICollection<Enrollment> Enrollments { get; set; }
    }

No more required?

+11
source share
5 answers

virtual never needed in ef. This was only necessary if you needed lazy boot support.

Lazy loading EF Core, virtual . ( ) ( ).

+22

...

, ​​EF Lazy... ... CORE

:

1. :

( ) . , , .

.

2. :

( ) . , : . , .

.

:

( ), , ( , ), ( ), , , , .

, ...

:

( ):

foreach(var line in query)
{
    var v = line.NotVirtual; // I access the property for every line
}

:

foreach(var line in query)
{
   if(line.ID == 509)        // because of this condition
   var v = line.Virtual; // I access the property only once in a while
}

:

1000 , , , . , , , :

context.LazyLoadingEnabled = false;

.

EF:

WhateverEntities db = new WhateverEntities() 
db.Configuration.LazyLoadingEnabled = false;
+9

. 2018 Lazy Loading Entity Framework Core 2.1 .

, , virtual. :

- Microsoft.EntityFrameworkCore.Proxies UseLazyLoadingProxies. [...] EF Core lazy-load , , , .

:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public virtual Blog Blog { get; set; }
}

Lazy -, ILazyLoader . .

, Lazy Loading: . virtual , Lazy Loading . .

+2

Update: The initial lazy boot implementation planned for EF Core 2.1 will require navigation properties to be declared virtual. See https://github.com/aspnet/EntityFrameworkCore/issues/10787 and, more generally, track progress on lazy loading, see https://github.com/aspnet/EntityFrameworkCore/issues/10509 .

+1
source

Source: https://habr.com/ru/post/1670244/


All Articles