标签: entity-framework-migrations

Code First - 两个外键为主键,无法添加迁移

我的用户表:

public class User
    {
        [Key]
        public int UserId { get; set; }

        public virtual ICollection<PollVote> PollVotes { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我的投票表:

public class Poll
    {
        [Key]
        public int PollId { get; set; }

        public virtual ICollection<PollVote> PollVotes { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我的投票表

public class PollVote
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]        
        public int VoteId { get; set; }

        [Key]
        public int PollId { get; set; }

        [Key]
        public int UserId { get; set; }

        public DateTime TimeVoted { get; set; } …
Run Code Online (Sandbox Code Playgroud)

entity-framework ef-code-first entity-framework-migrations

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

获取当前登录的IdentityUser?

我正在尝试创建一个使用 Windows Identity 和 Entity Framework Code First 的应用程序。所以我正在创建一个自定义类来保存一些数据。

public class Order {
  public string Name { get; set; }
  public DateTime CreatedDate { get; set; }
  public IdentityUser User { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在我尝试将用户表链接到订单表(由实体框架生成)。这样我就可以执行 Order.User 来获取创建订单的用户的用户信息。

我想使用创建一个新订单

new Order {
  Name = "Test",
  CreatedDate = new DateTime(2014, 3, 30),
  User = <What goes here?>
}
Run Code Online (Sandbox Code Playgroud)

现在如何获取当前登录用户的“IdentityUser”?或者我以错误的方式处理这个问题?

entity-framework asp.net-identity entity-framework-migrations

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

处理代码优先迁移中的错误

我的 Up() 中有以下内容

public override void Up()
{      
    try
    {
        Sql("drop index [IX_SalesInvoiceLine] on dbo.SalesInvoiceLines"); 
    }
    catch (Exception)
    {
        // ignored
    }
    string sql = @"CREATE UNIQUE NONCLUSTERED INDEX [IX_SalesInvoiceLine] ON [dbo].[SalesInvoiceLines] ( [SalesInvoice_Id] ASC, [seqNo] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]";
    Sql(sql);
 }
Run Code Online (Sandbox Code Playgroud)

当我在调试器中单步执行代码时,我可以单步执行每一行。

但是,当我运行迁移时,我收到一条消息

"Cannot drop the index'dbo.SalesInvoiceLines_IXSalesInvoiceLine' , 
because it does not exist or you …
Run Code Online (Sandbox Code Playgroud)

entity-framework-migrations

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

包管理器控制台添加迁移命令不起作用

首先,与此问题相关的所有其他问题都是针对 ASP.NET Core 项目的,解决方案是将 Microsoft.EntityFrameworkCore.Design 添加到 project.json 上的工具部分,但是......我没有使用 ASP .NET Core,所以我的项目中没有project.json。因此,在将其标记为重复之前,请记住它,因为我看到这是一个相当常见的问题......

当我add-migration InitialMigration在包管理器控制台上键入时,出现以下错误:

无法执行此命令,因为未安装 Microsoft.EntityFrameworkCore.Design。安装与已安装的 Microsoft.EntityFrameworkCore 版本匹配的包版本,然后重试。

但正如您在下图中看到的,它安装在这个项目上:

在此输入图像描述

我的数据库层位于一个单独的项目中,即完整框架类库,因此我没有 projec.json 文件。

有人尝试在类库项目上使用迁移吗?

entity-framework-core entity-framework-migrations

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

添加迁移 - Entity Framework Core 和 Entity Framework 6 均已安装

我有一个包含多个项目的解决方案,其中一个使用 EF 6,另一个使用 entityframeworkcore。

在添加 EF6 项目之前,迁移工作正常,但现在我无法使用迁移命令: add-migration 'anything'

Entity Framework Core 和 Entity Framework 6 都已安装。Entity Framework Core 工具正在运行。对实体框架 6 使用“EntityFramework\Add-Migration”。

对于带有 EF6 的项目,我可以使用以下方式添加迁移: EntityFramework\Add-Migration 'anthing_here',但我无法使用这种方式向带有 EFCore 的项目添加迁移。

有什么建议 ??

c# entity-framework entity-framework-core visual-studio-2017 entity-framework-migrations

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

如何卸载 EF Core 表?或者如何删除所有迁移?或者如何从用户代码调用`dotnet ef database update 0`?

我正在开发 Web 应用程序并在它附近安装 ef 核心表在数据库中的小型命令行应用程序。最后可以完成调用dbContext.Database.Migrate();,这有效。

现在我想提供unistall选项(使用此应用程序)。

但是如何删除迁移(意味着dotnet ef database update 0从我的代码中调用功能)?

它可能不是一个命令调用(就像dbContext.Database.Migrate();)。但是片段循环遍历迁移程序集中的所有迁移并调用“Downs”。

c# .net-core entity-framework-core-migrations entity-framework-core-2.1 entity-framework-migrations

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

EF Core 一对多关系列表返回 null

我正在尝试学习如何在 EF Core 中正确使用 DbContext。我有一个团队课程:

public class Team 
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool CanSelfManage { get; set; } = false;
    public virtual List<Mileage> Mileages { get; set; }
    public IdentityUser Member { get; set; }
    public string State { get; set; }
    public List<Horse> Horses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有一个里程类:

public class Mileage
{
    public int ID { get; set; }
    public virtual Team Team { get; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first entity-framework-migrations

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

EF Core - 唯一约束,包括迁移中未检测到的导航属性

我正在尝试应用唯一的复合约束,约束的一部分是外键。我似乎让它工作的唯一方法是在我的域类中显式定义外键,这是我想避免的。这可能吗?

该问题和解决方法适用于HasAlternateKeyHasIndex。该解决方案构建良好,但在创建迁移时会忽略约束,直到影子属性转换为域类中的真实属性。

这不起作用(迁移会忽略这一点):

entity.HasAlternateKey(e => new { e.Header.Id, e.Version, e.StartDate });
Run Code Online (Sandbox Code Playgroud)

将影子属性 HeaderID 转换为真实属性后,这确实有效:

entity.HasAlternateKey(e => new { e.HeaderId, e.Version, e.StartDate });
entity.HasOne(e => e.Header).WithMany().HasForeignKey(f => f.HeaderId);
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-migrations ef-core-2.2

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

EF Core 不断抱怨索引不存在

我在运行时收到来自 EF Core 的一堆日志抱怨,如下所示:

[10:56:14 DBG] The index {'InvoiceId'} was not created on entity type 'CompleteCase' as the properties are already covered by the index {'InvoiceId', 'Code'}.
[10:56:14 DBG] The index {'ReportPeriodId'} was not created on entity type 'FixedBill' as the properties are already covered by the index {'ReportPeriodId', 'MedCompanyTypeId', 'FixedBillTypeId'}.
[10:56:14 DBG] The index {'CasePeriodId'} was not created on entity type 'Invoice' as the properties are already covered by the index {'CasePeriodId', 'ReportPeriodId', 'MedCompanyTypeId'}.
[10:56:14 DBG] The index {'InvoiceId'} …
Run Code Online (Sandbox Code Playgroud)

sql-server foreign-keys relational-database entity-framework-core entity-framework-migrations

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

指定的 LocalDB 实例名称无效:能够创建迁移但无法运行它们

我创建了一个新的 XAF Blazor 解决方案并能够运行它来创建数据库。然后我根据这个问题添加了数据库迁移 但是当我从包管理器中运行迁移时我得到

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SNI_PN11, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.
)
 ---> System.ComponentModel.Win32Exception (0x89C5011B): Unknown error (0x89c5011b)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject …
Run Code Online (Sandbox Code Playgroud)

resharper xaf visual-studio entity-framework-core entity-framework-migrations

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