Settings Results in 4 milliseconds

Connection Timeout Expired.  The timeout period el ...
Category: Home

 Connection Timeout Expired.  The timeout period elapsed while attempting to consume&n ...


Views: 0 Likes: 18
An Exception occurred in the database while saving ...
Category: .Net 7

Question How do you solve for Entity Framework Context Error "<span style="back ...


Views: 0 Likes: 50
InvalidOperationException: The property 'EntityNam ...
Category: Questions

Question InvalidOperationException The property 'EntityName' has a temporary value while attemp ...


Views: 26 Likes: 65
SQL Server Tips and Tricks
Category: SQL

Error Debugging Did you know you could double click on the SQL Error and ...


Views: 0 Likes: 44
InvalidOperation: The connection was not closed. T ...
Category: Entity Framework

Question Why is this error showing up? the error says "InvalidOperation The co ...


Views: 0 Likes: 41
InvalidOperationException: An error was generated ...
Category: .Net 7

Question How do you retrieve data from two different tables using Linq in Asp.Net 5 Entity Frame ...


Views: 35 Likes: 58
How to Count the Number of records in the Database ...
Category: .Net 7

Question How do you get a Count ...


Views: 532 Likes: 132
Trying out MongoDB with EF Core using Testcontainers
Trying out MongoDB with EF Core using Testcontaine ...

Helping developers use both relational and non-relational databases effectively was one of the original tenets of EF Core. To this end, there has been an EF Core database provider for Azure Cosmos DB document databases for many years now. Recently, the EF Core team has been collaborating with engineers from MongoDB to bring support for MongoDB to EF Core. The initial result of this collaboration is the first preview release of the MongoDB provider for EF Core. In this post, we will try out the MongoDB provider for EF Core by using it to Map a C# object model to documents in a MongoDB database Use EF to save some documents to the database Write LINQ queries to retrieve documents from the database Make changes to a document and use EF’s change tracking to update the document The code shown in this post can be found on GitHub. Testcontainers It’s very easy to get a MongoDB database in the cloud that you can use to try things out. However, Testcontainers is another way to test code with different database systems which is particularly suited to Running automated tests against the database Creating standalone reproductions when reporting issues Trying out new things with minimal setup Testcontainers are distributed as NuGet packages that take care of running a container containing a configured ready-to-use database system. The containers use Docker or a Docker-alternative to run, so this may need to be installed on your machine if you don’t already have it. See Welcome to Testcontainers for .NET! for more details. Other than starting Docker, you don’t need to do anything else except import the NuGet package. The C# project We’ll use a simple console application to try out MongoDB with EF Core. This project needs two package references MongoDB.EntityFrameworkCore to install the EF Core provider. This package also transitives installs the common EF Core packages and the MongoDB.Driver package which is used by the EF Provider to access the MongoDB database. Testcontainers.MongoDb to install the pre-defined Testcontainer for MongoDB. The full csproj file looks like this <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <RootNamespace /> </PropertyGroup> <ItemGroup> <PackageReference Include="Testcontainers.MongoDB" Version="3.5.0" /> <PackageReference Include="MongoDB.EntityFrameworkCore" Version="7.0.0-preview.1" /> </ItemGroup> </Project> Remember, the full project is available to download from GitHUb. The object model We’ll map a simple object model of customers and their addresses public class Customer { public Guid Id { get; set; } public required string Name { get; set; } public required Species Species { get; set; } public required ContactInfo ContactInfo { get; set; } } public class ContactInfo { public required Address ShippingAddress { get; set; } public Address? BillingAddress { get; set; } public required PhoneNumbers Phones { get; set; } } public class PhoneNumbers { public PhoneNumber? HomePhone { get; set; } public PhoneNumber? WorkPhone { get; set; } public PhoneNumber? MobilePhone { get; set; } } public class PhoneNumber { public required int CountryCode { get; set; } public required string Number { get; set; } } public class Address { public required string Line1 { get; set; } public string? Line2 { get; set; } public string? Line3 { get; set; } public required string City { get; set; } public required string Country { get; set; } public required string PostalCode { get; set; } } public enum Species { Human, Dog, Cat } Since MongoDB works with documents, we’re going to map this model to a top level Customer document, with the addresses and phone numbers embedded in this document. We’ll see how to do this in the next section. Creating the EF model EF works by building a model of the mapped CLR types, such as those for Customer, etc. in the previous section. This model defines the relationships between types in the model, as well as how each type maps to the database. Luckily there is not much to do here, since EF uses a set of model building conventions that generate a model based on input from both the model types and the database provider. This means that for relational databases, each type gets mapped to a different table by convention. For document databases like Azure CosmosDB and now MongoDB, only the top-level type (Customer in our example) is mapped to its own document. Other types referenced from the top-level types are, by-convention, included in the main document. This means that the only thing EF needs to know to build a model is the top-level type, and that the MongoDB provider should be used. We do this by defining a type that extends from DbContext. For example public class CustomersContext DbContext { private readonly MongoClient _client; public CustomersContext(MongoClient client) { _client = client; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseMongoDB(_client, "efsample"); public DbSet<Customer> Customers => Set<Customer>(); } In this DbContext class UseMongoDB is called, passing in the client driver and the database name. This tells EF Core to use the MongoDB provider when building the model and accessing the database. A DbSet<Customer> property that defines the top-level type for which documents should be modeled. We’ll see later how to create the MongoClient instance and use the DbContext. When we do, examining the model DebugView shows this Model EntityType ContactInfo Owned Properties CustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow Navigations BillingAddress (Address) ToDependent ContactInfo.BillingAddress#Address (Address) Phones (PhoneNumbers) ToDependent PhoneNumbers ShippingAddress (Address) ToDependent ContactInfo.ShippingAddress#Address (Address) Keys CustomerId PK Foreign keys ContactInfo {'CustomerId'} -> Customer {'Id'} Unique Ownership ToDependent ContactInfo Cascade EntityType ContactInfo.BillingAddress#Address (Address) CLR Type Address Owned Properties ContactInfoCustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow City (string) Required Country (string) Required Line1 (string) Required Line2 (string) Line3 (string) PostalCode (string) Required Keys ContactInfoCustomerId PK Foreign keys ContactInfo.BillingAddress#Address (Address) {'ContactInfoCustomerId'} -> ContactInfo {'CustomerId'} Unique Ownership ToDependent BillingAddress Cascade EntityType ContactInfo.ShippingAddress#Address (Address) CLR Type Address Owned Properties ContactInfoCustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow City (string) Required Country (string) Required Line1 (string) Required Line2 (string) Line3 (string) PostalCode (string) Required Keys ContactInfoCustomerId PK Foreign keys ContactInfo.ShippingAddress#Address (Address) {'ContactInfoCustomerId'} -> ContactInfo {'CustomerId'} Unique Ownership ToDependent ShippingAddress Cascade EntityType Customer Properties Id (Guid) Required PK AfterSaveThrow ValueGenerated.OnAdd Name (string) Required Species (Species) Required Navigations ContactInfo (ContactInfo) ToDependent ContactInfo Keys Id PK EntityType PhoneNumbers Owned Properties ContactInfoCustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow Navigations HomePhone (PhoneNumber) ToDependent PhoneNumbers.HomePhone#PhoneNumber (PhoneNumber) MobilePhone (PhoneNumber) ToDependent PhoneNumbers.MobilePhone#PhoneNumber (PhoneNumber) WorkPhone (PhoneNumber) ToDependent PhoneNumbers.WorkPhone#PhoneNumber (PhoneNumber) Keys ContactInfoCustomerId PK Foreign keys PhoneNumbers {'ContactInfoCustomerId'} -> ContactInfo {'CustomerId'} Unique Ownership ToDependent Phones Cascade EntityType PhoneNumbers.HomePhone#PhoneNumber (PhoneNumber) CLR Type PhoneNumber Owned Properties PhoneNumbersContactInfoCustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow CountryCode (int) Required Number (string) Required Keys PhoneNumbersContactInfoCustomerId PK Foreign keys PhoneNumbers.HomePhone#PhoneNumber (PhoneNumber) {'PhoneNumbersContactInfoCustomerId'} -> PhoneNumbers {'ContactInfoCustomerId'} Unique Ownership ToDependent HomePhone Cascade EntityType PhoneNumbers.MobilePhone#PhoneNumber (PhoneNumber) CLR Type PhoneNumber Owned Properties PhoneNumbersContactInfoCustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow CountryCode (int) Required Number (string) Required Keys PhoneNumbersContactInfoCustomerId PK Foreign keys PhoneNumbers.MobilePhone#PhoneNumber (PhoneNumber) {'PhoneNumbersContactInfoCustomerId'} -> PhoneNumbers {'ContactInfoCustomerId'} Unique Ownership ToDependent MobilePhone Cascade EntityType PhoneNumbers.WorkPhone#PhoneNumber (PhoneNumber) CLR Type PhoneNumber Owned Properties PhoneNumbersContactInfoCustomerId (no field, Guid) Shadow Required PK FK AfterSaveThrow CountryCode (int) Required Number (string) Required Keys PhoneNumbersContactInfoCustomerId PK Foreign keys PhoneNumbers.WorkPhone#PhoneNumber (PhoneNumber) {'PhoneNumbersContactInfoCustomerId'} -> PhoneNumbers {'ContactInfoCustomerId'} Unique Ownership ToDependent WorkPhone Cascade Looking at this model, it can be seen that EF created owned entity types for the ContactInfo, Address, PhoneNumber and PhoneNumbers types, even though only the Customer type was referenced directly from the DbContext. These other types were discovered and configured by the model-building conventions. Create the MongoDB test container We now have a model and a DbContext. Next we need an actual MongoDB database, and this is where Testcontainers come in. There are Testcontainers available for many different types of database, and they all work in a very similar way. That is, a container is created using the appropriate DbBuilder, and then that container is started. For example await using var mongoContainer = new MongoDbBuilder() .WithImage("mongo6.0") .Build(); await mongoContainer.StartAsync(); And that’s it! We now have a configured, clean MongoDB instance running locally with which we can do what we wish, before just throwing it away. Save data to MongoDB Let’s use EF Core to write some data to the MongoDB database. To do this, we’ll need to create a DbContext instance, and for this we need a MongoClient instance from the underlying MongoDB driver. Often, in a real app, the MongoClient instance and the DbContext instance will be obtained using dependency injection. For the sake of simplicity, we’ll just new them up here var mongoClient = new MongoClient(mongoContainer.GetConnectionString()); await using (var context = new CustomersContext(mongoClient)) { // ... } Notice that the Testcontainer instance provides the connection string we need to connect to our MongoDB test database. To save a new Customer document, we’ll use Add to start tracking the document, and then call SaveChangesAsync to insert it into the database. await using (var context = new CustomersContext(mongoClient)) { var customer = new Customer { Name = "Willow", Species = Species.Dog, ContactInfo = new() { ShippingAddress = new() { Line1 = "Barking Gate", Line2 = "Chalk Road", City = "Walpole St Peter", Country = "UK", PostalCode = "PE14 7QQ" }, BillingAddress = new() { Line1 = "15a Main St", City = "Ailsworth", Country = "UK", PostalCode = "PE5 7AF" }, Phones = new() { HomePhone = new() { CountryCode = 44, Number = "7877 555 555" }, MobilePhone = new() { CountryCode = 1, Number = "(555) 2345-678" }, WorkPhone = new() { CountryCode = 1, Number = "(555) 2345-678" } } } }; context.Add(customer); await context.SaveChangesAsync(); } If we look at the JSON (actually, BSON, which is a more efficient binary representation for JSON documents) document created in the database, we can see it contains nested documents for all the contact information. This is different from what EF Core would do for a relational database, where each type would have been mapped to its own top-level table. { "_id" "CSUUID(\"9a97fd67-515f-4586-a024-cf82336fc64f\")", "Name" "Willow", "Species" 1, "ContactInfo" { "BillingAddress" { "City" "Ailsworth", "Country" "UK", "Line1" "15a Main St", "Line2" null, "Line3" null, "PostalCode" "PE5 7AF" }, "Phones" { "HomePhone" { "CountryCode" 44, "Number" "7877 555 555" }, "MobilePhone" { "CountryCode" 1, "Number" "(555) 2345-678" }, "WorkPhone" { "CountryCode" 1, "Number" "(555) 2345-678" } }, "ShippingAddress" { "City" "Walpole St Peter", "Country" "UK", "Line1" "Barking Gate", "Line2" "Chalk Road", "Line3" null, "PostalCode" "PE14 7QQ" } } } Using LINQ queries EF Core supports LINQ for querying data. For example, to query a single customer using (var context = new CustomersContext(mongoClient)) { var customer = await context.Customers.SingleAsync(c => c.Name == "Willow"); var address = customer.ContactInfo.ShippingAddress; var mobile = customer.ContactInfo.Phones.MobilePhone; Console.WriteLine($"{customer.Id} {customer.Name}"); Console.WriteLine($" Shipping to {address.City}, {address.Country} (+{mobile.CountryCode} {mobile.Number})"); } Running this code results in the following output 336d4936-d048-469e-84c8-d5ebc17754ff Willow Shipping to Walpole St Peter, UK (+1 (555) 2345-678) Notice that the query pulled back the entire document, not just the Customer object, so we are able to access and print out the customer’s contact info without going back to the database. Other LINQ operators can be used to perform filtering, etc. For example, to bring back all customers where the Species is Dog var customers = await context.Customers .Where(e => e.Species == Species.Dog) .ToListAsync(); Updating a document By default, EF tracks the object graphs returned from queries. Then, when SaveChanges or SaveChangesAsync is called, EF detects any changes that have been made to the document and sends an update to MongoDB to update that document. For example using (var context = new CustomersContext(mongoClient)) { var baxter = (await context.Customers.FindAsync(baxterId))!; baxter.ContactInfo.ShippingAddress = new() { Line1 = "Via Giovanni Miani", City = "Rome", Country = "IT", PostalCode = "00154" }; await context.SaveChangesAsync(); } In this case, we’re using FindAsync to query a customer by primary key–a LINQ query would work just as well. After that, we change the shipping address to Rome, and call SaveChangesAsync. EF detects that only the shipping address for a single document has been changed, and so sends a partial update to patch the updated address into the document stored in the MongoDB database. Going forward So far, the MongoDB provider for EF Core is only in its first preview. Full CRUD (creating, reading, updating, and deleting documents) is supported by this preview, but there are some limitations. See the readme on GitHub for more information, and for places to ask questions and file bugs. Learn more To learn more about EF Core and MongoDB See the EF Core documentation to learn more about using EF Core to access all kinds of databases. See the MongoDB documentation to learn more about using MongoDB from any platform. Watch Introducing the MongoDB provider for EF Core on the .NET Data Community Standup. Watch the upcoming Announcing MongoDB Provider for Entity Framework Core on the MongoDB livestream. Summary We used Testcontainers to try out the first preview release of the MongoDB provider for EF Core. Testcontainers allowed us to test MongoDB with very minimal setup, and we were able to create, query, and update documents in the MongoDB database using EF Core. The post Trying out MongoDB with EF Core using Testcontainers appeared first on .NET Blog.


