标签: entity-framework-core-5

Entity Framework Core 多对多更改导航属性名称

我有一个名为“LogBookSystemUsers”的表,我想在 EF Core 5 中设置多对多的功能。我几乎可以正常工作,但问题是我的 ID 列已命名SystemUserIdLogBookId但是当 EF 进行连接时,它会尝试使用SystemUserIDLogBookID。这是我当前的配置代码:

modelBuilder.Entity<SystemUser>()
            .HasMany(x => x.LogBooks)
            .WithMany(x => x.SystemUsers)
            .UsingEntity(x =>
            {
                x.ToTable("LogBookSystemUsers", "LogBooks");
            });
Run Code Online (Sandbox Code Playgroud)

我试过这个:

modelBuilder.Entity<SystemUser>()
            .HasMany(x => x.LogBooks)
            .WithMany(x => x.SystemUsers)
            .UsingEntity<Dictionary<string, object>>("LogBookSystemUsers",
                x => x.HasOne<LogBook>().WithMany().HasForeignKey("LogBookId"),
                x => x.HasOne<SystemUser>().WithMany().HasForeignKey("SystemUserId"),
                x => x.ToTable("LogBookSystemUsers", "LogBooks"));
Run Code Online (Sandbox Code Playgroud)

但这只是添加了两个新列,而不是设置当前列的名称。

这是所有数据库第一。我不想为多对多表使用一个类,因为我在我的项目中一直这样做,我不希望一堆无用的类四处飘散。有任何想法吗?

c# postgresql entity-framework-core entity-framework-core-5

8
推荐指数
1
解决办法
1685
查看次数

如何防止EF core 5在保存时创建保存点

我们在始终在外部管理事务的上下文中使用 EF Core。我们还必须使用 MARS。由于我们已升级到 EF Core 5,此组合会导致以下警告:

Microsoft.EntityFrameworkCore.Database.Transaction - Savepoints are disabled because Multiple Active 
Result Sets (MARS) is enabled. If 'SaveChanges' fails, then the transaction cannot be automatically 
rolled back to a known clean state. Instead, the transaction should be rolled back by the application 
before retrying 'SaveChanges'. See https://go.microsoft.com/fwlink/?linkid=2149338 for more information. 
To identify the code which triggers this warning, call 'ConfigureWarnings(w => 
w.Throw(RelationalEventId.SavepointsDisabledBecauseOfMARS))'.
Run Code Online (Sandbox Code Playgroud)

我对禁用保存点很好(至少现在是这样),但我对每个数据库保存的日志消息不太满意......

有没有办法告诉 EF Core 不使用这个新功能?

entity-framework-core-5

3
推荐指数
2
解决办法
784
查看次数

带有 Entity Framework Core 5.0 的 UWP

大家好,我想问一下我是否真的可以在 UWP 上以某种方式使用 Entity Framework Core 5.0。我正在尝试从 EF Core 从 2.1 升级到 5.0

我有以下设置:

  1. “Core.proj” - .NET Standard 2.0 类库- (这是我的 DbContext 以及所有关于 EF Core 2.1 的内容)
  2. “Win10.proj” - UWP 项目

在“Win10”中,我引用了“Core”,但为了使用 Entity Framework Core 5.0,我必须将 .NET Standard 从 2.0 升级到 2.1,但不支持 .NET Standard 2.1 用于 UWP。

我试图将“Core.proj”更改为 - .NET 5.0 类库,但随后我无法从“Win10.proj”中引用它。

entity-framework-core uwp entity-framework-core-5

3
推荐指数
1
解决办法
295
查看次数

使用 fluent api EF Core 5 的多对多关系

我想我有两个具有多对多关系的实体,我将使用 fluent api 来解决这种关系

public class Author
{
    public int AuthorId { get; set; }
    public string Name { get; set; }
    public ICollection<Book> Books { get; set; }
}

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public ICollection<Author> Authors { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{  
   //Book
    modelBuilder.Entity<Book>().HasKey(x => x.BookId);
    modelBuilder.Entity<Book>().Property(x => x.Title).IsRequired();

    //Author
    modelBuilder.Entity<Author>().HasKey(x => x.AuthorId);
    modelBuilder.Entity<Author>().Property(x => x.Name).IsRequired();

    //many to many relationship …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core entity-framework-core-5

2
推荐指数
1
解决办法
1548
查看次数

EF Core 5.0 - 更新 ASP.NET Core Web API 中的多对多实体

EF Core 5.0 引入了多对多关系。我对如何通过我的 asp .net api 更新它们感到困惑。

对于一对一和一对多关系,有一个约定,即简单地添加属性名称后跟 ID。

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

    public BlogImage BlogImage { get; set; }
}

public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

所以一个合适的 POST 请求可能看起来像

{ …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework-core entity-framework-core-5

2
推荐指数
1
解决办法
516
查看次数