gpt4 book ai didi

c# - 'CustomerId' 操作的结果中不存在所需的列 'FromSql'

转载 作者:太空狗 更新时间:2023-10-30 01:12:51 26 4
gpt4 key购买 nike

错误信息很清楚:

'The required column 'CustomerId' was not present in the results of a 'FromSql' operation'

但不知何故我真的没想到 CustomerId?

错误发生在这里:

contacts = db.Contacts.FromSql("SIP_API_MONDIA_Contacts_sel").ToList();
addresses = db.Addresses.FromSql("SIP_API_MONDIA_Address_sel").ToList();

Controller :

  public IList<Customer> GetAllCustomers()
{
//Initialize the objects
IList<Customer> customers = null;
IList<Contacts> contacts = null;
IList<Addresses> addresses = null;

//Fetch the data from stored procedures
customers = db.Customers.FromSql("SomeProcName").ToList();
contacts = db.Contacts.FromSql("SomeProcName").ToList();
addresses = db.Addresses.FromSql("SomeProcName").ToList();

//Loop through customers and add the contact and addresses when required
foreach(var item in customers)
{
item.Contacts = contacts.Where(x => x.Customer == item.Id).ToList();
item.Addresses = addresses.Where(x => x.Customer == item.Id).ToList();
}
return customers;
}

模型:

public class Customer
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string VatCode { get; set; }
public string ChamberOfCommerceCode { get; set; }
public DateTime Modified { get; set; }
public DateTime Created { get; set; }
public string LanguageCode { get; set; }
public decimal Discount { get; set; }
public string CustomerManager { get; set; }
public Guid PriceList { get; set; }
public Guid PaymentCondition { get; set; }
// public bool VatLiable { get; set; }
public bool IsBlocked { get; set; }
public bool IsProspect { get; set; }
public bool IsSuspect { get; set; }
public string Website { get; set; }
public string DashboardUrl { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
// public ICollection<FreeFields> FreeFields { get; set; }
// public Dictionary<string, string> UknownElements { get; set; }
public ICollection<Contacts> Contacts { get; set; }
public ICollection<Addresses> Addresses { get; set; }
}

public class FreeFields
{
public string Key { get; set; }
public string Value { get; set; }
}

public class Contacts
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Initials { get; set; }
public string Function { get; set; }
public Guid Customer { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string LanguageCode { get; set; }
public bool IsMainContact { get; set; }
public string Gender { get; set; }
public string Username { get; set; }
}
public class Addresses
{
public Guid Id { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string Postcode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string CountryCode { get; set; }
public string Type { get; set; }
public Guid Customer { get; set; }// This Property should be GUID instead of String..
public bool IsMainAddress { get; set; }
public string Route { get; set; }
public string State { get; set; }
}

我不完全确定“CustomerId”错误的含义存储过程返回模型的 100% 精确值。

编辑以添加 sql 结果集的打印 scrn && DbContext:

public class IsahContext : DbContext
{
public IsahContext()
{

}

public IsahContext(DbContextOptions<IsahContext> options)
: base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(Setting.ConnectionString);
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}

//Entities will come here
public DbSet<Customer> Customers { get; set; }
public DbSet<Addresses> Addresses { get; set; }
public DbSet<Contacts> Contacts { get; set; }

}

enter image description here

enter image description here

最佳答案

CustomerId

引入的一对多关系的外键的约定名称
public ICollection<Contacts> Contacts { get; set; }
public ICollection<Addresses> Addresses { get; set; }

Customer 的集合导航属性类(class)。

虽然相关类ContactsAddresses包含属性 Guid Customer ,由于它的名字,它不被识别为外键,所以它属于 No Foreign Key Property类别。 EF Core 假定一个名为 CustomerId 的影子属性(和列) . Shadow property convention解释是:

Shadow properties can be created by convention when a relationship is discovered but no foreign key property is found in the dependent entity class. In this case, a shadow foreign key property will be introduced. The shadow foreign key property will be named <navigation property name><principal key property name> (the navigation on the dependent entity, which points to the principal entity, is used for the naming). If the principal key property name includes the name of the navigation property, then the name will just be <principal key property name>. If there is no navigation property on the dependent entity, then the principal type name is used in its place.

为了映射Customer属性作为 FK,您应该使用 ForeignKey属性:

You can use the Data Annotations to configure which property should be used as the foreign key property for a given relationship. This is typically done when the foreign key property is not discovered by convention.

Tip
The [ForeignKey] annotation can be placed on either navigation property in the relationship. It does not need to go on the navigation property in the dependent entity class.

例如(因为您在依赖实体中没有导航属性):

[ForeignKey(nameof(Contacts.Customer))]
public ICollection<Contacts> Contacts { get; set; }

[ForeignKey(nameof(Addresses.Customer))]
public ICollection<Addresses> Addresses { get; set; }

Fluent API :

modelBuilder.Entity<Customer>()
.HasMany(customer => customer.Contacts)
.WithOne() // no nav property
.HasForeignKey(contact => contact.Customer); // the FK property

modelBuilder.Entity<Customer>()
.HasMany(customer => customer.Addresses)
.WithOne() // no nav property
.HasForeignKey(address => address.Customer); // the FK property

关于c# - 'CustomerId' 操作的结果中不存在所需的列 'FromSql',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54888517/

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