gpt4 book ai didi

c# - Entity Framework 核心 : FK with Different DataType than PK

转载 作者:行者123 更新时间:2023-11-30 14:46:26 25 4
gpt4 key购买 nike

运行时:netcoreapp2.0

ASP.Net 核心:2.0.1

EF 核心: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.

现在使用 Fluent 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://learn.microsoft.com/es-es/ef/core/modeling/value-conversions

关于c# - Entity Framework 核心 : FK with Different DataType than PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com