What is New in Entity Framework 5 Shipped in Asp.N ...
Category: .Net 7

Entity Framework 5 New Stuff You can Log Messages using a short ...


Views: 274 Likes: 78
InvalidOperationException: An exception was thrown ...
Category: Entity Framework

What is a LINQ Query?Linq which stands for Language Integrated Query ...


Views: 0 Likes: 69
Invalid column name '*_ID'
Category: Entity Framework

Question Entity Framework Code First Error "Invalid Column 'ColumnName". I am c ...


Views: 0 Likes: 31
What is New in Asp Net 7 and EF Core 7 (Best Featu ...
Category: Research

Asp.Net Core 7Navigation If you want the Entity to be included in a ...


Views: 0 Likes: 19
An error was generated for warning 'Microsoft.Enti ...
Category: Entity Framework

Question What does this error mean? "InvalidOperationEx ...


Views: 0 Likes: 33
_context.SaveChanges is not working
Category: Entity Framework

Question Why is _context.SaveChanges() not working in Entity F ...


Views: 0 Likes: 60
Asp.Net Core EF Core Error: Cannot access a dispos ...
Category: Entity Framework

Error Cannot access a disposed of object. ...


Views: 3944 Likes: 102
Asp.Net MVC Development Notes
Category: .Net 7

<a href="https//www.freecodecamp.org/news/an-awesome-guide-on-how-to-build-restful-apis-w ...


Views: 752 Likes: 79
Sql Server Error, Entity Framework Core Error: An ...
Category: SQL

Question <span style="font-family arial, helv ...


Views: 852 Likes: 109
Asp.Net 5 Development Notes (DotNet Core 3.1 Study ...
Category: Software Development

Study Notes to use when progra ...


Views: 423 Likes: 61
[EF Core] How to Enable Sensitive Data Logging and ...
Category: Entity Framework

Question How do you enable sensitive data and detailed error <a class="text-decoration-none" hre ...


Views: 0 Likes: 33
InvalidOperationException: The required column 'Ca ...
Category: Entity Framework

Question I can't see a Navigation Property called Categoryid anywhere in the Class Modal, where ...


Views: 0 Likes: 10
HTTP Error 502.5 - Process Failure: The current .N ...
Category: Network

Problem The current .Net SDK does not support targeting .NET Core 3.0. Either target .NET Core 2 ...


Views: 845 Likes: 108
SqlException: Cannot insert the value NULL into co ...
Category: Entity Framework

Question Why is Entity <a class="text-decoration-none" href="https//www.ernest ...


Views: 0 Likes: 49
HTTP Error 500.19 Internal server error
Category: Servers

Question How do you solve for e ...


Views: 0 Likes: 41
InvalidOperationException, A second operation star ...
Category: Entity Framework

Problem InvalidOperationException A s ...


Views: 886 Likes: 102
SqlException: A connection was successfully establ ...
Category: .Net 7

Question How do you solve the error that says "SqlException A connection was ...


Views: 0 Likes: 31
Docker on Raspberry Pi Error .Net Core 3.1 Error: ...
Category: Docker

How do you resolve this error Docker on Raspberry Pi Error .Net Core 3.1 Error ...


Views: 924 Likes: 119
Entity Framework Core Error: To change the IDENTIT ...
Category: Entity Framework

Question How do you resolve the Entity ...


Views: 2404 Likes: 105
How to Solve XUnit Automated Testing Error: Castle ...
Category: MVC

How to Solve the XUnity Automated Test Error</s ...


Views: 1715 Likes: 114
System.InvalidOperationException: The ConnectionSt ...
Category: Entity Framework

Question How do you resolve the error that says The Connection property has not been initialize ...


Views: 0 Likes: 31
Upgrading to .Net Core 3.1 Error Visual Studio 201 ...
Category: .Net 7

Problem When upgrading to Dot Net Core 3.1 from .Net Core 3.0 ...


Views: 671 Likes: 106
[Solved] Entity Framework Core (EF 6) Error: Unabl ...
Category: Entity Framework

Question How do you resolve Unable to track an entity of type 'YourModalName' ...


Views: 593 Likes: 91
The expression 'b.listOfFileRecords' is invalid in ...
Category: Entity Framework

Question System.InvalidOperationException 'The expression 'b.listOfFileRecords ...


Views: 0 Likes: 42
Microsoft.EntityFrameworkCore.Database.Transaction ...
Category: Entity Framework

What is Entity Framework and what is it used for?Entity Framework is an ORM meaning i ...


Views: 2822 Likes: 114
System.ObjectDisposedException: Cannot access a di ...
Category: Other

Question How do you solve "System.ObjectDisposedException Cannot access a disposed context ins ...


Views: 177 Likes: 68
Return url is required
Category: .Net 7

Question There is this annoying error in Asp.Net Core MVC that says Return Url is required. How ...


Views: 0 Likes: 58
Asp.Net Core Error: InvalidOperationException: The ...
Category: .Net 7

Problem&nbsp;<span style="background-color #fff ...


Views: 341 Likes: 96
How to Select Top Distinct Values from a LINQ Grou ...
Category: Entity Framework

Question How do I select the top distinct values from a <a class="text-decoration-none" href="ht ...


Views: 0 Likes: 35
Entity framework "InvalidCastException: Unable to ...
Category: .Net 7

Question I have spend so much time on this error but I don't see where the issu ...


Views: 171 Likes: 69
Microsoft Data SqlClient SqlException (0x80131904) ...
Category: Entity Framework

Question How do you resolve the error in C# Asp.Net 6 that says "<span style="background-color ...


Views: 0 Likes: 61
Error The instance of entity type ModalType cannot ...
Category: .Net 7

Question How do you solve the Entity Framework Error that says "Error The inst ...


Views: 372 Likes: 66
Entity Framework Core 6 and 7 Tips and Tricks
Category: Entity Framework

Interceptors in EF Core 7- Use the TPC (Table per Concrete) Mapping strategy (prefer ...


Views: 0 Likes: 29
Performance Tuning in Entity Framework Core 5 and ...
Category: .Net 7

[Update] 6/16/20221. The Introductio ...


Views: 333 Likes: 87
How to get the last record of Entity Framework mod ...
Category: .Net 7

Question How do you get a recen ...


Views: 10 Likes: 61
Cassandra Entity Framework Error "TypeLoadExceptio ...
Category: Questions

Question Why is this error happening? "TypeLoadException Method 'GenerateScript' in type 'Micro ...


Views: 0 Likes: 55
Performance Tuning for ASP.NET Web Applications
Category: .Net 7

Performance Tuning in ASP.NET Core with C# ...


Views: 398 Likes: 109
An Exception was thrown while attempting to evalua ...
Category: .Net 7

Question Why is this error happening, ...


Views: 0 Likes: 41
Software Best Practices Learned by Experience
Category: System Design

[Updated] It is considered good practice to cache your data in memory, either o ...


Views: 0 Likes: 38
How to Use DisplayFor to Display Text from EF Core ...
Category: .Net 7

Question <a class="text-decoration-none" href="https//www.ernestech.com/articl ...


Views: 0 Likes: 33
The configured execution strategy 'SlqServerRetryi ...
Category: .Net 7

Question I have this error come up in Asp.Net 5 Production environment hosted i ...


Views: 0 Likes: 57
EF Core 6 Error: Error Number:262,State:1,Class:14 ...
Category: Entity Framework

Question How do you resolve EF Core 6 Error Error Number262,State1,Class14 ...


Views: 314 Likes: 96
The instance of entity type 'IdentityUserLogin<str ...
Category: .Net 7

Questions How do you solve The instan ...


Views: 455 Likes: 68

Login to Continue, We will bring you back to this content 0



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