我有多个上下文,其中之一有效,但其他三个找不到。
\n\n我在用着PM> Add-Migration InitialCreateLdapIdentity -context LdapIdentityDbContext<LdapUser, LdapUserRole> -verbose
这是它的输出:
\n\nFinding DbContext classes...\nFinding IDesignTimeDbContextFactory implementations...\nFinding application service provider...\nFinding BuildWebHost method...\nUsing environment \'Development\'.\nUsing application service provider from BuildWebHost method on \'Program\'.\nMicrosoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]\n User profile is available. Using \'C:\\Users\\brech\\AppData\\Local\\ASP.NET\\DataProtection-Keys\' as key repository and Windows DPAPI to encrypt keys at rest.\nFound DbContext \'ApplicationIdentityDbContext<ApplicationUser, ApplicationUserRole>\'.\nFound DbContext \'LdapIdentityDbContext<LdapUser, LdapUserRole>\'.\nFound DbContext \'BlogIdentityDbContext<BlogUser, BlogUserRole>\'.\nFound DbContext \'CountriesDbContext\'.\nFinding DbContext classes in the project...\nMicrosoft.EntityFrameworkCore.Design.OperationException: No DbContext named \'LdapIdentityDbContext<LdapUser, LdapUserRole>\' was found.\n
Run Code Online (Sandbox Code Playgroud)\n\n有趣的是,当我跑步Add-Migration InitialCreateCountries -context CountriesDbContext
时,这就起作用了。 …
我正在尝试LinqKit
AsExpandable
在我的EfCore2.0
项目中使用,但遇到了Includes
不起作用的问题。
在尝试调试时,我LinqKit
从 github下载了源代码,并Nuget
用项目引用替换了我项目中的引用。
在调试LinqKit
项目时,我注意到我的调用Include
没有达到我在 上设置的断点ExpandableQueryOfClass<T>.Include
。
我做了一些进一步的测试并注意到如果我第一次转换到断点会被击中ExpandableQueryOfClass
(这是一个LinqKit
我公开的内部类,所以如果我引用Nuget
包,我就不能做转换)。
这是一个错误LinqKit
还是我做错了什么?
这是我的测试代码。
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Internal.DAL.Db;
using Internal.Models.Customer;
using LinqKit; // Referencing LinqKit.Microsoft.EntityFrameworkCore
using Xunit;
namespace Internal.EntityFramework.Tests
{
public class UnitTest1
{
private DbContextOptionsBuilder<DataContext> _ctxBuilder =>
new DbContextOptionsBuilder<DataContext>().UseSqlServer(Connection.String);
[Fact]
public async Task SuccessTest()
{
using (var ctx = new DataContext(_ctxBuilder.Options))
{
var query …
Run Code Online (Sandbox Code Playgroud) 我必须从未安装 Visual Studio 的 SQL Server 2016 的生产实例恢复上次 EF Core 迁移。这个怎么做?
注意:我已经尝试在服务器上安装.NET SDK,但它不知道该命令dotnet ef
。执行命令的结果是
No executable found matching command "dotnet-ef"
如何在 EF Core 2 中的自引用表中实现级联删除(代码优先)?
(例如,有一个评论表,一个人可以回复一条评论,这个回复可以由另一个人回复。)
public class Comment
{
public virtual int Id { get; set; }
public virtual int? ParentId { get; set; }
public Comment Parent { get; set; }
public virtual IList<Comment> Replies { get; set; }
public virtual string Description { get; set; }
public virtual Article Article { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
entity-framework cascade entity-framework-core ef-core-2.0 entity-framework-migrations
出于某种原因,我收到此错误。以下是规格,然后是我所做的。
Visual Studio 2017 v15.6.2
SQL Server 2008 v10.50.4
我打开了包管理器控制台并添加了以下包...
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Run Code Online (Sandbox Code Playgroud)
然后我跑了
Scaffold-DbContext "Server=ph2srv76;Database=AVDRS;Integrated Security=True;Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force
Run Code Online (Sandbox Code Playgroud)
但是在构建过程中我收到以下错误...
我使用实体框架核心 2.0.1。在我的 EF 查询中,我需要将几个简单的查询组合成一个 UNION 查询。所以,对于一个非常基本的例子,我有几个类似的查询
using (var dbc = new ItemsDbContext("some-connection-string"))
{
var q1 = dbc.Items.Where(a => a.ItemType == "A");
var q2 = dbc.Items.Where(a => a.ItemType == "B");
var myList = q1.Union(q2).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我希望将这样的 SQL 语句作为单个查询对我的 SQL Server 数据库执行:
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType = N'A'
UNION
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType = N'B'
Run Code Online (Sandbox Code Playgroud)
但是,正如我在 SQL Server Profiler 中看到的那样,执行了两个单独的查询:
查询 1:
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType = N'A'
Run Code Online (Sandbox Code Playgroud)
查询 2:
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType …
Run Code Online (Sandbox Code Playgroud) 我有一个实体User
,它有两个属性CreatedBy
并且UpdatedBy
都引用User
. 默认情况下,EF 假定这两者是一对一的关系。我收到以下错误消息:
消息:System.InvalidOperationException:无法确定在“User.CreatedBy”和“User.UpdatedBy”之间检测到的一对一关系的子/从属端。要识别关系的子/依赖方,请配置外键属性。如果这些导航不应该是同一关系的一部分,请在不指定反向的情况下配置它们。有关更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=724062。
目前,我有一个这样的课程:
public class User
{
public int Id { get; set; }
public int? CreatedById { get; set; }
public User CreatedBy { get; set; }
public int? UpdatedById { get; set; }
public User UpdatedBy { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基本上,这就是我正在尝试的:
CreatedBy
,任意数量的用户都可以拥有相同的UpdatedBy
。我怎样才能让 EF 忽略导航属性?我拥有的主要原因CreatedBy
是我Include(u => u.CreatedBy)
以后可以使用。我知道使用IEnumerable<User> AllCreatedUsers
属性可以解决这个问题,但我不想为IEnumerable
我的实体中的每个创建一个。有没有办法用流畅的 API …
c# navigation-properties entity-framework-core ef-fluent-api ef-core-2.0
这个问题已被问过几次,但没有一个解决方案/文档对我有用。
场景 - 我需要从头开始构建一个数据库,并使用所有代码迁移来编辑它,根据文档,下面的行应该从 DbContext/ModelBuilder 创建数据库。
_dbContext.Database.Migrate();
Run Code Online (Sandbox Code Playgroud)
然而,这只是创建数据库本身而不是任何表。如果我运行下面的命令,
_dbContext.Database.EnsureCreated();
Run Code Online (Sandbox Code Playgroud)
数据库和所有表都已创建,但这不是一个选项,因为我需要进一步编辑迁移。我在 Migrate 行之后直接尝试了下面的代码,但 pendingMigrations 始终为 0。
var pendingMigrations = _dbContext.Database.GetAppliedMigrations().ToList();
if (pendingMigrations.Any())
{
var migrator = _dbContext.Database.GetService<IMigrator>();
foreach (var targetMigration in pendingMigrations)
migrator.Migrate(targetMigration);
}
Run Code Online (Sandbox Code Playgroud)
有什么我在这里想念的吗?Database.Migrate() 不应该同时创建表和数据库吗?
注意 - 在测试中,我在尝试另一种方法之前删除了数据库。
在实体框架中使用时间戳列时,它由rowversion
SQL Server 中的列类型支持并表示为 CLR byte[]
(根据文档)。该列的长度为 8 个字节。
为什么他们决定使用byte[]
而不是UInt64
?它会很好地保持价值。使用 byte[] 是否有任何不明显的好处,或者只是将 EF 与其他数据库引擎一起使用,这些引擎可以在内部将类似 rowversion 的列实现为不同的数据类型。
我试图使用vscode在ubuntu上运行和ASPNET核心应用程序。当运行应用程序时,它给我这个错误:
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at …
Run Code Online (Sandbox Code Playgroud) ef-core-2.0 ×10
c# ×3
sql-server ×3
asp.net-core ×1
cascade ×1
entity-framework-migrations ×1
linqkit ×1