2016-11-28 62 views
0

失敗,我有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 
} 

我的模型建立在用戶是

e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.Id).IsRequired(); 

當我嘗試運行:

DOTNET ef migrations add

我得到 - >

與外鍵的屬性{ 'ID':長}不能指定主 鍵{ 'ID':INT},因爲它是不兼容的。爲此 關係配置主體 密鑰或一組兼容外鍵屬性。

UPDATE:

它應該能夠爲新車型有像表文本的集合:

public class Customer 
{ 
    public int Id { get; set; } 
    public ICollection<Text>Texts { get; set; } 
} 

.... e.HasMany(O => o.Texts).WithOne()。HasForeignKey(d => d.Id).IsRequired();

+0

一個人的'int',另一個是'long' –

+0

肯定,但我怎麼能說,關鍵屬性類型是長 – user3387996

回答

1

在EF上下文配置,特別是在HasForeignKey()你應該指定Text模型,屬性應該是指向User模型外鍵?

由於User模型的主鍵是一個int,外鍵指向從TextUser自然應該也是一個int

我認爲你所犯的錯誤是,你配置的Text的PK也是FK的關係Text - >User。試着改變你的Text模式:

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

而且你的配置:

e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.UserId).IsRequired(); 
+0

如果我添加一個新的模式:客戶具有相同,ICollection的的集合文本。你的答案不會奏效,是不是有解決方案,我可以在這裏解決'自然'的行爲? – user3387996

+0

你能否詳細說明我的答案不會起作用,你添加了什麼模型?也許有一些代碼呢?不可能改變自然行爲,因爲外鍵是應該唯一標識另一個表中的行的東西。如果該行的標識(主鍵)是一個'int',那麼您可以用來標識該行的唯一數據類型是'int',否則將無法識別它。 – valorl

+0

我對我的帖子進行了更新:p – user3387996

0

使用EF核心也有類似的問題,但並不想包括(在我的等價類),用戶ID的依賴實體文本,只是爲了讓快樂的EF。最後發現,你可以替換)的關係(用戶ID)所使用的主鍵 使用HasPrimaryKey(

modelBuilder.Entity<User>() 
     .HasMany(t => t.Texts) 
     .WithOne() 
     .HasPrincipalKey(u => u.Text); 
0

首先,改變你的型號命名,請,

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); 
     }); 

我們必須找出原因哪個鍵被引用是因爲多個主鍵。我曾在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); 
     });