繁体   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