错误信息是明确的:
‘必需的'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%精确值。
为了添加结果集的打印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; }
    }


发布于 2019-02-26 16:54:17
类引入的一对多关系的外键的常规名称CustomerId。
public ICollection<Contacts> Contacts { get; set; }
public ICollection<Addresses> Addresses { get; set; }Customer类的集合导航属性。
虽然相关的类Contacts和Addresses包含一个属性Guid Customer,但由于它的名称不能识别为外键,所以它属于无外键财产类别。并且EF假定有一个名为CustomerId的影子属性(和列)。影子财产公约的解释是:
如果发现关系,但在依赖实体类中没有找到外键属性,则可以按照约定创建阴影属性。在这种情况下,将引入影子外键属性。影子外键属性将命名为
<navigation property name><principal key property name>(指向主体实体的依赖实体上的导航用于命名)。如果主体键属性名称包括导航属性的名称,则名称将仅为<principal key property name>。如果依赖实体上没有导航属性,则在其位置使用主体类型名称。
为了将Customer属性映射为FK,您应该使用以下两个ForeignKey属性:
您可以使用数据注释来配置应该将哪个属性用作给定关系的外键属性。这通常是在约定未发现外键属性时完成的。 Tip
[ForeignKey]注释可以放在关系中的任一导航属性上。它不需要访问依赖实体类中的导航属性。
例如(因为在依赖实体中没有导航属性):
[ForeignKey(nameof(Contacts.Customer))]
public ICollection<Contacts> Contacts { get; set; }
[ForeignKey(nameof(Addresses.Customer))]
public ICollection<Addresses> Addresses { get; set; }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发布于 2019-02-26 15:17:46
您还可以发布DB上下文类的相关部分和存储过程的示例结果吗?
没有这些,我只能猜测:
标识列没有使用[Key]属性标记,也不遵循"EntityNameId“的标准命名约定,因此无法扣除标识列。
因此,我建议将[Key]属性添加到Id属性中:
[Key]
public Guid Id { get; set; }如果这不起作用,请张贴上下文和SP结果。
https://stackoverflow.com/questions/54888517
复制相似问题