gpt4 book ai didi

c# - 获取 SQL 错误 : Invalid column name 'PrimaryContact_ContactID'

转载 作者:太空狗 更新时间:2023-10-29 23:28:21 25 4
gpt4 key购买 nike

有人能帮我摆脱痛苦,让我知道我做过/没做过什么导致我的代码失败吗?

我首先使用 Entity Framework 数据库。当我查询一个实体并请求包含一个相关实体时,我收到此错误:列名称“PrimaryContact_ContactID”无效。在调用 Load 方法的行上抛出异常。

我正在尝试查询其中一个 Enterprise 实体,并让它也包含 Company 实体。

(这是一个重复异常的简化示例)

这是我的实体类:

//Enterprise
public class Enterprise
{
public int Id { get; set; }
public string Name { get; set; }
public int CompanyID { get; set; }

public virtual Company Company { get; set; }
}

//Company
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public int PrimaryContactId { get; set; }

public virtual CompanyContact PrimaryContact { get; set; }
}

//CompanyContact class
public class CompanyContact
{
public int ContactID { get; set; }
public string Name { get; set; }
}

这是DataContext

public class DataContext : DbContext
{
public DbSet<Enterprise> Enterprises { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<CompanyContact> CompanyContacts { get; set; }

public DataContext(string connStrName = nameof(DataContext)) : base($"Name={connStrName}")
{
Database.SetInitializer<DataContext>(null);
base.Configuration.ProxyCreationEnabled = false;
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

modelBuilder.Entity<Enterprise>().HasKey(pk => pk.Id);
modelBuilder.Entity<Enterprise>().Property(i => i.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

modelBuilder.Entity<Company>().HasKey(pk => pk.Id);
modelBuilder.Entity<Company>().Property(i => i.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Company>().HasOptional(t => t.PrimaryContact);

modelBuilder.Entity<CompanyContact>().HasKey(pk => pk.ContactID);
modelBuilder.Entity<CompanyContact>().Property(i => i.ContactID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

}
}

这里是我调用代码的 Main 方法。

    static void Main(string[] args)
{
try
{
using (var ctx = new DataContext())
{
ctx.Database.Log = Console.Write;

var ent = ctx.Enterprises.FirstOrDefault(e => e.Id == 1);

//Exception thrown on this line.
ctx.Entry(ent).Reference(e => e.Company).Load();

Console.WriteLine(ent);
Console.WriteLine(ent.Company);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}

Console.ReadLine();
}

我打开了 EF 日志记录,这里是运行代码的输出(为了让问题更简短,我没有包括完整的堆栈跟踪):

Opened connection at 2/22/2019 3:43:02 PM -06:00
SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[CompanyID] AS [CompanyID]
FROM [dbo].[Enterprise] AS [Extent1]
WHERE 1 = [Extent1].[Id]
-- Executing at 2/22/2019 3:43:02 PM -06:00
-- Completed in 85 ms with result: SqlDataReader

Closed connection at 2/22/2019 3:43:02 PM -06:00
Opened connection at 2/22/2019 3:43:02 PM -06:00
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[PrimaryContactID] AS [PrimaryContactID],
[Extent1].[PrimaryContact_ContactID] AS [PrimaryContact_ContactID]
FROM [dbo].[Company] AS [Extent1]
WHERE [Extent1].[Id] = @EntityKeyValue1
-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)
-- Executing at 2/22/2019 3:43:02 PM -06:00
-- Failed in 68 ms with error: Invalid column name 'PrimaryContact_ContactID'.

Closed connection at 2/22/2019 3:43:02 PM -06:00
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'PrimaryContact_ContactID'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrap

最后,这是数据库架构图:

Database Diagram

我确定我一定遗漏了一些基本的东西,但经过大量搜索后,我仍无法解决我的问题。

最佳答案

您需要执行 [ForeignKey] 不遵循 EF 默认约定的导航属性的属性:

public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public int PrimaryContactId { get; set; }

[ForeignKey(nameof(PrimaryContactId))]
public virtual CompanyContact PrimaryContact { get; set; }
}

否则,EF 将无法识别您的 PrimaryContactId属性作为 PrimaryContact 的列它将创建另一列,PrimaryContact_ContactID .

关于c# - 获取 SQL 错误 : Invalid column name 'PrimaryContact_ContactID' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54835691/

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