簡體   English   中英

實體框架核心:具有與PK不同的DataType的FK

[英]Entity Framework Core: FK with Different DataType than PK

運行時 :netcoreapp2.0

ASP.Net核心 :2.0.1

EF Core :2.0.1

Microsoft.EntityFrameworkCore.SqlServer :2.0.1

所以,我正在處理一個被許多服務使用的舊數據庫,原始創建者將一個表的PK定義為short,將該表的FK定義為int。 我可以處理這個案子嗎?

注意:此時更改列類型是不可行的。

考慮兩個類:

public class Database {
  public short DatabaseId { get; set; }
  public Database { get; set; }
}

public class Client {
  public int ClientId { get; set; }
  public int DatabaseId { get; set; }
  public Database Database { get; set; }
}

我的第一次嘗試:

public int DatabaseId { get; set; }
[ForeignKey("DatabaseId")] public Database Database { get; set; }

System.InvalidOperationException: The relationship from 'Client.Database' to 'Database.Client' with foreign key properties {'DatabaseId' : int} cannot target the primary key {'DatabaseId' : short} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.

現在使用流暢的API:

modelBuilder.Entity<Client>(table => {    
    table.HasOne(p => p.Database)
        .WithOne(i => i.Client)
        .HasForeignKey<Client>(c => c.DatabaseId)
        .HasPrincipalKey<Database>(d => d.DatabaseID);
});

嘗試定義陰影屬性失敗

modelBuilder.Entity<Client>(table => {
    table.Property<int>("DatabaseId");

    table.HasOne(p => p.Database)
        .WithOne(i => i.Client)
        .HasForeignKey("DatabaseId")
        .HasPrincipalKey<Database>(d => d.DatabaseId);
});

System.InvalidOperationException: You are configuring a relationship between 'Client' and 'Database' but have specified a foreign key on 'DatabaseId'. The foreign key must be defined on a type that is part of the relationship.

好的,這次沒有影子屬性:

modelBuilder.Entity<Client>(table => {

    table.HasOne(p => p.Database)
        .WithOne(i => i.Client)
        .HasForeignKey<Client>(c => c.DatabaseId)
        .HasPrincipalKey<Database>(d => d.SQLDatabaseID);
});

System.InvalidOperationException: The types of the properties specified for the foreign key {'DatabaseId'} on entity type 'Client' do not match the types of the properties in the principal key {'DatabaseID'} on entity type 'Database'.

所以,然后我嘗試只更改實體類型,也許EF可以將int映射到int16。

public class Client {
  public int ClientId { get; set; }
  public short DatabaseId { get; set; } // <-- now a short
  public Database Database { get; set; }
}

System.InvalidOperationException: An exception occurred while reading a database value for property 'Client.DatabaseId'. The expected type was 'System.Int16' but the actual value was of type 'System.Int32'. ---> System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int16'.

我開始覺得這是不可能的。

在EF Core 2.1中,您可以使用值轉換,即

entityBuilder.Property(c => c.Id).HasConversion<string>(); 

其中通用字符串是數據庫類型,目標是模型類型。 您可以在https://docs.microsoft.com/es-es/ef/core/modeling/value-conversions上進一步閱讀

您可以使用以下代碼來繞過實體框架生成的錯誤來定義外鍵

[Column(TypeName = "int")]
public short DatabaseId { get; set; }

但是一旦你運行遷移,你將收到此錯誤,這是由sql server生成的。

列'Databases.DatabaseId'與外鍵'FK_Clients_Databases_DatabaseId'中引用列'Clients.DatabaseId'的數據類型不同。

這很有道理。 您嘗試實現的內容在物理上是不可能的,因為SQL Server要求外鍵是相同的類型,否則您無法構建外鍵關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM