What is New in Asp Net 7 and EF Core 7 (Best Features)


Asp.Net Core 7

Navigation:
If you want the Entity to be included in another Entity, you can now add a [Owened] Data Annotation to the Entity you want to be included.
- When you add [Owned] this automatically brings in this Owned Entity every time you retrieve the parent, so if you don't want this behavior then do not use it. However, if you want to use Navigation in your Models this is a great way to go.

See the example below:

public class Customer {

public Contact Contact {get; set}
}

[Owned]//This will add the Contact Entity into the Customer
//Table, will be all one table
[Table("Contacts")]//This will create a separate Table called Contacts
//Will not include the Contact table in the Customer Table.
public class Contact{

}

This means that the #Customer Entity is part of the Aggregate. When the code above is executed, the EF #Core now creates a Customer Table in it is a Contact table marked as Shared. Keep in mind that there is no need of creating a DbSet for a Contact Entity. 

[Note]: In Asp.Net 7 you can now use await foreach to loop through data coming from the #Database Context as AsyncEnumerable. See the code below:

await foreach(var user in _context.Users.AsAsynchEnumerable()){

//Do you stuff

}


[Note]: If you want to store data as #JSON in #SQL Server using Entity Framework 7 just add the .ToJson() in the OnModelCreating. See the code below:

protected override void OnModelCreating(ModelBulder modelBuilder){
modelBulder
     .Entity<Customer>()
     .OwnsOne(customer => customer.Contact)
     .ToJson();//In SQL Server this data is stored as nvachar(max)
     //but the margic happens when you are able to query into that blob
    //of JSON.
}

Keep in mind that when you do bulk updates or deletes, the ChangeTracker is not aware of what is going on. Just in case you see that the Entity Tracked are not updated. The changes go straight to the database.


Used Case for storing JSON into SQL Server column using Entity Framework:
1. This is useful that you could store the JSON data in one column, one use case for this is when you need to store the User Feed in that User's specify Row.
2. You could utilize this feature for storing Adjucency data for Graph Data Structure in one column.
3. Utilizing JSON Rows has tremendous Performance benefits as you will not perform any joins when retrieving the data.

Optimizing Performance when working with JSON Columns in Entity Framework
1.  Add a Property to the JSON Entity Model that is not going to be inside the Json String stored in JSON Column, this property is going to be computed called ComputedProperty, which could be a different name.

Make sure you map or let the EF Core Database Context know that the column is a computed Property.

public class M{
public M1 m1 {get; set;}
public string m1Computed {get; set;}
}

//This is the best way to optimize SQL Expression
//from EF Core side of things
OnModelCreate(){

modelBuilder.Entity<M>()
                     .Property(c => c.m1Computed)
                     .HasComputedColumnSql("JSON_VALUE(M1,'$.propertyInSideJson.AnotherPropertyIfApplicable')");

//Then define an index on the M object
modelBulder.Entity<M>()
                     .HasIndex(c => c.m1Computed);
}

   - If you use a WHERE clause on a property that is not an INDEX there is no performance benefit, the property has to be an Index and the SQL Server Database Engine has
      know about that index to gain performance benefits.

    - Keep in mind that when you ask the Database to create an Index, you are asking it to do more work. If you happen not to query the database on that
      index you created then the Index is useless.  Working with Indexes has performance Penalties when you update or Insert into the database. 

    - This is specific to SQL Server Index Optimization, every database will be different.




What is New in Entity Framework 7

[Note]:
Bulk Updates: In the past, if you wanted to update a number of records, EF Core made separate trips to the Database to update even just one column. Now in EF Core 7, you can build updates in just one trip.

await context.Blogs
                      .Where(a => a.Likes == 1)
                      .ExecuteUpdateAsync(updates =>
                               updates.SetProperty(b =>
                                            b.ThePropertyIWantToChange, ValueIWant)
                      );
//Keep in mind that you don't need to do context.SaveChanges() anymore
//with this query

[Note]: You could also delete in bulk, this is a game changer for performance

await context.Blogs.Where(a => a.Something == Something)
                      . ExecuteDeletyAsync();
//Keep in mind that you don't need context.SaveChanges()
//in this case.

[NB]: Incrementing a value in bulk

//You don't have to type context.SaveChanges()
//The query above will produce a JOIN on Blogs

await context.Posts
                      .Where(p => p.Blogs.Rating == 1)
                      .ExecuteUpdateAsync(u => u.SetProperty(p => p.Views,p => p.Views + 1));


5. EF Core 7 now has a new feature called DataSource, which allows you to create database connections on the fly.
    - It is Thread Safe, it is more like Database Connection Pooling. Unlike DBContext which is not ThreadSafe
          - This is huge in terms of using an ORM for your data Queries
    - It is a connection Pool behind the scene.

6. EF Core has now a new feature called "Aggregate function" you can now perform the Aggregate function right on the Entity like below"
   

var max = _context.Blogs.Max(b => b.value);?



DataSource:
    1. You can now create two different Database Connections if you want to do Operations in Parallel,
       - Because DBContext is not thread-safe, that is why you see the error "Another Operation Started before the previous one completed" error in EF Core 6. This was a nightmare
         to debug especially when the function utilizing DbContext is an Async fun
    2. If you don't use the DI to open the connection to the database, that is if you use a data source straight, then you are responsible for disposing of the DB connection since you created it using the DataSource Factory, do not forget that best practice is to wrap the code in a 
        "using" directive.

2. You could use String.Join to return a string of comma delimited string of values from the database

var names = string.Join(" ,", a.Select(b => b.Names));?

 


 

Asp.Net 7 Features

  1. Rate Limiting
    2. Output Caching
    3. Web Socket Connection is now supported in HTTP/2
         Previously it was only supported in HTTP/1 this time it will be faster because might support Multiplexing.
    4. The coming of HTTP/3 and Quick

Language and Runtime

  1. Improvements around StartsWith and EndsWith


    References:

1. .Net Data Community Standup - JSON Columns

2. https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-2/

2. EF Core 7 Data Source Feature

3. EF 7 Core Aggregate Functions

4. Build Updates in EF Core
  





Josh said:

Is this a question, do you need help resolving it.

Posted On: September 12, 2022 13:05:19 PM
Bo said:

Hello Team, I am new here.

Posted On: September 11, 2022 17:04:19 PM
Bang said:

Hello

Posted On: September 11, 2022 9:14:07 AM
Jack said:

What a mess

Posted On: September 11, 2022 8:46:41 AM
Mark said:

This is so awesome

Posted On: September 11, 2022 8:43:29 AM

For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]