从视图的子表中进行选择时,Entity Framework Core 会生成一个新列

发布于 2025-01-09 19:06:41 字数 3169 浏览 0 评论 0原文

我的数据库中有一个表 Customer ,并且有一个外部数据库表 Customer ,其中包含大量信息,我创建了一个 CustomerAccount 视图来表示此信息

查看 - CustomerAccount

CREATE VIEW CustomerAccount AS
SELECT c.Id, c.CustomerNumber, a.Name, ISNULL(DateTimeCreated, 0) as CreatedDate, ISNULL(DateTimeUpdated, ISNULL(DateTimeCreated, 0)) as UpdatedDate, ~InactiveFlag as Active 
FROM externaldatabase..CMAccount a
INNER JOIN mydatabase..Customer c on a.URI = c.CustomerNumber;

我的表 - Customer

CREATE TABLE [dbo].[Customer](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CustomerNumber] [int] NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [UpdatedDate] [datetime] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Product1Product2 表具有我的数据库Customer<的外键/strong> table

表的配置Product1

public void Configure(EntityTypeBuilder<Product1> builder)
{
    builder
        .HasKey(b => b.Id);

    // all other properties

    builder
        .HasOne(b => b.Customer)
        .WithMany();
}

表 Product2 的配置

public void Configure(EntityTypeBuilder<Product2> builder)
{
    builder
        .HasKey(b => b.Id);

    // all other properties

    builder
        .HasOne(b => b.Customer)
        .WithMany();
}

表 Customer 的配置:

public void Configure(EntityTypeBuilder<Customer> builder)
{
    builder
        .HasKey(b => b.Id);

    builder
        .HasIndex(b => b.CustomerNumber);

    builder
        .Property(b => b.CustomerNumber)
        .IsRequired();

    builder
        .Property(b => b.CreatedDate)
        .HasColumnType("datetime")
        .IsRequired();

    builder
        .Property(b => b.UpdatedDate)
        .HasColumnType("datetime")
        .IsRequired();

    builder
        .Property(b => b.Active)
        .IsRequired();
}

视图 CustomerAccount 的配置:

public void Configure(EntityTypeBuilder<CustomerAccount> builder)
{
    builder
        .ToView(nameof(CustomerAccount));

    builder
        .HasMany(c => c.Product1Setups)
        .WithOne()
        .HasForeignKey(p1 => p1.CustomerId);

    builder
        .HasMany(c => c.Product2Setups)
        .WithOne()
        .HasForeignKey(p2 => p2.CustomerId);
}

即使我使用字符串重载如下所示,同样的情况发生:

builder
    .HasMany(c => c.Product2Setups)
    .WithOne()
    .HasForeignKey("CustomerId");

我期望它们之间只有逻辑关系,但是当从 Product1 或 Product2 中进行选择时,EF 核心会生成一个具有无效列名 CustomerId1 的查询。两个表都已经是上面示例中的 mydatabase..Customer 引用的名为 CustomerId 的列。

有什么解决办法吗?

更新 - TL/DR:

总之,我需要做的是让两个父对象、一个表和一个视图由子表中的相同列引用,因为视图共享相同的唯一值基本上是关键。

I have a table in my database Customer and have an external database table Customer with a lot of information, I created a CustomerAccount view for representing this

View - CustomerAccount

CREATE VIEW CustomerAccount AS
SELECT c.Id, c.CustomerNumber, a.Name, ISNULL(DateTimeCreated, 0) as CreatedDate, ISNULL(DateTimeUpdated, ISNULL(DateTimeCreated, 0)) as UpdatedDate, ~InactiveFlag as Active 
FROM externaldatabase..CMAccount a
INNER JOIN mydatabase..Customer c on a.URI = c.CustomerNumber;

My table - Customer

CREATE TABLE [dbo].[Customer](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CustomerNumber] [int] NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [UpdatedDate] [datetime] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Product1 and Product2 tables have foreign keys for my database Customer table

Configuration for the table Product1

public void Configure(EntityTypeBuilder<Product1> builder)
{
    builder
        .HasKey(b => b.Id);

    // all other properties

    builder
        .HasOne(b => b.Customer)
        .WithMany();
}

Configuration for the table Product2

public void Configure(EntityTypeBuilder<Product2> builder)
{
    builder
        .HasKey(b => b.Id);

    // all other properties

    builder
        .HasOne(b => b.Customer)
        .WithMany();
}

Configuration for the table Customer:

public void Configure(EntityTypeBuilder<Customer> builder)
{
    builder
        .HasKey(b => b.Id);

    builder
        .HasIndex(b => b.CustomerNumber);

    builder
        .Property(b => b.CustomerNumber)
        .IsRequired();

    builder
        .Property(b => b.CreatedDate)
        .HasColumnType("datetime")
        .IsRequired();

    builder
        .Property(b => b.UpdatedDate)
        .HasColumnType("datetime")
        .IsRequired();

    builder
        .Property(b => b.Active)
        .IsRequired();
}

Configuration for the view CustomerAccount:

public void Configure(EntityTypeBuilder<CustomerAccount> builder)
{
    builder
        .ToView(nameof(CustomerAccount));

    builder
        .HasMany(c => c.Product1Setups)
        .WithOne()
        .HasForeignKey(p1 => p1.CustomerId);

    builder
        .HasMany(c => c.Product2Setups)
        .WithOne()
        .HasForeignKey(p2 => p2.CustomerId);
}

Even if I use the string overload as below the same happens:

builder
    .HasMany(c => c.Product2Setups)
    .WithOne()
    .HasForeignKey("CustomerId");

I was expecting only a logical relationship between those, but when selecting from Product1 or Product2 EF core generates a query with an invalid column name CustomerId1. Both tables are already a column named CustomerId referenced by mydatabase..Customer of the example above.

Is there any solution for that?

UPDATE - TL/DR:

In summary what I need to do is to have two parent objects, one table and one view being referenced by the same columns in the children tables, since the view shares the same unique key basically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文