gpt4 book ai didi

EF Core for CosmosDB entity and container mapping(用于CosmosDB实体和容器映射的EF Core)

转载 作者:bug小助手 更新时间:2023-10-28 20:36:51 24 4
gpt4 key购买 nike



I am working on a .NET Core 6 application Hot Chocolate GraphQL application, and connecting to Azure CosmosDB using Micorosft.EntityFrameworkCore.Cosmos.

我正在开发一个.NET Core 6应用程序Hot Chocolate GraphQL应用程序,并使用Micorosft.martyFrameworkCore.Cosmos连接到Azure CosmosDB。


My container name is named auditlog:

我的容器名称为auditlog:


enter image description here


I have defined an entity like this:

我已经定义了一个这样的实体:


enter image description here


And lastly, I defined my DataContext like this:

最后,我这样定义了我的DataContext:


enter image description here


However, for some reason I can't query AuditLog. ToContainer is not working. I need to change the entity class name to auditlog and that's the only time it will work.

但是,由于某些原因,我不能查询AuditLog。ToContainer不工作。我需要将实体类名称更改为auditlog,这是它唯一有效的时候。


Am I doing something wrong?

我是不是做错了什么?


更多回答

Please do not upload images of code/data/errors. It's really important to provide properly formatted text, for many reasons outlined in the meta post I linked to.

请不要上传代码/数据/错误的图像。提供格式正确的文本真的很重要,在我链接的元帖子中概述了许多原因。

优秀答案推荐


connecting to Azure CosmosDB using Micorosft.EntityFrameworkCore.Cosmos



Below are the steps I followed:

以下是我遵循的步骤:



  • The database context for an application is represented by EmployeeDbContext, which is derived from DbContext.

    应用程序的数据库上下文由DbContext表示,DbContext派生自DbContext。



  • The collection of Employee entities is denoted by DbSet<Employee> Employees"

    Employee实体的集合由DbSet Employees“表示



  • Set up the Cosmos DB connection in OnConfiguring() by providing the connection string and the database name.

    通过提供连接字符串和数据库名称,在OnConfiguring()中设置Cosmos DB连接。



  • The mapping between the Employee entity and the Employees Cosmos DB container is defined in OnModelCreating().

    Employee实体和Employees Cosmos DB容器之间的映射在OnModelCreating()中定义。



  • Additionally, OnModelCreating() states that the partition key relies on the id attribute.

    此外,OnModelCreating()声明分区键依赖于id属性。



  • Created an instance of EmployeeDbContext in the Main method.

    在Main方法中创建了EmployeeDbContext的实例。



  • dbContext.Database.EnsureCreated() checks to see if the database and container have been created.

    EnsureCreated()检查是否已经创建了数据库和容器。



  • To the Employees Db Set, create a new Employee object.

    对于Employees数据库集,创建一个新的Employee对象。



  • Use dbContext.SaveChanges() to save database changes.

    使用dbConext.SaveChanges()保存数据库更改。




Below Is the code I tried with:

下面是我尝试过的代码:


public class EmployeeDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = "*****";
var databaseName = "EmployeeDb";

optionsBuilder.UseCosmos(
connectionString,
databaseName);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().ToContainer("Employees").HasPartitionKey(e => e.id);
}
}

class Program
{
static void Main()
{
using var dbContext = new EmployeeDbContext();
try
{
dbContext.Database.EnsureCreated();

var newEmployee = new Employee
{
id = Guid.NewGuid(),
FirstName = "Pavan",
LastName = "Balaji",
DateOfBirth = new DateTime(1998, 7, 28)
};

dbContext.Employees.Add(newEmployee);
dbContext.SaveChanges();

Console.WriteLine("Employee added successfully.");

var employees = dbContext.Employees.ToList();
Console.WriteLine("List of Employees:");

foreach (var employee in employees)
{
Console.WriteLine($"ID: {employee.id}, Name: {employee.FirstName} {employee.LastName}, DOB: {employee.DateOfBirth}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");

if (ex.InnerException != null)
{
Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}
}
}
}

Output:
In Console:

输出:在控制台中:


Employee added successfully.
List of Employees:
ID: a18857c5-37f0-477a-be62-b64f3ad60039, Name: Pavan Balaji, DOB: 7/28/1998 12:00:00 AM

After running SELECT * FROM c:

运行SELECT*FROM c:


[
{
"id": "a18857c5-37f0-477a-be62-b64f3ad60039",
"DateOfBirth": "1998-07-28T00:00:00",
"Discriminator": "Employee",
"FirstName": "Pavan",
"LastName": "Balaji"
}
]

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com