多对一迁移因外键长而失败

Dom*_*omi 5 c# entity-framework

我有 2 个模型:

public class Text
{
    public long Id { get; set; }
    public string Text { get; set; }
}

public class User
{
    public int Id { get; set; }
    public ICollection<Text> Texts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我建立在用户基础上的模型是

e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.Id).IsRequired();
Run Code Online (Sandbox Code Playgroud)

当我尝试运行时:

dotnet ef 迁移添加

我收到此错误:

带有外键属性 {'Id' : long} 不能定位主键 {'Id' : int} 因为它不兼容。为此关系配置一个主键或一组兼容的外键属性。

更新:

新模型应该能够拥有表格文本的集合,例如:

public class Customer
{
    public int Id { get; set; }
    public ICollection<Text> Texts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

....

e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.Id).IsRequired();
Run Code Online (Sandbox Code Playgroud)

Don*_*ong 9

首先,请更改您的型号命名,

public class Text
{
    public long Id { get; set; }
    public int UserId { get; set; }// add a foreign key that could point to User.Id
    public string Body { get; set; }//you cannot have a string property called "Text".
    public virtual User Owner { get; set; }
}
public class User
{
    public int Id { get; set; }
    public virtual ICollection<Text> Texts { get; set; } = new HashSet<Text>();
}

builder.Entity<Text>(table =>
        {
            table.HasKey(x => x.Id);

            table.HasOne(x => x.User)
            .WithMany(x => x.Texts)
            .HasForeignKey(x => x.UserId)
            .HasPrincipalKey(x => x.Id)//<<== here is core code to let foreign key userId point to User.Id.
            .OnDelete(DeleteBehavior.Cascade);
        });
Run Code Online (Sandbox Code Playgroud)

我们必须找出引用哪个键的原因是因为有多个主键。我在MSDN上看到过一次,但找不到了。

您可以对外键使用影子属性,现在看起来很流行。

public class Text
{
    public long Id { get; set; }
    public string Body { get; set; }
    public virtual User Owner { get; set; }
}
public class User
{
    public int Id { get; set; }
    public virtual ICollection<Text> Texts { get; set; } = new HashSet<Text>();
}

builder.Entity<Text>(table =>
        {
            table.HasKey(x => x.Id);

            // Add the shadow property to the model
            table.Property<int>("UserId");

            table.HasOne(x => x.User)
            .WithMany(x => x.Texts)
            .HasForeignKey("UserId")//<<== Use shadow property
            .HasPrincipalKey(x => x.Id)//<<==point to User.Id.
            .OnDelete(DeleteBehavior.Cascade);
        });
Run Code Online (Sandbox Code Playgroud)


Rig*_*iga 5

使用 EF Core 时遇到了类似的问题,但不想在依赖实体 Text 上包含(在我的类中等效)UserId,只是为了让 EF 开心。最后发现可以使用 HasPrincipalKey() 替换关系中使用的主键(UserId)

    modelBuilder.Entity<User>()
        .HasMany(t => t.Texts)
        .WithOne()
        .HasPrincipalKey(u => u.Text);
Run Code Online (Sandbox Code Playgroud)


val*_*orl 3

在 EF 上下文配置中,特别是在HasForeignKey()您应该指定模型上的哪个属性Text应该是指向模型的外键User

由于User模型的主键是 an ,因此从指向 的int外键自然也应该是 an 。TextUserint

我认为您犯的错误是您将 的 PK 配置为关系->Text的 FK 。尝试将您的模型更改为:TextUserText

public class Text
{
   public long Id { get; set; }
   public string Text{ get; set; }
   public int UserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

以及您的配置:

e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.UserId).IsRequired();
Run Code Online (Sandbox Code Playgroud)