【问题标题】:The required column 'CustomerId' was not present in the results of a 'FromSql' operation“FromSql”操作的结果中不存在所需的“CustomerId”列
【发布时间】:2019-07-20 04:10:45
【问题描述】:

错误信息很清楚:

'所需的列 'CustomerId' 未出现在结果中 'FromSql' 操作'

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

错误发生在这里:

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

控制器:

  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 结果集 && DbContext 的打印 scrn:

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; }

    }

【问题讨论】:

    标签: c# entity-framework asp.net-core entity-framework-core


    【解决方案1】:

    CustomerId 是由引入的一对多关系的外键的常规名称

    public ICollection<Contacts> Contacts { get; set; }
    public ICollection<Addresses> Addresses { get; set; }
    

    Customer 类的集合导航属性。

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

    当发现关系但在依赖实体类中没有找到外键属性时,可以按照约定创建影子属性。在这种情况下,将引入影子外键属性。影子外键属性将被命名为&lt;navigation property name&gt;&lt;principal key property name&gt;(依赖实体上的导航,它指向主体实体,用于命名)。如果主键属性名称包含导航属性的名称,则名称将为&lt;principal key property name&gt;。如果依赖实体上没有导航属性,则使用主体类型名称代替它。

    要将Customer 属性映射为FK,您应该使用ForeignKey 属性:

    您可以使用数据注释来配置应将哪个属性用作给定关系的外键属性。这通常在未按惯例发现外键属性时完成。

    提示
    [ForeignKey] 注释可以放置在关系中的任一导航属性上。不需要去依赖实体类中的导航属性。

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

    [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
    

    【讨论】:

    • 非常感谢!会尝试一下并回复你;)
    【解决方案2】:

    能否请您也发布DB上下文类的相关部分和存储过程的示例结果。

    没有这些,我只能猜测:

    Identity 列没有标记[Key] 属性,也不遵循“EntityNameId”的标准命名约定,因此无法扣除标识列。

    所以,我建议将[Key] 属性添加到Id 属性:

    [Key]
    public Guid Id { get; set; }
    

    如果这不起作用,请发布上下文和 SP 结果。

    【讨论】:

    • 我只在 dbcontext 类中声明了 dbset。另外:执行第一个 proc 工作正常(为了获得客户)我尝试在需要的地方添加 [Key] 但这并没有改变任何东西,不幸的是我将添加 proc 结果集和上下文的 prnt scrn。
    • 更新了问题。
    • 那么,如果你强制列名? [Column("Customer")] Customer 属性的属性?
    • 我什至没有运气就将 Guid Customer 更改为 nvarchar。它与id有关
    • 也许尝试在上下文中将 DbSet 更改为 DbQuery
    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多